【16.52%】【codeforces 733C】Epidemic in Monstropolis
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
【题解】
贪心.
最后的目标序列是按顺序给的;
则我们要达到的目标也应该是顺序的;
则我们枚举当前要达到哪一个目标i(i=1..k)
然后从在a[i..j]里面找一个最大值mx;其中a[i]+a[i+1]+..+a[j]==bi;
就一直用这个最大值往左往右吃(因为是最大的,所以i..j这个范围内只要再吃一只就全都能吃了,当然如果a[i]..a[j]全都是相同的值则无解);
吃掉之后把数组整个左移就好;
数据的第13个点坑的是如果最后的序列两个序列的长度不同也是算作无解的;
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#define lson L,m,rt<<1
#define rson m+1,R,rt<<1|1
#define LL long long
using namespace std;
const int MAXN = 600;
const int dx[5] = {0,1,-1,0,0};
const int dy[5] = {0,0,0,-1,1};
const double pi = acos(-1.0);
int n,a[MAXN],k,b[MAXN];
vector < pair<int,char> > v;
void input_LL(LL &r)
{
r = 0;
char t = getchar();
while (!isdigit(t) && t!='-') t = getchar();
LL sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
void input_int(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)&&t!='-') t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
void TAT()
{
puts("NO");
exit(0);
}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
input_int(n);
for (int i = 1;i <= n;i++)
input_int(a[i]);
input_int(k);
for (int i = 1;i <= k;i++)
input_int(b[i]);
for (int i = 1;i <= k;i++)
{
while (a[i]!=b[i])
{
if (a[i] > b[i])
TAT();
int sum = 0,mx = 0;
int j = i;
while (j <= n && sum < b[i])
mx = max(mx,a[j]),sum+=a[j],j++;
bool flag = 0;
for (int l = i;l<=n;l++)
if (a[l]==mx && l-1>=i && a[l-1]!=mx)
{
flag = 1;
a[l-1]+=a[l];
v.push_back(make_pair(l,'L'));
for (int t = l;t<=n-1;t++)
a[t] = a[t+1];
n--;
break;
}
else
if (a[l]==mx && (l+1)<=n && a[l+1]!=mx)
{
flag = 1;
a[l]+=a[l+1];
v.push_back(make_pair(l,'R'));
for (int t = l+1;t<=n-1;t++)
a[t]=a[t+1];
n--;
break;
}
if (!flag)
TAT();
}
}
if (n!=k)
TAT();
puts("YES");
int len = v.size();
for (int i = 0;i <= len-1;i++)
printf("%d %c\n",v[i].first,v[i].second);
return 0;
}
【16.52%】【codeforces 733C】Epidemic in Monstropolis的更多相关文章
- Codeforces 733C:Epidemic in Monstropolis(暴力贪心)
http://codeforces.com/problemset/problem/733/C 题意:给出一个序列的怪兽体积 ai,怪兽只能吃相邻的怪兽,并且只有体积严格大于相邻的怪兽才能吃,吃完之后, ...
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- 【52.49%】【codeforces 556A】Case of the Zeros and Ones
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【16.23%】【codeforces 586C】Gennady the Dentist
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 807D】Dynamic Problem Scoring
[题目链接]:http://codeforces.com/contest/807/problem/D [题意] 给出n个人的比赛信息; 5道题 每道题,或是没被解决->用-1表示; 或者给出解题 ...
- 【codeforces 821E】Okabe and El Psy Kongroo
[题目链接]:http://codeforces.com/problemset/problem/821/E [题意] 一开始位于(0,0)的位置; 然后你每次可以往右上,右,右下3走一步; (x+1, ...
- 【81.82%】【codeforces 740B】Alyona and flowers
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【23.39%】【codeforces 558C】Amr and Chemistry
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【42.59%】【codeforces 602A】Two Bases
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
随机推荐
- 以流的形式接收Http请求
IEnumerable<ReportObject> IHttpReceiveReport.OnPost(System.Web.HttpContextBase context) { //[{ ...
- android studio 一次编译错误:Error:Minimum supported Gradle version is 2.14.1.
因为需要,今天从git上重新下载工程到另一个目录下,结果运行的时候报了这个错:Error:Minimum supported Gradle version is 2.14.1. Current ve ...
- 【CS Round #48 (Div. 2 only)】Dominant Free Sets
[链接]h在这里写链接 [题意] 让你在n个点组成的集合里面选取不为空的集合s. 使得这里面的点没有出现某个点a和b,ax>=bx且ay>=by; 问你s的个数. [题解] 我们把这些点按 ...
- 从零开始使用git第二篇:git的日常操作
从零开始使用git 第二篇:git的日常操作 第一篇:从零开始使用git第一篇:下载安装配置 第二篇:从零开始使用git第二篇:git实践操作 第三篇:从零开始使用git第三篇:git撤销操作.分支操 ...
- OC学习篇之---Foundation框架中的其他类(NSNumber,NSDate,NSExcetion)
1.NSNumber 这个类主要是用来封装基本类型的,说到这里,就不得不说一下了: OC中的集合是不允许存入基本类型的,所以NSNumber类就诞生了,需要将基本类型封装一下,然后存进去,这个类似于J ...
- C语言主要做哪些方面的开发---一个来自“IT技术学习”微信群的问题及答复
近期,在"IT技术学习"微信群中,有同学问了这样一个问题:C语言主要做哪些方面的开发?在这篇文章中,我想结合自身的经验,对这个问题进行下解答. C语言是计算机及其相关专业(如通信. ...
- Ubuntu12.04.4 Vmware 虚拟机安装总结
Ubuntu12.04.4 Vmware 虚拟机安装总结 背景:近期准备入手一块树莓派(RaspberryPI),准备一下开发环境,可惜机器硬盘小,又舍不得格调Win7,所以仅仅好装虚拟机了.考虑到对 ...
- python类继承时构造函数的运行问题
假设子类定义了自己的__init__构造方法函数.当子类的实例对象被创建时,子类仅仅会运行自己的__init__方法函数.假设子类没有定义自己的构造方法函数.会沿着搜索树找到父类的构造方法函数去运行父 ...
- iOS Universal Static Framework 手动转 XCode Cocoa Framework
不须要又一次创建Project,手动改动project设置. 第一步:在Project文件里,改动type,去掉static 1. 搜索wrapper.framework.static,去掉stati ...
- angular的学习参考材料
原文地址:https://www.jianshu.com/p/b9db7bb3d4ec 目的 其实写这篇文章的主要目的是为了提供给那些刚刚入门angular或者有意学习angular的读者准备的. 我 ...