codeforces #378C(733.C) Epidemic in Monstropolis

来源:互联网 发布:淘宝穿过的鞋子退货 编辑:程序博客网 时间:2024/05/16 08:33

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 weightai.

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:

  1. the first monster can't eat the second monster because a1 = 1 is not greater than a2 = 2;
  2. the second monster can't eat the third monster because a2 = 2 is not greater than a3 = 2;
  3. the second monster can't eat the fifth monster because they are not neighbors;
  4. 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, thej-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 printx — 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
61 2 2 2 1 225 5
output
YES2 L1 R4 L3 L
input
51 2 3 4 5115
output
YES5 L4 L3 L2 L
input
51 1 1 3 332 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.


题目大意:有一些怪兽在排队,体重大的怪兽可以吃掉相邻的比它体重小的怪兽

构造一种顺序使得经过一些轮次后怪兽顺序和题中所要求的相同


我们可以发现肯定是ai中的连续一段合成bi的某个元素,且第一个肯定合成为第一个

那么只需要贪心扫描即可把ai分成k段满足bi

然后每段中找最大值的位置,如果有多个最大值,记录最左边那个。如果最左边是一连串的最大值,则记录最右边那个

然后特判下先往左还是先往右吃,向一个方向吃到边界再反方向吃回去即可

记录前面被吃掉了多少怪兽,统计偏移量即可快速计算出当前怪兽是第几个怪兽

#include<cstdio>#include<string>#include<cstring>#include<iostream>using namespace std;int a[100001],b[100001];struct asx{int l,r;}c[100001];int main(){int n;scanf("%d",&n);int i;for(i=1;i<=n;i++)scanf("%d",&a[i]);int k;scanf("%d",&k);for(i=1;i<=k;i++)scanf("%d",&b[i]);int d1=0,d2=1,la=1;int s1=0;bool flag=true;int p=0;while(d1<n){d1++;s1+=a[d1];if(s1==b[d2]){int i;int maxx=a[la],maxs=1;for(i=la+1;i<=d1;i++){if(a[i]>maxx){maxx=a[i];maxs=1;}else if(a[i]==maxx)maxs++;}if(la!=d1&&maxs==d1-la+1){flag=false;break;}p++;c[p].l=la;c[p].r=d1;s1=0;la=d1+1;d2++;}else if(s1>b[d2]){flag=false;break;}}if(s1!=0||d2!=k+1)flag=false;if(!flag)printf("NO\n");else{printf("YES\n");int j,k;int la=0;for(i=1;i<=p;i++){int maxx=a[c[i].l],maxi=c[i].l;bool flagx=true;for(j=c[i].l+1;j<=c[i].r;j++){if(a[j]>maxx){flagx=false;maxx=a[j];maxi=j;}else if(flagx&&a[j]==maxx&&a[j]==a[c[i].l]){maxx=a[j];maxi=j;}else if(a[j]<maxx)flagx=false;}if(maxx>a[maxi-1]){int lx=0;j=maxi;for(k=1;k<=j-c[i].l;k++){printf("%d L\n",j-la);la++;}lx=la;for(k=1;k<=c[i].r-j;k++){printf("%d R\n",j-la);lx++;}la=lx;}else{int lx=0;j=maxi;for(k=1;k<=c[i].r-j;k++){printf("%d R\n",j-la);lx++;}for(k=1;k<=j-c[i].l;k++){printf("%d L\n",j-la);la++;}la+=lx;}}}return 0;}


阅读全文
0 0
原创粉丝点击