您的位置:首页 > 产品设计 > UI/UE

Codeforces 426E Sereja and Two Sequences【思维+Dp+二分】好题!

2017-09-26 15:10 621 查看
E. Sereja and Two Sequences

time limit per test
4 seconds

memory limit per test
512 megabytes

input
standard input

output
standard output

Sereja has two sequences a1, a2, ..., an and b1, b2, ..., bm,
consisting of integers. One day Sereja got bored and he decided two play with them. The rules of the game was very simple. Sereja makes several moves, in one move he can perform one of the following actions:

Choose several (at least one) first elements of sequence a (non-empty prefix of a),
choose several (at least one) first elements of sequence b (non-empty prefix of b);
the element of sequence a with the maximum index among the chosen ones must be equal to the element of sequence b with
the maximum index among the chosen ones; remove the chosen elements from the sequences.

Remove all elements of both sequences.

The first action is worth e energy units and adds one dollar to Sereja's electronic account. The second action is worth the number
of energy units equal to the number of elements Sereja removed from the sequences before performing this action. After Sereja performed the second action, he gets all the money that he earned on his electronic account during the game.

Initially Sereja has s energy units and no money on his account. What maximum number of money can Sereja get? Note, the amount of Seraja's
energy mustn't be negative at any time moment.

Input

The first line contains integers n, m, s, e (1 ≤ n, m ≤ 105; 1 ≤ s ≤ 3·105; 103 ≤ e ≤ 104).
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 105).
The third line contains m integers b1, b2, ..., bm (1 ≤ bi ≤ 105).

Output

Print a single integer — maximum number of money in dollars that Sereja can get.

Examples

input
5 5 100000 1000
1 2 3 4 5
3 2 4 5 1


output
3


input
3 4 3006 1000
1 2 31 2 4 3


output
2


题目大意:

给出一个长度为N的序列,再给出长度为M的一个序列,现在有两种操作:

①在序列1中剩余部分中选择一个数a【pos】;再在序列2中剩余部分中选择一个数b【pos2】,需要保证a【pos】==b【pos2】,然后我们将序列1中从1到pos这段数字全部删除,再将序列2中从1到pos2这段数字全部删除,并且获得无效的一个金币,需要花费体力为e.

②操作2需要花费的体力是操作1一共删除的数字的个数,然后将获得的所有无效的美元变成有效的。

问最终我们最多可以获得多少有效的金币。

最初体力为S;

思路:

①观察数据范围,我们知道,S/E的最大值最高为300.也就是说,我们最多可能获得的金币的个数为300枚。

②我们通过简单的思考可以得知,简单的贪心方案总会有纰漏,考虑到问题是求最优解,那么去想Dp,设定Dp【300(i)】【j】=x表示我们获得了i枚无效的金币,序列1已经删除到了位子j的情况下,b数组删除的最少的个数为x个。

那么不难想到状态转移方程:Dp【i】【j】=min(从位子Dp【i-1】【k】向右遍历,直到第一个b【pos】==a【j】为止)【k<j】;

③我们发现,这里Dp【i-1】【k】中我们没有必要每个k都遍历到,我们只要找到最小值即可。

那么状态转移方程优化到:Dp【i】【j】=min(从pos开始遍历,每次pos++,直到第一个b【pos】==a【j】为止)pos=min(Dp【i-1】【k】)【k<j】;

这里求pos可以维护一个二维前缀最小值,然后O(1)就可以查询的到。

④我们知道,从位子pos开始遍历,其具有单调性,那么我们将a【j】这个值在b数组中出现的位子存到数组中,然后遍历的过程进行二分查找优化即可。。

过程维护一下就行了。

Ac代码:

#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
int a[150000],b[1500000];
vector<int>mp[150000];
int dp[325][150000];
int minn[325][150000];
int main()
{
int n,m,A,B;
while(~scanf("%d%d%d%d",&n,&m,&A,&B))
{
for(int i=1;i<=100000;i++)mp[i].clear();
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=m;i++)
{
scanf("%d",&b[i]);
mp[b[i]].push_back(i);
}
memset(minn,0,sizeof(minn));
for(int i=1;i<=300;i++)
{
for(int j=0;j<=n;j++)
{
minn[i][j]=0x3f3f3f3f;
}
}
int output=0;
for(int i=1;i<=300;i++)
{
for(int j=1;j<=n;j++)
{
int pre=minn[i-1][j-1];
minn[i][j]=minn[i][j-1];
int ans=-1;
int l=0;
int r=mp[a[j]].size()-1;
while(r-l>=0)
{
int mid=(l+r)/2;
if(mp[a[j]][mid]>pre)
{
r=mid-1;
ans=mp[a[j]][mid];
}
else l=mid+1;
}
if(ans==-1)continue;
if(i*B+j+ans<=A)output=i;
minn[i][j]=min(minn[i][j-1],ans);
}
}
printf("%d\n",output);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Codeforces 426E