C. Epidemic in Monstropolis
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的更多相关文章
- CF733C Epidemic in Monstropolis[模拟 构造 贪心]
C. Epidemic in Monstropolis time limit per test 1 second memory limit per test 256 megabytes input s ...
- 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 ...
- Epidemic in Monstropolis
Epidemic in Monstropolis 题目链接:http://codeforces.com/contest/733/problem/C 贪心 新序列的m个数肯定是由原序列的连续的m个子序列 ...
- 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 ...
- 【16.52%】【codeforces 733C】Epidemic in Monstropolis
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- codeforces733-C. Epidemic in Monstropolis 贪心加链表
题意 现在有一个怪兽序列a[i],权值大的怪兽可以吃权值小的怪兽,吃完之后权值大的怪兽的权值会变成两者权值的和,相邻的怪兽才能吃 吃完之后,位置合并,队列前移,从左到右重新编号,重复这一过程, 然后给 ...
- Codeforces 733C:Epidemic in Monstropolis(暴力贪心)
http://codeforces.com/problemset/problem/733/C 题意:给出一个序列的怪兽体积 ai,怪兽只能吃相邻的怪兽,并且只有体积严格大于相邻的怪兽才能吃,吃完之后, ...
- CodeForces 733C Epidemic in Monstropolis
模拟. 连续的一段$a$合成一个$b$.每段中如果数字只有$1$个,那么可以合成.如果数字个数大于等于$2$个,如果都是一样的,那么无法合成,否则要找到一个可以移动的最大值位置开始移动.一开始写了一个 ...
- 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 ...
随机推荐
- c macro pair
成对使用的macro, 不过也有机会用错, 死都不知道怎么死的, 这宏... #define pthread_cleanup_push(func, val) \ { \ struct __darwin ...
- Linux下安装GB2312的示例
Linux下安装GB2312的示例 Step 1: 到Linux字符集的安装包目录下 [cd /usr/share/i18n/charmaps] Step 2: 解压该目录下的GB2312.gz ...
- HihoCoder1670 : 比赛日程安排([Offer收割]编程练习赛41)(模拟)
描述 H国编程联赛中有N只队伍,编号1~N. 他们计划在2018年一共进行M场一(队)对一(队)的比赛. 为了让参赛队员能得到充分的休息,联赛组委会决定:每支队伍连续两场比赛之间至少间隔一天.也就是如 ...
- 使用ubuntu自带的Remmina Remote Desktop Client远程登录服务器配置
1.配置:点击new , 配置服务器ip地址.名称.密码 2.打开本机终端执行一下命令: echo xfce4-session>.session echo xfce4-session>.x ...
- DB2 Error Messages (Sorted by SQLCODE)
DB2 Error Messages (Sorted by SQLCODE) DB2 Error Messages (Sorted by SQLCODE) SQLCODE SQLSTATE Descr ...
- 查看MySql数据库物理文件存放位置
查找数据库文件位置使用命令 show global variables like "%datadir%";
- [CentOS] 结合Nginx部署DotNetCore的demo项目【转载】
部署前准备 1.VisualStudio2017+.netcore2.0SDK 2.Centos7.2 3.SecureCRT,Xftp(根据自己喜好) 创建WebApi项目 修改Program.cs ...
- In-App Purchase Programming Guide----(八) ---- Preparing for App Review
Preparing for App Review After you finish testing, you’re ready to submit your app for review. This ...
- VNC协议分析
VNC协议分析 摘自: http://blog.csdn.net/forever_feng/article/details/4703088 简介 VNC(Virtual Network Computi ...
- python中出现 IndentationError:unindent does not match any outer indentation level
python中出现IndentationError:unindent does not match any outer indentation level 今天在网上copy的一段代码,代码很简单,每 ...