【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 ...
随机推荐
- js进阶 13-11/12 jquery如何实现折叠导航
js进阶 13-11/12 jquery如何实现折叠导航 一.总结 一句话总结:还是用的slideToggle滑动效果,并且这一个展开时,所有兄弟都关闭. 1.文字缩进怎么设置? 感觉设置margin ...
- Java判断1个字符串中出现了几次其他字符串
public class Test { public static int count(String text,String sub){ , start =; ){ start += sub.leng ...
- 【Codeforces Round #301 (Div. 2) D】 Bad Luck Island
[链接] 我是链接,点我呀:) [题意] 剪刀.石头.布各有r,s,p个生活在同一个村子里. 它们两两之间相遇的几率都相同(相遇后就会按照划拳的规则判断输赢,输的人就死掉了). 问你最后只剩下剪刀,只 ...
- [Angular2 Router] Configure Auxiliary Routes in the Angular 2 Router - What is the Difference Towards a Primary Route?
In this tutorial we are going to learn how we can can configure redirects in the angular 2 router co ...
- OpenNI2获取华硕XtionProLive深度图和彩色图并用OpenCV显示
使用OpenNI2打开XtionProLive时有个问题,彩色图分辨率不管怎样设置始终是320*240,深度图倒是能够设成640*480,而OpenNI1.x是能够获取640*480的彩色图的. 彩色 ...
- CSS3实现的立体button
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 如何启用“锁定内存页”选项 (Windows)
默认情况下,禁用 Windows 策略"锁定内存页"选项.必须启用此权限才能配置地址窗口化扩展插件 (AWE).此策略将确定哪些帐户可以使用进程将数据保留在物理内存中,从而阻止系统 ...
- 【习题 3-6 UVA - 232】Crossword Answers
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟题.注意场宽为3 [代码] #include <bits/stdc++.h> using namespace std ...
- php 静态方法和非静态方法的调用说明
1. php类中,静态方法调用当前类的非静态方法必须用self关键字,不能用$this 2. php类中,公有方法调用私有方法使用$this关键字,只能实例化调用 3. php类中,公有方法调用私有方 ...
- iOS调试 - 基本技巧
在程序中,无论是你想弄清楚为什么数组中有3个对象而不是5个,或者为什么一个新的玩家开始之后,游戏在倒退——调试在这些处理过程中是比较重要的一部 分.通过本文的学习,我们将知道在程序中,可以使用的大多数 ...