【codeforces 779A】Pupils Redistribution
【题目链接】:http://codeforces.com/contest/779/problem/A
【题意】
让你把两个组的5个人的数目都变成一样的.
支持交换操作;
问你最少需要交换几次.
【题解】
/*
哪个少了,就往对面的多的换一下;
模拟一下写个贪心就好
A:num[1..5]
B:num[1..5]
rep1(i,1,5)
{
if (bnum[i]>anum[i])
{
rep1(j,i,5)
swap(anum[j],bnum[j]);
}
if (bnum[i]<anum[i])
{
if ((bnum[i]+anum[i])&1)
return puts("-1"),0;
while (bnum[i]<anum[i])
{
int j = -1;
rep1(k,i+1,5)
if (bnum[k]>anum[k] && (anum[k]+bnum[k])%2==0)
{
j = k;
break;
}
if (j==-1)
return puts("-1"),0;
bnum[j]--,anum[j]++;
bnum[i]++,anum[i]--;
}
}
}
*/
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 110;
int anum[10],bnum[10];
int n,ans = 0;
int main()
{
//freopen("F:\\rush.txt", "r", stdin);
rei(n);
rep1(i, 1, n)
{
int x;
rei(x);
anum[x]++;
}
rep1(i, 1, n)
{
int x;
rei(x);
bnum[x]++;
}
rep1(i, 1, 5)
{
if (bnum[i]>anum[i])
{
rep1(j, i, 5)
swap(anum[j], bnum[j]);
}
if (bnum[i]<anum[i])
{
if ((bnum[i] + anum[i]) & 1)
return puts("-1"), 0;
while (bnum[i]<anum[i])
{
int j = -1;
rep1(k, i + 1, 5)
if (bnum[k]>anum[k] && (anum[k] + bnum[k]) % 2 == 0)
{
j = k;
break;
}
if (j == -1)
return puts("-1"), 0;
bnum[j]--, anum[j]++;
bnum[i]++, anum[i]--;
ans++;
}
}
}
printf("%d\n", ans);
return 0;
}
【codeforces 779A】Pupils Redistribution的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 758C】Unfair Poll
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【Codeforces 670C】 Cinema
[题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...
随机推荐
- RGB颜色转换为多种颜色工具、公式
http://www.easyrgb.com/index.php?X=CALC#Result http://www.easyrgb.com/index.php?X=MATH
- 利用mysql5.6 的st_distance 实现按照距离远近排序。 (转载)
http://blog.csdn.net/zhouzhiwengang/article/details/53612481
- [Vue + TS] Write a Vue Component as a Class in TypeScript
Starter app: https://github.com/alexjoverm/Vue-Typescript-Starter Writing Vue components as plain ob ...
- ios 推断是qq,银行卡,手机号等等公用的方法。
#import <Foundation/Foundation.h> typedef enum { IdentifierTypeKnown = 0, IdentifierT ...
- 【MemSQL Start[c]UP 3.0 - Round 1 E】Desk Disorder
[链接]h在这里写链接 [题意] 有N个人. 2N个座位. 现在告诉你这N个人它们现在的座位.以及它们想去的座位. 每个人可以去它们想去的座位或者就站在原地不动. 新的座位和旧的座位,都不允许一个座位 ...
- JavaScript字符串替换replace方法
在日常的js开发中, 当要把字符串中的内容替换时,如果使用类似C#的string.replace方法,如下 var str='aabbccaa'; str=str.replace('aa','dd') ...
- Oracle中暂时表空间的清理
作者:iamlaosong Oracle暂时表空间主要用来做查询和存放一些缓冲区数据. 暂时表空间消耗的主要原因是须要对查询的中间结果进行排序.暂时表空间的主要作用: 索引create或rebuild ...
- VSX(翻译)Moving Code Blocks Among Code Regions using VS 2010 Extensions
Moving Code Blocks Among Code Regions using VS 2010 Extensions (翻译)使用VS 2010 扩展性将代码块移至Region区域中 Down ...
- linux下查看动态链接库依赖关系的命令 x86: ldd *.so arm: arm-linux-readelf -d *.so 实际例子: 以项目中用到的库librtsp.so分析: lijun@ubuntu:~/workspace$ arm-hisiv100nptl-linux-ld -d librtsp.so arm-hisiv100nptl-linux-ld:
linux下查看动态链接库依赖关系的命令 x86:ldd *.so arm:arm-linux-readelf -d *.so 实际例子:以项目中用到的库librtsp.so分析:l ...
- Linux中U盘和SD卡加载卸载命令
U盘挂载命令U盘插入的时候会显示启动信息,启动信息中sda: sda1指U盘的设备名为sda1dev设备目录下有一个sda1设备文件,此设备文件就是我们插入的U盘,我们将这个设备文件挂载到Linux系 ...