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. hls协议(最清晰的讲解)

    今天来介绍一下HLS协议,这个协议是由苹果公司提出并推广开来的.来一段维基百科的定义. HTTP Live Streaming(缩写是HLS)是一个由苹果公司提出的基于HTTP的流媒体网络传输协议.是 ...

  2. Maven-将jar包安装到本地仓库

    因为项目需要,使用的是sqlserver数据库,但是却找不到其对应的pom依赖,所以需要将本地jar包安装到本地仓库,定义pom依赖.以此为例,其他jar包均可参考该方式 cmd命令语句: mvn i ...

  3. 作业3rd

    第三周作业 课本学习 使用nmap扫描特定靶机 使用nessus扫描特定靶机 靶机网络情况如下 在攻击机使用Nessus,步骤如下 新建一个扫描 填入目的主机ip,点击开始进行扫描 等待 扫描结果如下 ...

  4. 洛谷 P2962 [USACO09NOV]灯Lights

    题目描述 Bessie and the cows were playing games in the barn, but the power was reset and the lights were ...

  5. SET_Matplotlib

    fig.tight_layout() 调整子图间距 legend 图例分开显示

  6. POJ2186(有向图缩点)

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 28379   Accepted: 11488 De ...

  7. Windows WMIC命令使用详解2

    Windows WMIC命令使用详解(附实例) https://blog.csdn.net/aflyeaglenku/article/details/77878525 第一次执行WMIC命令时,Win ...

  8. 三台主机搭建LAMP(apache、mariadb、php)

    实验环境:均是CentOS7 httpd:172.16.254.88   2.4.6 PHP:172.16.250.140 5.4.16 mariadb:172.16.250.94 5.5.52 第三 ...

  9. 如何更改linux文件的拥有者及用户…

    本文整理自: http://blog.163.com/yanenshun@126/blog/static/128388169201203011157308/ http://ydlmlh.iteye.c ...

  10. 1. docker安装

    前提 系统:我这边都使用虚拟机安装的CentOS7,具体安装可以参考:Windows安装Linux虚拟机(CentOS7) yum:推荐更新下yum:yum update;我们这边CentOS7自带d ...