http://codeforces.com/contest/733/problem/C

一道很恶心的模拟题。

注意到如果能凑成b[1],那么a的前缀和一定是有一个满足是b[1]的,因为,如果跳过了一些前面的数不用,就会剩下一个多余的东西在哪里。所以就是把a数组分成了若干段,判断每一段是否凑成b[i]了,

能凑成b[i]的条件是:

这一段a[]中,最大值的左边或者右边要有一个比它小,然后吃了它,就能吃全部了。

注意当只有1个的时候,是一定是true的。不用吃其他了,

剩下的就是恶心的模拟了,因为它的下标不断变换。

所以我就用了一个vector去存。

删除vector中的元素要用迭代器。

复杂度是O(n)的,

总体复杂度O(n*n)

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = + ;
int a[maxn];
int b[maxn];
int n, k;
vector<int>arr;
struct node {
int pos;
char ch;
node() {}
node(int a, char b) : pos(a), ch(b) {}
}show[maxn];
int lenshow;
bool slove(int begin, int end, int how) {
if (begin == end) return true;
int mx = -inf;
arr.clear();
for (int i = ; i <= how; ++i) arr.push_back(inf);
for (int i = begin; i <= end; ++i) {
mx = max(mx, a[i]);
}
int pos = inf;
for (int i = begin; i <= end; ++i) {
if (a[i] == mx) {
if (i > begin && mx > a[i - ]) {
pos = i - begin + ;
break;
}
if (i < end && mx > a[i + ]) {
pos = i - begin + ;
break;
}
}
}
if (pos == inf) return false; for (int i = begin; i <= end; ++i) arr.push_back(a[i]);
vector<int> :: iterator it = arr.begin() + pos + how - ;
vector<int> :: iterator it2; int all = end - begin + ;
pos = pos + how - ;
int tbegin = how;
int tend = arr.size() - ; while (true) {
if (pos > tbegin && arr[pos] > arr[pos - ]) {
show[++lenshow] = node(pos, 'L');
arr[pos - ] += arr[pos];
it2 = it;
arr.erase(it2);
it--;
// cout << *it << endl;
pos--;
all--;
tend = arr.size() - ;
if (all == ) return true;
}
if (pos < tend && arr[pos] > arr[pos + ]) {
show[++lenshow] = node(pos, 'R');
arr[pos + ] += arr[pos];
it2 = it;
arr.erase(it2);
// cout << *it << endl;
// it = arr.begin() + how + pos - 1;
tend = arr.size() - ;
all--;
if (all == ) return true;
}
}
return true;
}
void work() {
scanf("%d", &n);
for (int i = ; i <= n; ++i) {
scanf("%d", &a[i]);
}
scanf("%d", &k);
for (int i = ; i <= k; ++i) {
scanf("%d", &b[i]);
}
int how = ;
int begin = ;
int up = ;
for (int i = ; i <= k; ++i) {
int total = ;
bool flag = false;
int tbegin = begin;
while (begin <= n) {
total += a[begin];
if (total == b[i]) {
flag = slove(tbegin, begin, how);
up = begin;
how++;
begin++;
break;
}
begin++;
if (total > b[i]) {
printf("NO\n");
return;
}
}
if (flag == false) {
printf("NO\n");
return;
}
}
if (up != n) {
cout << "NO" << endl;
return;
}
cout << "YES" << endl;
for (int i = ; i <= lenshow; ++i) {
printf("%d %c\n", show[i].pos, show[i].ch);
}
} int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
work();
return ;
}

C. Epidemic in Monstropolis的更多相关文章

  1. CF733C Epidemic in Monstropolis[模拟 构造 贪心]

    C. Epidemic in Monstropolis time limit per test 1 second memory limit per test 256 megabytes input s ...

  2. Codeforces Round #378 (Div. 2) C. Epidemic in Monstropolis 模拟

    C. Epidemic in Monstropolis time limit per test 1 second memory limit per test 256 megabytes input s ...

  3. Epidemic in Monstropolis

    Epidemic in Monstropolis 题目链接:http://codeforces.com/contest/733/problem/C 贪心 新序列的m个数肯定是由原序列的连续的m个子序列 ...

  4. Codeforces Round #378 (Div. 2)-C. Epidemic in Monstropolis

    C. Epidemic in Monstropolis time limit per test 1 second memory limit per test 256 megabytes input s ...

  5. 【16.52%】【codeforces 733C】Epidemic in Monstropolis

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. codeforces733-C. Epidemic in Monstropolis 贪心加链表

    题意 现在有一个怪兽序列a[i],权值大的怪兽可以吃权值小的怪兽,吃完之后权值大的怪兽的权值会变成两者权值的和,相邻的怪兽才能吃 吃完之后,位置合并,队列前移,从左到右重新编号,重复这一过程, 然后给 ...

  7. Codeforces 733C:Epidemic in Monstropolis(暴力贪心)

    http://codeforces.com/problemset/problem/733/C 题意:给出一个序列的怪兽体积 ai,怪兽只能吃相邻的怪兽,并且只有体积严格大于相邻的怪兽才能吃,吃完之后, ...

  8. CodeForces 733C Epidemic in Monstropolis

    模拟. 连续的一段$a$合成一个$b$.每段中如果数字只有$1$个,那么可以合成.如果数字个数大于等于$2$个,如果都是一样的,那么无法合成,否则要找到一个可以移动的最大值位置开始移动.一开始写了一个 ...

  9. Codeforces Round #378 (Div. 2) A B C D 施工中

    A. Grasshopper And the String time limit per test 1 second memory limit per test 256 megabytes input ...

随机推荐

  1. asteris录音设置

    [macro-recording] exten =>s,1,Set(CALLFILENAME=${STRFTIME(${EPOCH},UTC-8,%Y-%m-%d-%H-%M-%S)}-${CA ...

  2. bzoj 4514: 数字配对

    题目大意 自己看 题解 我们打表观察规律发现一定能构成一张二分图 也就是不存在奇环 所以我们一般保证费用非负的最大流即可. #include <cstdio> #include <c ...

  3. poj2955——括号匹配

    题目:http://poj.org/problem?id=2955 区间DP. 代码如下: #include<iostream> #include<cstdio> #inclu ...

  4. 百度地图API的第一次接触——地图事件

    0.初始化地图 var map = new BMap.Map("container"); var point = new BMap.Point(116.404, 39.915); ...

  5. 百度地图API的第一次接触——自定义控件

    1.定义一个控件类,即function function ZoomControl(){ // 设置默认停靠位置和偏移量 this.defaultAnchor = BMAP_ANCHOR_TOP_LEF ...

  6. Vijos:P1234口袋的天空

    背景 小杉坐在教室里,透过口袋一样的窗户看口袋一样的天空. 有很多云飘在那里,看起来很漂亮,小杉想摘下那样美的几朵云,做成棉花糖. 描述 给你云朵的个数N,再给你M个关系,表示哪些云朵可以连在一起. ...

  7. Lagom学习 五 Hello world工程

    用Maven创建一个Hello world的Lagom工程: 1: 在想创建工程的目下下,打开CMD 2:  mvn archetype:generate -Dfilter=com.lightbend ...

  8. 怎么在docker容器的mysql的编码格式变为utf8

    第一个方法: 1.  编辑MySql的配置文件 MySql的配置文件Windows下一般在系统目录下或者在MySql的安装目录下名字叫my.ini,可以搜索,Linux下一般是/etc/my.cnf ...

  9. C#自定义控件 类似于Linechart

    界面效果: 对外提供的属性设置 /// <summary> /// 背景色 /// </summary> public Color BackColor; /// <sum ...

  10. 02_SQliteOpenHelper介绍&oncreate方法介绍

    file:///D:/BaiduNetdiskDownload/adt-bundle-windows-x86_64_20140101/adt-bundle-windows-x86_64_2014010 ...