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. HttpClient_02_实现https协议

    二.参考文档 1.Java 和 HTTP 的那些事(四) HTTPS 和 证书 1. 轻松把玩HttpClient之配置ssl,采用设置信任自签名证书实现https 2.如何使用HttpClient来 ...

  2. Go丨语言学习笔记--func

    Java语言跟Go语言的函数比较 Go语言 func funcName(input type1,input type2,......)(output type1,output type2,...... ...

  3. hdu-2169 Computer(树形dp+树的直径)

    题目链接: Computer Time Limit: 1000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) ...

  4. 在Tabbed Activity(ViewPager)中切换Fragment

    我用Android Studio的向导新建了一个Tabbed Activity,里面是ViewPager样式的,有三个tabs.如下: 但是我尝试在第一个tab中设置一个按钮,打开其他tab的时候,却 ...

  5. WebP探索

    不同场景下WebP的使用方案总结:   App:Android4.0以上提供原生支持,其他版本使用官方提供的解析库(Android.iOS):   浏览器:JS能力检测,对支持 WebP 的用户输出 ...

  6. BZOJ1855 [Scoi2010]股票交易[单调队列dp]

    题 题面有点复杂,不概括了. 后面的状态有前面的最优解获得大致方向是dp.先是瞎想了个$f[i][j]$表示第$i$天手里有$j$张股票时最大收入(当天无所谓买不买). 然后写了一个$O(n^4)$状 ...

  7. WPF中Visible设为Collapse时,VisualTreeHelper.GetChildrenCount为0

    今天遇到一个奇怪的问题, 在给一个控件内的子元素绑定事件时,失败. 发现原因是,这个控件初始化时Visible="Collapse",这时控件内的可视树就没有生成.导致绑定事件失败 ...

  8. js 元素的各种宽度高度

    一.属性 1.只读属性 所谓的只读属性指的是DOM节点的固有属性,该属性只能通过js去获取而不能通过js去设置,而且获取的值是只有数字并不带单位的(px,em等),如下: 1)clientWidth和 ...

  9. Javascript Tip(!!)

    var a:var b=!!a; a默认是undefined.!a是true,!!a则是false,所以b的值是false,而不再是undefined,也非其它值,主要是为后续判断提供便利. !!一般 ...

  10. 【机器学习】决策树C4.5、ID3

    一.算法流程 step1:计算信息熵 step2: 划分数据集 step3: 创建决策树 step4: 利用决策树分类 二.信息熵Entropy.信息增益Gain 重点:选择一个属性进行分支.注意信息 ...