题意:给定一个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 (模拟)的更多相关文章

  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. 06-看图理解数据结构与算法系列(AVL树)

    AVL树 AVL树,也称平衡二叉搜索树,AVL是其发明者姓名简写.AVL树属于树的一种,而且它也是一棵二叉搜索树,不同的是他通过一定机制能保证二叉搜索树的平衡,平衡的二叉搜索树的查询效率更高. AVL ...

  2. 关于OPENSSL的EVP函数的使用

    4月份没什么做,就是做了OPENSSL的 加密和解密的应用,现在公开一下如何调用OPENSSL对字符串进行加密和解密,当中也学会了对加密数据进行BASE64编码,现在公开一下代码,在这感谢GITHUB ...

  3. Automation 的 Wait 工具

    public static WebDriverWait createWait(WebDriver driver) { return new WebDriverWait(driver, Environm ...

  4. [luoguP3390]【模板】矩阵快速幂

    传送门 模板不解释. ——代码 #include <cstdio> #include <cstring> #define LL long long int n; LL k; ; ...

  5. Flask基础(3):session、flash、特殊装饰器、蓝图、路由正则匹配、上下文管理 & flask-session

    Session: Flask 默认将 session 以加密的形式放到了浏览器的 cookie 中 Flask 的 session 就是一个字典,字典有什么方法 session 就有什么方法 flas ...

  6. VS调试STL问题总结

    ---恢复内容开始--- 以前写代码总觉用自己写的东西比较牛逼,vector?stack?为什么不自己实现.后来才认识到这是个幼稚的想法!首先每次都自己实现是一种重复劳动:其次,自己写的话很难保证没有 ...

  7. ssh远程登录

    ssh root@192.168.124.128 密钥登录: 1).ssh-keygen 生成公钥和私钥 [root@rhel5 ~]# ssh-keygen -t rsa Generating pu ...

  8. Ubuntu12.04之修改密码

    Ubuntu 12.04 默认root没有密码 修改密码方式如下: test@localhost:~$ sudo passwd root [sudo] password for test: 输入新的 ...

  9. Servlet开发(3)

    Servlet开发 Servlet过滤器: 主要是对访问主页的用户进行过滤,通过登录系统进入的用户可以看到主页内容,在session中存在currentuser. 可以对此进行判断: package ...

  10. the apple code

    i know you will forget but 9 you will