题目传送门

 /*
贪心:按照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. 【Nginx】处理用户请求

    实际处理请求的方法ngx_http_mytest_handler(在配置配置项的回调方法中被调用(用于解析配置项))将接收一个ngx_http_request_t类型的参数,返回一个ngx_int_t ...

  2. 【c++】c++一些基础面试题

    http://www.mianwww.com/html/2013/10/19128.html http://blog.csdn.net/wdzxl198/article/details/9050751 ...

  3. 我所写的CNN框架 VS caffe

    我所写的CNN框架 VS caffe 一个月前.自己模仿caffe实现了一个卷积神经网络的框架. 同样点 1无缝支持CPU和GPU模式,GPU模式使用cuda实现. 不同点 1我的CNN不依赖与不论什 ...

  4. The Pilots Brothers&#39; refrigerator-DFS路径打印

    I - The Pilots Brothers' refrigerator Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format ...

  5. C# MVC 枚举转 SelectListItem

    <span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif; background-color: r ...

  6. Buildroot构建指南--快速上手与实用技巧【转】

    本文转载自:http://blog.csdn.net/zhou_chenz/article/details/52335634 Buildroot官方全英文使用手册的链接是https://buildro ...

  7. 集成到Buildroot中的Helloword程序【转】

    本文转载自:http://www.openloongson.org/forum.php?mod=viewthread&tid=85 本帖最后由 gt945 于 2015-7-5 17:06 编 ...

  8. YTU 2895: H--唱歌的鸟儿

    2895: H--唱歌的鸟儿 时间限制: 1 Sec  内存限制: 128 MB 提交: 26  解决: 10 题目描述 烟大东门有一棵大杨树,树上经常会有很多鸟儿飞来飞去.春天来了,学生物的小姜发现 ...

  9. 还在为AndroidStudio的Gradle版本配置头疼?看看老司机的解决方法吧

    在AndroidStudio中新建项目成功后会自动下载对应版本的Gradle,那么下载的Gradle到什么地方呢? Mac上会默认下载到 /Users/<用户名>/.gradle/wrap ...

  10. android:json解析的两个工具:Gson和Jackson的使用小例子

    1.简介 json是android与服务器通信过程中常用的数据格式,例如,如下是一个json格式的字符串: {"address":"Nanjing"," ...