贪心 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 ...
随机推荐
- 初探无线安全审计设备WiFi Pineapple Nano系列之PineAP
前言: 之前曾经介绍过国外无线安全审计设备The WiFi Pineapple Nano的SSLsplit模块和ettercap模块及实验. 在玩WiFi Pineapple Nano 设备的过程中, ...
- 【Nginx】如何使用http配置
处理http配置项可以分为下面4个步骤: 1)创建数据结构用于存储配置项对应的参数 2)设定配置项在nginx.conf中出现时的限制条件与回调方法 3)实现第2步中的回调方法,或者使用Nginx框架 ...
- CV_HAAR_FEATURE_DESC_MAX和CV_HAAR_FEATURE_MAX
#define CV_HAAR_FEATURE_MAX 3 //提前定义的一个宏,在程序中表示一个haar特征由至多三个矩形组成 #define CV_HAAR_FEATURE_DESC_MAX 20 ...
- 用户代码未处理 UpdateException
无法更新 EntitySet"Project_project",由于它有一个 DefiningQuery.而 <ModificationFunctionMapping> ...
- 一个bug在redmine中的诞生到终结
1.測试员測试出bug,跟踪状态为支持,状态为新建,指派给产品经理. 2.产品经理鉴定确觉得bug.改动跟踪状态为bug.指派给技术经理: 3.技术经理收到bug,指派给开发者: 4.开发者收到bug ...
- 自定义的强大的UITableViewCell
UITableView的强大更多程度上来自于可以任意自定义UITableViewCell单元格.通常,UITableView中的Cell是动态的,在使用过程中,会创建一个Cell池,根据每个cell的 ...
- Ubuntu 14.04正式公布,一个不眠之夜
请看下图: 这就是Ubuntu 14.04 LTS桌面版本号的一份视图.感觉既亲切,又寻常,可是,没有什么大的变化.注意:这个Ubuntu桌面版本号要陪伴我们长达5年之久! 直到4月18日(北京时间) ...
- R.layout引用不了布局文件
删除import android.R 引用包所在的R文件..
- SAP事务码 一
SE80 -- edit source code. SE24 -- class create or display. SFP -- created and maintained independent ...
- 7-39 Math对象
7-39 Math对象 学习要点 掌握常用的数学计算方法 温馨提示:关于学习方法的建议 不要强求自己讲参考手册上所以的属性和方法都搞清楚,原因如下: 有些属性和方法非常生僻,很少用,甚至经过一段时间后 ...