题目传送门

 /*
贪心:按照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的更多相关文章

  1. 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 ...

  2. UVaLive 6832 Bit String Reordering (模拟)

    题意:给定一个01序列,然后让你你最少的操作数把这它变成目标. 析:由于01必须是交替出现的,那么我们就算两次,然后取最值. 代码如下: #pragma comment(linker, "/ ...

  3. 【Bit String Reordering UVALive - 6832 】【模拟】

    题意分析 题目讲的主要是给你一个01串,然后给你要变成的01串格式,问你要转换成这一格式最少需要移动的步数. 题目不难,但当时并没有AC,3个小时的个人赛1道没AC,归根到底是没有逼自己去想,又想的太 ...

  4. 贪心 UVALive 6834 Shopping

    题目传送门 /* 题意:有n个商店排成一条直线,有一些商店有先后顺序,问从0出发走到n+1最少的步数 贪心:对于区间被覆盖的点只进行一次计算,还有那些要往回走的区间步数*2,再加上原来最少要走n+1步 ...

  5. UVaLive 7637 Balanced String (构造)

    题意:给定一个括号的序列,原先的序列是碰到左括号加1,碰到右括号减1,然后把序列打乱,让你找出字典序最小的一个答案. 析:直接从第一个括号判断就好了,优先判断左括号,如果不行就加右括号. 代码如下: ...

  6. csu - 1536: Bit String Reordering (模拟)

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1536 不知道为何怎么写都写不对. 这题可以模拟. 虽然题目保证一定可以从原串变成目标串,但是不一定 ...

  7. LeetCode - 767. Reorganize String

    Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...

  8. 训练报告 (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 ...

  9. ACM 删数问题 SDUT 2072

    http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Index/problemdetail/pid/2072.html 删数问题 Time Limit ...

随机推荐

  1. 数据库(Mysql)背后的数据结构-学习

    来吧,用这三篇文章夯实对Mysql的理解吧. 关于数据库索引及其优化,更多可参见此文:http://www.cnblogs.com/pkuoliver/archive/2011/08/17/mass- ...

  2. [PythonCode]扫描局域网的alive ip地址

    内网的主机都是自己主动分配ip地址,有时候须要查看下有那些ip在使用,就写了个简单的脚本. linux和windows下都能够用,用多线程来ping1-255全部的地址,效率不高.2分钟左右. 先凑合 ...

  3. 【转】面向切面编程-AOP,挺有用的

    原文:http://blog.csdn.net/yangzhihello/article/details/40377399 下面的代码有点问题, ins[methodName] = function( ...

  4. VS自己定义project向导开发(Custom Wizard)

     在vs2010中定制VC++project向导有例如以下两种方式: 改动现有向导.以满足自己的须要: 使用"自己定义向导"来产生一个向导骨架,并设计自己的向导. 本文档仅仅讨 ...

  5. 自己定义验证器——用Struts2框架以框架师的思维灵活做好该事情

    面对的问题:自己定义一个18位身份验证器.编写验证器.在validators.xml文件里进行注冊.在验证配置文件里使用? 第一部分:理解Struts2中自带的验证器 第二部分:如何通过server( ...

  6. android通过adb wireless的使用

    转自:http://www.cnblogs.com/Androider123/p/3848415.html?utm_source=tuicool 开发android程序,总是需要插拔插拔的,usb口都 ...

  7. c中的变量

    1 变量类型 1.1 static global or static .data/.bss 1.2 automic stack,its relevant to os kernel and compil ...

  8. Delphi全角转半角

    function ToDBC( input :String):WideString;varc:WideString;i:Integer;beginc := input;for i:=1 to Leng ...

  9. YTU 2906: 多重继承 日期与时间

    2906: 多重继承 日期与时间 时间限制: 1 Sec  内存限制: 128 MB 提交: 240  解决: 190 题目描述 在空缺的地方补全代码,只需提交补全部分即可 请用C++方式提交 #in ...

  10. web项目开发 之 前端规范 --- HTML编码规范

    此文严格按照W3C规范和部分实际项目可读性,浏览器加载,性能等众多属性权衡,做出平时前端编码规范文 档.供广大web工作者参考并实施,对维护和项目扩展升级都能省时省力. 转载请注明出处,JS前端实用开 ...