贪心 UVALive 6832 Bit String Reordering
- /*
- 贪心:按照0或1开头,若不符合,选择后面最近的进行交换。然后选取最少的交换次数
- */
- #include <cstdio>
- #include <algorithm>
- #include <cstring>
- #include <string>
- #include <cmath>
- #include <vector>
- #include <map>
- #include <queue>
- using namespace std;
- const int MAXN = + ;
- const int INF = 0x3f3f3f3f;
- int a[MAXN], b[MAXN], c[MAXN];
- int main(void) //UVALive 6832 Bit String Reordering
- {
- // freopen ("A.in", "r", stdin);
- int n, m;
- while (scanf ("%d%d", &n, &m) == )
- {
- for (int i=; i<=n; ++i) {scanf ("%d", &a[i]); c[i] = a[i];}
- for (int i=; i<=m; ++i) scanf ("%d", &b[i]);
- int cnt1 = , cnt2 = ; int now = ; int p = ;
- bool ok1 = true, ok2 = true;
- for (int i=; i<=m && ok1; ++i)
- {
- for (int j=; j<=b[i]; ++j)
- {
- if (a[p+j] != now)
- {
- int k = p + j;
- while (k <= n && a[k] != now) k++;
- if (k == n+ || a[k] != now) {ok1 = false; break;}
- cnt1 += k - (p + j);
- swap (a[k], a[p+j]);
- }
- }
- p += b[i]; now = - now;
- }
- now = ; p = ;
- for (int i=; i<=m && ok2; ++i)
- {
- for (int j=; j<=b[i]; ++j)
- {
- if (c[p+j] != now)
- {
- int k = p + j;
- while (k <= n && c[k] != now) k++;
- if (k == n+ || c[k] != now) {ok2 = false; break;}
- cnt2 += k - (p + j);
- swap (c[p+j], c[k]);
- }
- }
- p += b[i]; now = - now;
- }
- // printf ("%d %d\n", cnt1, cnt2);
- if (!ok1) printf ("%d\n", cnt2);
- else if (!ok2) printf ("%d\n", cnt1);
- else printf ("%d\n", min (cnt1, cnt2));
- }
- return ;
- }
贪心 UVALive 6832 Bit String Reordering的更多相关文章
- coderforces Gym 100803A/Aizu 1345/CSU 1536/UVALive 6832 Bit String Reordering(贪心证明缺)
Portal: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1345 http://codeforces.com/gym/100 ...
- UVaLive 6832 Bit String Reordering (模拟)
题意:给定一个01序列,然后让你你最少的操作数把这它变成目标. 析:由于01必须是交替出现的,那么我们就算两次,然后取最值. 代码如下: #pragma comment(linker, "/ ...
- 【Bit String Reordering UVALive - 6832 】【模拟】
题意分析 题目讲的主要是给你一个01串,然后给你要变成的01串格式,问你要转换成这一格式最少需要移动的步数. 题目不难,但当时并没有AC,3个小时的个人赛1道没AC,归根到底是没有逼自己去想,又想的太 ...
- 贪心 UVALive 6834 Shopping
题目传送门 /* 题意:有n个商店排成一条直线,有一些商店有先后顺序,问从0出发走到n+1最少的步数 贪心:对于区间被覆盖的点只进行一次计算,还有那些要往回走的区间步数*2,再加上原来最少要走n+1步 ...
- UVaLive 7637 Balanced String (构造)
题意:给定一个括号的序列,原先的序列是碰到左括号加1,碰到右括号减1,然后把序列打乱,让你找出字典序最小的一个答案. 析:直接从第一个括号判断就好了,优先判断左括号,如果不行就加右括号. 代码如下: ...
- csu - 1536: Bit String Reordering (模拟)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1536 不知道为何怎么写都写不对. 这题可以模拟. 虽然题目保证一定可以从原串变成目标串,但是不一定 ...
- LeetCode - 767. Reorganize String
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...
- 训练报告 (2014-2015) 2014, Samara SAU ACM ICPC Quarterfinal Qualification Contest
Solved A Gym 100488A Yet Another Goat in the Garden B Gym 100488B Impossible to Guess Solved C Gym ...
- ACM 删数问题 SDUT 2072
http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Index/problemdetail/pid/2072.html 删数问题 Time Limit ...
随机推荐
- Deepin-安装laravel
首先获取到composer.phar wget https://getcomposer.org/download/1.6.3/composer.phar 下载以后移动到目标区域 sudo mv com ...
- FZU 2150 Fire Game (暴力BFS)
[题目链接]click here~~ [题目大意]: 两个熊孩子要把一个正方形上的草都给烧掉,他俩同一时候放火烧.烧第一块的时候是不花时间的.每一块着火的都能够在下一秒烧向上下左右四块#代表草地,.代 ...
- (void __user *)arg 中__user的作用
__user宏简单告诉编译器(通过 noderef)不应该解除这个指针的引用(因为在当前地址空间中它是没有意义的). (void __user *)arg 指的是arg值是一个用户空间的地址,不能直接 ...
- 关于axis2.1.6与websphere7的包冲突问题的解决方式
1,复制axis2.1.6内的module目录内的全部文件到lib 并改动扩展名为.jar 2,删除module目录(可选,不删除也能够) 3,部署到was 4,设置was相应应用程序的类载入方案为父 ...
- 2016/4/1 PDO:: 数据访问抽象层 ? :
①PDO方式连接 数据库 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...
- call function
1 call递归扩展变量 本质上仍然是变量扩展,等价于$(),只不过扩展的时候带了参数,$(call xxx)返回的是xxx扩展之后的值.参数依次赋值给$(1),$(2)......,但是参数要在赋值 ...
- js实现加密(?!)
<script src="yourUrl/md5.min.js"></script> 或者: <script src="http://cdn ...
- HTTP要点概述:四,HTTP方法
使用HTTP协议的时候,客户端可以通过HTTP方法告知服务器自己请求的意图. 看了这篇文章以后,谁再说HTTP方法只有GET和POST,你的眼睛是用来吃饭的嘛! 一,GET:获取资源 GET用来请求访 ...
- XMU 1606 nc与滴水问题 【模拟】
1606: nc与滴水问题 Time Limit: 1000 MS Memory Limit: 64 MBSubmit: 85 Solved: 27[Submit][Status][Web Boa ...
- (转载)synchronized代码块
原文:http://blog.csdn.net/luoweifu/article/details/46613015 作者:luoweifu 转载请标名出处 <编程思想之多线程与多进程(1)——以 ...