UVaLive 6832 Bit String Reordering (模拟)
题意:给定一个01序列,然后让你你最少的操作数把这它变成目标。
析:由于01必须是交替出现的,那么我们就算两次,然后取最值。
代码如下:
- #pragma comment(linker, "/STACK:1024000000,1024000000")
- #include <cstdio>
- #include <string>
- #include <cstdlib>
- #include <cmath>
- #include <iostream>
- #include <cstring>
- #include <set>
- #include <queue>
- #include <algorithm>
- #include <vector>
- #include <map>
- #include <cctype>
- #include <cmath>
- #include <stack>
- #define debug puts("+++++")
- //#include <tr1/unordered_map>
- #define freopenr freopen("in.txt", "r", stdin)
- #define freopenw freopen("out.txt", "w", stdout)
- using namespace std;
- //using namespace std :: tr1;
- typedef long long LL;
- typedef pair<int, int> P;
- const int INF = 0x3f3f3f3f;
- const double inf = 0x3f3f3f3f3f3f;
- const LL LNF = 0x3f3f3f3f3f3f;
- const double PI = acos(-1.0);
- const double eps = 1e-8;
- const int maxn = 2e5 + 5;
- const LL mod = 1e9 + 7;
- const int N = 1e6 + 5;
- const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
- const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
- const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
- inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
- inline int gcd(int a, int b){ return b == 0 ? a : gcd(b, a%b); }
- inline int lcm(int a, int b){ return a * b / gcd(a, b); }
- int n, m;
- const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- inline int Min(int a, int b){ return a < b ? a : b; }
- inline int Max(int a, int b){ return a > b ? a : b; }
- inline LL Min(LL a, LL b){ return a < b ? a : b; }
- inline LL Max(LL a, LL b){ return a > b ? a : b; }
- inline bool is_in(int r, int c){
- return r >= 0 && r < n && c >= 0 && c < m;
- }
- int a[20], b[20];
- int c[20], d[20];
- int solve(){
- int ans = 0;
- memcpy(a, d, sizeof d);
- for(int i = 0; i < n; ++i){
- if(a[i] == c[i]) continue;
- for(int j = i+1; j < n; ++j)
- if(a[j] == c[i]){ ans += j-i; a[j] = !c[i]; break; }
- }
- return ans;
- }
- int main(){
- while(scanf("%d %d", &n, &m) == 2){
- int sum = 0;
- for(int i = 0; i < n; ++i) scanf("%d", a+i), sum += a[i];
- for(int i = 0; i < m; ++i) scanf("%d", b+i);
- memcpy(d, a, sizeof a);
- int cnt = 0, s = 0;
- bool ok = true;
- for(int i = 0; i < m; ++i, ok = !ok)
- for(int j = 0; j < b[i]; ++j)
- c[cnt++] = ok, s += ok;
- int ans = INF;
- if(s == sum) ans = Min(ans, solve());
- s = 0;
- for(int i = 0; i < n; ++i) c[i] = !c[i],s += c[i];
- if(s == sum) ans = Min(ans, solve());
- printf("%d\n", ans);
- }
- return 0;
- }
UVaLive 6832 Bit String Reordering (模拟)的更多相关文章
- 贪心 UVALive 6832 Bit String Reordering
题目传送门 /* 贪心:按照0或1开头,若不符合,选择后面最近的进行交换.然后选取最少的交换次数 */ #include <cstdio> #include <algorithm&g ...
- 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 ...
- csu - 1536: Bit String Reordering (模拟)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1536 不知道为何怎么写都写不对. 这题可以模拟. 虽然题目保证一定可以从原串变成目标串,但是不一定 ...
- 【Bit String Reordering UVALive - 6832 】【模拟】
题意分析 题目讲的主要是给你一个01串,然后给你要变成的01串格式,问你要转换成这一格式最少需要移动的步数. 题目不难,但当时并没有AC,3个小时的个人赛1道没AC,归根到底是没有逼自己去想,又想的太 ...
- UVALive 7325 Book Borders (模拟)
Book Borders 题目链接: http://acm.hust.edu.cn/vjudge/contest/127407#problem/B Description A book is bein ...
- Codeforces Round #550 (Div. 3) E. Median String (模拟)
Median String time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- 2018.08.22 NOIP模拟 string(模拟)
string [描述] 给定两个字符串 s,t,其中 s 只包含小写字母以及*,t 只包含小写字母. 你可以进行任意多次操作,每次选择 s 中的一个*,将它修改为任意多个(可以是 0 个)它的前一个字 ...
- UvaLive 4917 Abstract Extract (模拟)
题意: 给定一篇文章, 文章中有段落, 段落中有句子. 句子只会以'!' , '.' , '?' 结尾, 求出每段中含有与他下面同样是该段落中相同单词数最多的句子, 注意, 单词忽略大小写, 重复的单 ...
- Codeforces Round #550 (Div. 3) E. Median String (思维,模拟)
题意:给你两个字符串\(s\)和\(t\),保证\(t\)的字典序大于\(s\),求他们字典序中间的字符串. 题解:我们假设题目给的不是字符串,而是两个10禁止的正整数,那么输出他们之间的数只要把他两 ...
随机推荐
- 24L01-2.4G无线传输模块调节记录
在调试24L01的时候,虽然能用到别人的程序,但仅仅是程序的初始化,并没有告诉我们如何去后续的操作,如何去再次发送一组数.最近调试24L01接近尾声,将逐一的地方总结下来,以便以后查阅,也供其他人借鉴 ...
- h-ui.admin.pro.iframe头部和标签Tab修改CSS
原效果:头部高度偏高,tab标签不太好看 修改后:缩小高度,调整tab标签css样式 百度网盘链接:https://pan.baidu.com/s/1qknPNAMGL7BFUIsleOF9M ...
- 解决Antimalware Service Executable CPU,内存占用高的问题
1.win键+R键打开运行对话框框,输入gpedit.msc打开本地组策略编辑器(组策略):2.依次打开计算机配置-管理模板-Windows组件-Windows Defender:3.如果要关闭Win ...
- 史上最全Java多线程面试题及答案
多线程有什么用? 线程和进程的区别是什么? Java实现线程有哪几种方式? 启动线程方法start()和run()有什么区别? 怎么终止一个线程?如何优雅地终止线程? 一个线程的生命周期有哪几种状态? ...
- [转] 结构体file_operations
原文地址: http://www.cnblogs.com/sunyubo/archive/2010/12/22/2282079.html 结构体file_operations在头文件 linux/fs ...
- POJ——T 2976 Dropping tests
http://poj.org/problem?id=2976 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13861 ...
- bzoj——3555: [Ctsc2014]企鹅QQ
3555: [Ctsc2014]企鹅QQ Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 2617 Solved: 921[Submit][Statu ...
- spring mvc 访问静态资源404
访问比如css js出现404提示 在spring的配置文件中加上如下代码即可 <!-- 静态资源404 --> <mvc:resources location="/res ...
- TensorFlow-GPU环境配置之四——配置和编译TensorFlow
首先,使用configure进行配置 配置完成后,使用bazel编译retrain命令,编译命令中加入--config=cuda即为启用GPU 编译进行中... 编译完成 编译完成后,调用retrai ...
- 怎样把UCos-ii_在STM32上的移植
下载代码 stm32 标准外设库是 stm32 全系列芯片的外设驱动,有了它能够大大加速我们 开发 stm32. 首先从 st 公司的站点下载最新的 stm32 标准外设库,写本文时最新的版本号是 V ...