题意:给定一个01序列,然后让你你最少的操作数把这它变成目标。

析:由于01必须是交替出现的,那么我们就算两次,然后取最值。

代码如下:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <cctype>
  14. #include <cmath>
  15. #include <stack>
  16. #define debug puts("+++++")
  17. //#include <tr1/unordered_map>
  18. #define freopenr freopen("in.txt", "r", stdin)
  19. #define freopenw freopen("out.txt", "w", stdout)
  20. using namespace std;
  21. //using namespace std :: tr1;
  22.  
  23. typedef long long LL;
  24. typedef pair<int, int> P;
  25. const int INF = 0x3f3f3f3f;
  26. const double inf = 0x3f3f3f3f3f3f;
  27. const LL LNF = 0x3f3f3f3f3f3f;
  28. const double PI = acos(-1.0);
  29. const double eps = 1e-8;
  30. const int maxn = 2e5 + 5;
  31. const LL mod = 1e9 + 7;
  32. const int N = 1e6 + 5;
  33. const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
  34. const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
  35. const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
  36. inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
  37. inline int gcd(int a, int b){ return b == 0 ? a : gcd(b, a%b); }
  38. inline int lcm(int a, int b){ return a * b / gcd(a, b); }
  39. int n, m;
  40. const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  41. const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  42. inline int Min(int a, int b){ return a < b ? a : b; }
  43. inline int Max(int a, int b){ return a > b ? a : b; }
  44. inline LL Min(LL a, LL b){ return a < b ? a : b; }
  45. inline LL Max(LL a, LL b){ return a > b ? a : b; }
  46. inline bool is_in(int r, int c){
  47. return r >= 0 && r < n && c >= 0 && c < m;
  48. }
  49. int a[20], b[20];
  50. int c[20], d[20];
  51.  
  52. int solve(){
  53. int ans = 0;
  54. memcpy(a, d, sizeof d);
  55. for(int i = 0; i < n; ++i){
  56. if(a[i] == c[i]) continue;
  57. for(int j = i+1; j < n; ++j)
  58. if(a[j] == c[i]){ ans += j-i; a[j] = !c[i]; break; }
  59. }
  60. return ans;
  61. }
  62.  
  63. int main(){
  64. while(scanf("%d %d", &n, &m) == 2){
  65. int sum = 0;
  66. for(int i = 0; i < n; ++i) scanf("%d", a+i), sum += a[i];
  67. for(int i = 0; i < m; ++i) scanf("%d", b+i);
  68. memcpy(d, a, sizeof a);
  69. int cnt = 0, s = 0;
  70. bool ok = true;
  71. for(int i = 0; i < m; ++i, ok = !ok)
  72. for(int j = 0; j < b[i]; ++j)
  73. c[cnt++] = ok, s += ok;
  74. int ans = INF;
  75. if(s == sum) ans = Min(ans, solve());
  76. s = 0;
  77. for(int i = 0; i < n; ++i) c[i] = !c[i],s += c[i];
  78. if(s == sum) ans = Min(ans, solve());
  79. printf("%d\n", ans);
  80. }
  81. return 0;
  82. }

UVaLive 6832 Bit String Reordering (模拟)的更多相关文章

  1. 贪心 UVALive 6832 Bit String Reordering

    题目传送门 /* 贪心:按照0或1开头,若不符合,选择后面最近的进行交换.然后选取最少的交换次数 */ #include <cstdio> #include <algorithm&g ...

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

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

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

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

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

  5. UVALive 7325 Book Borders (模拟)

    Book Borders 题目链接: http://acm.hust.edu.cn/vjudge/contest/127407#problem/B Description A book is bein ...

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

  7. 2018.08.22 NOIP模拟 string(模拟)

    string [描述] 给定两个字符串 s,t,其中 s 只包含小写字母以及*,t 只包含小写字母. 你可以进行任意多次操作,每次选择 s 中的一个*,将它修改为任意多个(可以是 0 个)它的前一个字 ...

  8. UvaLive 4917 Abstract Extract (模拟)

    题意: 给定一篇文章, 文章中有段落, 段落中有句子. 句子只会以'!' , '.' , '?' 结尾, 求出每段中含有与他下面同样是该段落中相同单词数最多的句子, 注意, 单词忽略大小写, 重复的单 ...

  9. Codeforces Round #550 (Div. 3) E. Median String (思维,模拟)

    题意:给你两个字符串\(s\)和\(t\),保证\(t\)的字典序大于\(s\),求他们字典序中间的字符串. 题解:我们假设题目给的不是字符串,而是两个10禁止的正整数,那么输出他们之间的数只要把他两 ...

随机推荐

  1. 24L01-2.4G无线传输模块调节记录

    在调试24L01的时候,虽然能用到别人的程序,但仅仅是程序的初始化,并没有告诉我们如何去后续的操作,如何去再次发送一组数.最近调试24L01接近尾声,将逐一的地方总结下来,以便以后查阅,也供其他人借鉴 ...

  2. h-ui.admin.pro.iframe头部和标签Tab修改CSS

    原效果:头部高度偏高,tab标签不太好看 ​ 修改后:缩小高度,调整tab标签css样式 ​ 百度网盘链接:https://pan.baidu.com/s/1qknPNAMGL7BFUIsleOF9M ...

  3. 解决Antimalware Service Executable CPU,内存占用高的问题

    1.win键+R键打开运行对话框框,输入gpedit.msc打开本地组策略编辑器(组策略):2.依次打开计算机配置-管理模板-Windows组件-Windows Defender:3.如果要关闭Win ...

  4. 史上最全Java多线程面试题及答案

    多线程有什么用? 线程和进程的区别是什么? Java实现线程有哪几种方式? 启动线程方法start()和run()有什么区别? 怎么终止一个线程?如何优雅地终止线程? 一个线程的生命周期有哪几种状态? ...

  5. [转] 结构体file_operations

    原文地址: http://www.cnblogs.com/sunyubo/archive/2010/12/22/2282079.html 结构体file_operations在头文件 linux/fs ...

  6. POJ——T 2976 Dropping tests

    http://poj.org/problem?id=2976 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13861   ...

  7. bzoj——3555: [Ctsc2014]企鹅QQ

    3555: [Ctsc2014]企鹅QQ Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 2617  Solved: 921[Submit][Statu ...

  8. spring mvc 访问静态资源404

    访问比如css js出现404提示 在spring的配置文件中加上如下代码即可 <!-- 静态资源404 --> <mvc:resources location="/res ...

  9. TensorFlow-GPU环境配置之四——配置和编译TensorFlow

    首先,使用configure进行配置 配置完成后,使用bazel编译retrain命令,编译命令中加入--config=cuda即为启用GPU 编译进行中... 编译完成 编译完成后,调用retrai ...

  10. 怎样把UCos-ii_在STM32上的移植

    下载代码 stm32 标准外设库是 stm32 全系列芯片的外设驱动,有了它能够大大加速我们 开发 stm32. 首先从 st 公司的站点下载最新的 stm32 标准外设库,写本文时最新的版本号是 V ...