CodeForces

来源:互联网 发布:moment.js实现国际化 编辑:程序博客网 时间:2024/06/07 07:33

题目链接:http://codeforces.com/problemset/problem/733/C点击打开链接

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:

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


题意应该很好懂 但是很不好操作。。

我的想法是从左往右对答案序列扫一遍 每次对于当前元素 查看初始序列中是否存在和刚好为当前元素的子串 

如果不存在说明一定达不到

如果存在 判断是否这些子串是否同为一个数且数目大于一 如果是 也达不到

如果不相等 就一定可以达到 这个时候对这个子串单独处理 从寻找最大的元素开始对两边吃 

注意这里可能有3 4 4这种情况 那么只能选择第二个而不能选择第三个 因为第三个没得吃

这个过程比较麻烦 需要判断临界点和特殊情况 并且要将vector不断缩小

最后将过程记录下来输出出来便是

#include <bits/stdc++.h>using namespace std;struct xjy{    char c;    int num;};vector<int > s;vector<int > c;vector< xjy > ans;int main(){    int n,m;    int flag=1;    scanf("%d",&n);    for(int i=1;i<=n;i++)        {            int mid;            cin >> mid;            s.push_back(mid);        }    scanf("%d",&m);    for(int i=1;i<=m;i++)    {        int mid;        cin >> mid;        c.push_back(mid);    }    int nowpoint=0;    int prepoint=0;    for(int i=0;i<c.size();i++)    {        int now=c[i];        int summid=0;        for(;nowpoint<s.size();)        {            if(summid==now)                break;            summid+=s[nowpoint];            nowpoint++;        }        if(summid==now)        {            int maxn=0;            int maxid=0;            vector < int > ss;            set<int > sset;            for(int j=prepoint;j<nowpoint;j++)            {                if(maxn<s[j])                {                    maxn=s[j];                    maxid=j-prepoint;                }                ss.push_back(s[j]);                sset.insert(s[j]);            }            int cnt=sset.size();            if(ss.size()==1)                cnt=2;            if(cnt>1)            {                while(ss.size()!=1)                {                    if(maxid==0)                    {                        if(ss[maxid]>ss[maxid+1])                        {                            xjy mid;                            ss[maxid]+=ss[maxid+1];                            mid.num=maxid+1+i;                            mid.c='R';                            ss.erase(ss.begin()+maxid+1);                            ans.push_back(mid);                        }                        else                            maxid++;                    }                    else if(maxid==(ss.size()-1))                    {                        if(ss[maxid]>ss[maxid-1])                        {                            xjy mid;                            ss[maxid]+=ss[maxid-1];                            mid.num=maxid+1+i;                            mid.c='L';                            ss.erase(ss.begin()+maxid-1);                            maxid--;                            ans.push_back(mid);                        }                        else                            maxid--;                    }                    else                    {                        if(ss[maxid]>ss[maxid+1])                        {                            xjy mid;                            ss[maxid]+=ss[maxid+1];                            mid.num=maxid+1+i;                            mid.c='R';                            ss.erase(ss.begin()+maxid+1);                            ans.push_back(mid);                        }                        else if(ss[maxid]>ss[maxid-1])                        {                            xjy mid;                            ss[maxid]+=ss[maxid-1];                            mid.num=maxid+1+i;                            mid.c='L';                            ss.erase(ss.begin()+maxid-1);                            maxid--;                            ans.push_back(mid);                        }                        else                            maxid++;                    }                }                prepoint=nowpoint;                summid=0;            }            else            {                flag=0;            }        }        else        {            flag=0;        }    }    if(nowpoint!=n)        flag=0;    if(flag)    {        printf("YES\n");        for(int i=0;i<ans.size();i++)            printf("%d %c\n",ans[i].num,ans[i].c);    }    else        printf("NO\n");}



原创粉丝点击