Codeforces Round #378 (Div. 2)C. Epidemic in Monstropolis【思维+暴力】
2016-11-01 11:56
429 查看
C. Epidemic in Monstropolis
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
There was an epidemic in Monstropolis and all monsters became sick. To recover, all monsters lined up in queue for an appointment to the only doctor in the city.
Soon, monsters became hungry and began to eat each other.
One monster can eat other monster if its weight is
strictly greater than the weight of the monster being eaten, and they stand in the queue next to each other. Monsters eat each other instantly. There are no monsters which are being eaten at the same moment. After the monster
A eats the monster
B, the weight of the monster A increases by the weight of the eaten monster
B. In result of such eating the length of the queue decreases by one, all monsters after the eaten one step forward so that there is no empty places in the queue again. A monster can eat several monsters one after another.
Initially there were n monsters in the queue, the
i-th of which had weight
ai.
For example, if weights are [1, 2, 2, 2, 1, 2] (in order of queue, monsters are numbered from
1 to 6 from left to right) then some of the options are:
the first monster can't eat the second monster because
a1 = 1 is not greater than
a2 = 2;
the second monster can't eat the third monster because
a2 = 2 is not greater than
a3 = 2;
the second monster can't eat the fifth monster because they are not neighbors;
the second monster can eat the first monster, the queue will be transformed to
[3, 2, 2, 1, 2].
After some time, someone said a good joke and all monsters recovered. At that moment there were
k (k ≤ n) monsters in the queue, the
j-th of which had weight
bj. Both sequences (a and
b) contain the weights of the monsters in the order from the first to the last.
You are required to provide one of the possible orders of eating monsters which led to the current queue, or to determine that this could not happen. Assume that the doctor didn't make any appointments while monsters were eating each other.
Input
The first line contains single integer n (1 ≤ n ≤ 500) — the number of monsters in the initial queue.
The second line contains n integers
a1, a2, ..., an (1 ≤ ai ≤ 106) —
the initial weights of the monsters.
The third line contains single integer k (1 ≤ k ≤ n) — the number of monsters in the queue after the joke.
The fourth line contains k integers
b1, b2, ..., bk (1 ≤ bj ≤ 5·108) —
the weights of the monsters after the joke.
Monsters are listed in the order from the beginning of the queue to the end.
Output
In case if no actions could lead to the final queue, print "NO" (without quotes) in the only line.
Otherwise print "YES" (without quotes) in the first line. In the next
n - k lines print actions in the chronological order. In each line print
x — the index number of the monster in the current queue which eats and, separated by space, the symbol 'L' if the monster which stays the
x-th in the queue eats the monster in front of him, or 'R' if the monster which stays the
x-th in the queue eats the monster behind him. After each eating the queue is enumerated again.
When one monster eats another the queue decreases. If there are several answers, print any of them.
Examples
Input
Output
Input
Output
Input
Output
Note
In the first example, initially there were n = 6 monsters, their weights are
[1, 2, 2, 2, 1, 2] (in order of queue from the first monster to the last monster). The final queue should be
[5, 5]. The following sequence of eatings leads to the final queue:
the second monster eats the monster to the left (i.e. the first monster), queue becomes
[3, 2, 2, 1, 2];
the first monster (note, it was the second on the previous step) eats the monster to the right (i.e. the second monster), queue becomes
[5, 2, 1, 2];
the fourth monster eats the mosnter to the left (i.e. the third monster), queue becomes
[5, 2, 3];
the finally, the third monster eats the monster to the left (i.e. the second monster), queue becomes
[5, 5].
Note that for each step the output contains numbers of the monsters in their current order in the queue.
题目大意:
给你一个数组a,让你将数组a变成目标数组b,
类似大鱼吃小鱼的操作,当一个数严格大于另一个相邻的数的时候,这个数就能吃掉那个小数,然后相对位子编号发生变化,问是否能达到目的,如果可以,输出YES,同时输出任意可行方案。否则输出NO,
思路:
1、首先数组b的相对位子是确定的,那么我们首先确定是否能够从a数组通过对每一段的截取获得出b数组的值。
例如样例:
1 2 2 2 1 2
2
5 5
我们先判断哪些数得到第一个5,哪些数得到第二个5、
1 2 2 | 2 1 2,很明显,这样就是一个可行解。
那么我们在这样判断的过程中,值得注意的一点是a数组是否会有剩余,如果有剩余,那么相当于a数组无法达到目标b数组:
1 2 3 4 5
1
10
我们是一定会剩出来一个5,那么就是无法达到目标。
2、然后我们将a数组分块处理,对应第一个块能够得到b数组中的第一个数,那么我们可以枚举这个区间中的任意一个位子作为起点,然后向左右吃,能吃就吃,如果两边都吃不了,那么枚举下一个位子,如果某一个位子能够吃掉所有区间内的数,那么当前这个方案就是这个区间的可行解,记录下来,然后进行下一个区间即可。
3、时间复杂度约为O(n^3),因为n不大,所以是可行的。
Ac代码:
#include<stdio.h>
#include<string.h>
using namespace std;
int a[505];
int b[505];
int ans[505][2];
int output[505][2];
int n,k,contz;
int judge()
{
int sum=0;
int now=1;
int flag=0;
for(int i=1;i<=n;i++)
{
sum+=a[i];
if(sum==b[now])
{
now++;
sum=0;
if(now==k+1&&i==n)flag=1;
}
}
return flag;
}
int Slove(int x,int y,int pre)
{
for(int i=x;i<=y;i++)
{
int cont=0;
int sum=a[i];
int l=i-1;
int r=i+1;
while(1)
{
int flag=0;
while(l>=x&&sum>a[l])
{
flag=1;
sum+=a[l];
ans[cont][0]=pre+l-x+2;
ans[cont++][1]=0;
l--;
}
while(r<=y&&sum>a[r])
{
flag=1;
sum+=a[r];
ans[cont][0]=pre+l-x+2;
ans[cont++][1]=1;
r++;
}
if(sum==b[pre+1])
{
for(int i=0;i<cont;i++)
{
output[contz][0]=ans[i][0];
output[contz++][1]=ans[i][1];
}
return 1;
}
if(flag==0)break;
}
}
return 0;
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
scanf("%d",&k);
for(int i=1;i<=k;i++)
{
scanf("%d",&b[i]);
}
if(judge()==1)
{
contz=0;
int sum=0;
int l=1;
int now=1;
int flag=0;
for(int i=1;i<=n;i++)
{
sum+=a[i];
if(sum==b[now])
{
int tt=Slove(l,i,now-1);
if(tt==1)
{
l=i+1;
sum=0;
now++;
}
else
{
flag=1;break;
}
}
}
if(flag==1)
{
printf("NO\n");
}
else
{
printf("YES\n");
for(int i=0;i<contz;i++)
{
if(output[i][1]==0)
{
printf("%d L\n",output[i][0]);
}
else printf("%d R\n",output[i][0]);
}
}
}
else printf("NO\n");
}
}
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
There was an epidemic in Monstropolis and all monsters became sick. To recover, all monsters lined up in queue for an appointment to the only doctor in the city.
Soon, monsters became hungry and began to eat each other.
One monster can eat other monster if its weight is
strictly greater than the weight of the monster being eaten, and they stand in the queue next to each other. Monsters eat each other instantly. There are no monsters which are being eaten at the same moment. After the monster
A eats the monster
B, the weight of the monster A increases by the weight of the eaten monster
B. In result of such eating the length of the queue decreases by one, all monsters after the eaten one step forward so that there is no empty places in the queue again. A monster can eat several monsters one after another.
Initially there were n monsters in the queue, the
i-th of which had weight
ai.
For example, if weights are [1, 2, 2, 2, 1, 2] (in order of queue, monsters are numbered from
1 to 6 from left to right) then some of the options are:
the first monster can't eat the second monster because
a1 = 1 is not greater than
a2 = 2;
the second monster can't eat the third monster because
a2 = 2 is not greater than
a3 = 2;
the second monster can't eat the fifth monster because they are not neighbors;
the second monster can eat the first monster, the queue will be transformed to
[3, 2, 2, 1, 2].
After some time, someone said a good joke and all monsters recovered. At that moment there were
k (k ≤ n) monsters in the queue, the
j-th of which had weight
bj. Both sequences (a and
b) contain the weights of the monsters in the order from the first to the last.
You are required to provide one of the possible orders of eating monsters which led to the current queue, or to determine that this could not happen. Assume that the doctor didn't make any appointments while monsters were eating each other.
Input
The first line contains single integer n (1 ≤ n ≤ 500) — the number of monsters in the initial queue.
The second line contains n integers
a1, a2, ..., an (1 ≤ ai ≤ 106) —
the initial weights of the monsters.
The third line contains single integer k (1 ≤ k ≤ n) — the number of monsters in the queue after the joke.
The fourth line contains k integers
b1, b2, ..., bk (1 ≤ bj ≤ 5·108) —
the weights of the monsters after the joke.
Monsters are listed in the order from the beginning of the queue to the end.
Output
In case if no actions could lead to the final queue, print "NO" (without quotes) in the only line.
Otherwise print "YES" (without quotes) in the first line. In the next
n - k lines print actions in the chronological order. In each line print
x — the index number of the monster in the current queue which eats and, separated by space, the symbol 'L' if the monster which stays the
x-th in the queue eats the monster in front of him, or 'R' if the monster which stays the
x-th in the queue eats the monster behind him. After each eating the queue is enumerated again.
When one monster eats another the queue decreases. If there are several answers, print any of them.
Examples
Input
6 1 2 2 2 1 2 2 5 5
Output
YES 2 L 1 R 4 L 3 L
Input
5 1 2 3 4 5 1 15
Output
YES 5 L 4 L 3 L 2 L
Input
5 1 1 1 3 3 3 2 1 6
Output
NO
Note
In the first example, initially there were n = 6 monsters, their weights are
[1, 2, 2, 2, 1, 2] (in order of queue from the first monster to the last monster). The final queue should be
[5, 5]. The following sequence of eatings leads to the final queue:
the second monster eats the monster to the left (i.e. the first monster), queue becomes
[3, 2, 2, 1, 2];
the first monster (note, it was the second on the previous step) eats the monster to the right (i.e. the second monster), queue becomes
[5, 2, 1, 2];
the fourth monster eats the mosnter to the left (i.e. the third monster), queue becomes
[5, 2, 3];
the finally, the third monster eats the monster to the left (i.e. the second monster), queue becomes
[5, 5].
Note that for each step the output contains numbers of the monsters in their current order in the queue.
题目大意:
给你一个数组a,让你将数组a变成目标数组b,
类似大鱼吃小鱼的操作,当一个数严格大于另一个相邻的数的时候,这个数就能吃掉那个小数,然后相对位子编号发生变化,问是否能达到目的,如果可以,输出YES,同时输出任意可行方案。否则输出NO,
思路:
1、首先数组b的相对位子是确定的,那么我们首先确定是否能够从a数组通过对每一段的截取获得出b数组的值。
例如样例:
1 2 2 2 1 2
2
5 5
我们先判断哪些数得到第一个5,哪些数得到第二个5、
1 2 2 | 2 1 2,很明显,这样就是一个可行解。
那么我们在这样判断的过程中,值得注意的一点是a数组是否会有剩余,如果有剩余,那么相当于a数组无法达到目标b数组:
1 2 3 4 5
1
10
我们是一定会剩出来一个5,那么就是无法达到目标。
2、然后我们将a数组分块处理,对应第一个块能够得到b数组中的第一个数,那么我们可以枚举这个区间中的任意一个位子作为起点,然后向左右吃,能吃就吃,如果两边都吃不了,那么枚举下一个位子,如果某一个位子能够吃掉所有区间内的数,那么当前这个方案就是这个区间的可行解,记录下来,然后进行下一个区间即可。
3、时间复杂度约为O(n^3),因为n不大,所以是可行的。
Ac代码:
#include<stdio.h>
#include<string.h>
using namespace std;
int a[505];
int b[505];
int ans[505][2];
int output[505][2];
int n,k,contz;
int judge()
{
int sum=0;
int now=1;
int flag=0;
for(int i=1;i<=n;i++)
{
sum+=a[i];
if(sum==b[now])
{
now++;
sum=0;
if(now==k+1&&i==n)flag=1;
}
}
return flag;
}
int Slove(int x,int y,int pre)
{
for(int i=x;i<=y;i++)
{
int cont=0;
int sum=a[i];
int l=i-1;
int r=i+1;
while(1)
{
int flag=0;
while(l>=x&&sum>a[l])
{
flag=1;
sum+=a[l];
ans[cont][0]=pre+l-x+2;
ans[cont++][1]=0;
l--;
}
while(r<=y&&sum>a[r])
{
flag=1;
sum+=a[r];
ans[cont][0]=pre+l-x+2;
ans[cont++][1]=1;
r++;
}
if(sum==b[pre+1])
{
for(int i=0;i<cont;i++)
{
output[contz][0]=ans[i][0];
output[contz++][1]=ans[i][1];
}
return 1;
}
if(flag==0)break;
}
}
return 0;
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
scanf("%d",&k);
for(int i=1;i<=k;i++)
{
scanf("%d",&b[i]);
}
if(judge()==1)
{
contz=0;
int sum=0;
int l=1;
int now=1;
int flag=0;
for(int i=1;i<=n;i++)
{
sum+=a[i];
if(sum==b[now])
{
int tt=Slove(l,i,now-1);
if(tt==1)
{
l=i+1;
sum=0;
now++;
}
else
{
flag=1;break;
}
}
}
if(flag==1)
{
printf("NO\n");
}
else
{
printf("YES\n");
for(int i=0;i<contz;i++)
{
if(output[i][1]==0)
{
printf("%d L\n",output[i][0]);
}
else printf("%d R\n",output[i][0]);
}
}
}
else printf("NO\n");
}
}
相关文章推荐
- Codeforces Round #392(Div. 2) C Unfair Poll【思维+暴力】
- Codeforces Round #378 (Div. 2) C. Epidemic in Monstropolis 模拟
- Codeforces Round #378 (Div. 2) C Epidemic in Monstropolis
- Codeforces Round #378 (Div. 2)C.Epidemic in Monstropolis(codeforces733c)
- CDOJ 每周一题div2 这是一道暴力题(暴力/思维)
- Codeforces Round #385 (Div. 2)B. Hongcow Solves A Puzzle【思维+暴力】
- Codeforces Round #336 (Div. 2)【A.思维,暴力,B.字符串,暴搜,前缀和,C.暴力,D,区间dp,E,字符串,数学】
- Codeforces Round #378 (Div. 2) -- C. Epidemic in Monstropolis (贪心模拟)
- Codeforces Round #378 (Div. 2) C.Epidemic in Monstropolis (分块处理)
- Codeforces Round #378 (Div. 2) C. Epidemic in Monstropolis(模拟)
- Codeforces#381(Div. 2) B.Alyona and flowers【思维+暴力】
- Codeforces Round #394(Div. 2)B. Dasha and friends【思维+暴力】
- Codeforces Round #410 (Div. 2)(A,字符串,水坑,B,暴力枚举,C,思维题,D,区间贪心)
- Codeforces Round #378 (Div. 2)-C. Epidemic in Monstropolis
- Codeforces Round #416 (Div. 2)(A,思维题,暴力,B,思维题,暴力)
- Codeforces Round #378 (Div. 2)C. Epidemic in Monstropolis
- Codeforces Round #401 (Div. 2)C. Alyona and Spreadsheet(暴力)
- Codeforces Round #386(Div. 2)D. Green and Black Tea【思维+构造】
- 暴力/进制转换 Codeforces Round #308 (Div. 2) C. Vanya and Scales
- 【Codeforces Round 336 (Div 2) C】【贪心 DP思维】Chain Reaction 每个灯塔位置为a[]破坏b[]范围所有灯塔 设置一个灯塔使得最多灯塔被保留