贪心 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 ...
随机推荐
- hi3531 SDK已编译文件系统制作jffs2文件系统镜像并解决这个问题 .
一, 安装SDK 1.Hi3531 SDK包位置 在"Hi3531_V100R001***/01.software/board"文件夹下,您能够看到一个 Hi3531_SDK_Vx ...
- Visual Studio Code 常用插件
这里记录在Visual Studio Code中,我经常用到的插件. C#这个就不必说了,想用VIsual Studio Code调试和编辑C#代码,必须要装这个插件.东西还好,就是每次安装和更新插件 ...
- [IT学习]Python pandas 学习
今天学习pandas来处理数据,结果用python 3.5.0的shell来调试,总是报错. 报错中包含如下字样: Traceback (most recent call last): File &q ...
- HBase在大搜车金融业务中的应用实践
摘要: 2017云栖大会HBase专场,大搜车高级数据架构师申玉宝带来HBase在大搜车金融业务中的应用实践.本文主要从数据大屏开始谈起,进而分享了GPS风控实践,包括架构.聚集分析等,最后还分享了流 ...
- [转]POI读写Excel 修改
[转]POI读写Excel 修改 一.Excel基础 二.HSSF概况 三.通过usermodel读取文件 四.通过usermodel写入文件 五.通过eventusermodel读取文件 六.HSS ...
- 三角函数补充(反三角函数与 sec)
1. sec=1cos Secant (sec) - Trigonometry function secx=HA
- AutoIT: 通过页面抓取来陈列任务管理器里面所有进程的列表
#include<Array.au3> $handle =WinGetHandle("Windows 任务管理器") ;$ctrl =ControlGetHandle( ...
- DTV 常用功能
AVL/Audio Description(AD SWITCH)/HearingImpaired 文档来自:https://max.book118.com/html/2016/0706/4752022 ...
- appium学习【一】:pycharm运行不生成HtmlTestRunner测试报告
参考文章和解决办法:http://blog.csdn.net/xie_0723/article/details/50825310 http://jingyan.bai ...
- bzoj3251
3251: 树上三角形 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 637 Solved: 262[Submit][Status][Discuss ...