【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 ...
随机推荐
- 【例题 7-2 UVA - 11059】Maximum Product
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] C语言for循环练习题 [代码] /* 1.Shoud it use long long ? 2.Have you ever tes ...
- mybatis中整合ehcache缓存框架的使用
mybatis整合ehcache缓存框架的使用 mybaits的二级缓存是mapper范围级别,除了在SqlMapConfig.xml设置二级缓存的总开关,还要在具体的mapper.xml中开启二级缓 ...
- FarPoint.Win.Spread 常规操作
FarPoint.Win.Spread.FpSpread fSpread = new FarPoint.Win.Spread.FpSpread(); //设置 行数.列数 ...
- Android 关于java.util.NoSuchElementException错误
写了一个从A文件复制到B文件的例子,其中要求去掉重复的行. 于是想到了Set,这本来是很容易的事情,结果在向外写数据时抱错 Java.util.NoSuchElementException 网络上反复 ...
- java三大特性:封装、继承、多态
Java三大特性之封装 一.定义 封装性指的是将对象的状态信息隐藏在对象内部,不允许外部程序直接访问对象内部的信息,而是通过该类所提供的方法来实现对内部信息的操作和访问. 二.使用封装的好处: 1 ...
- JavaScript字符串替换replace方法
在日常的js开发中, 当要把字符串中的内容替换时,如果使用类似C#的string.replace方法,如下 var str='aabbccaa'; str=str.replace('aa','dd') ...
- centos php 安装memcached 扩展 支持sasl
1.安装sasl yum install cyrus-sasl-lib.x86_64 yum install cyrus-sasl-devel.x86_64 2.下载libmemcached wget ...
- [TypeStyle] Generate static css + html files using TypeStyle
You can easily use TypeStyle to build static html files with encapsulated CSS. You can use this patt ...
- Swift3.0 功能二 (表情键盘与图文混排)
随着iOS越来越多表情键盘以及图文混排的需求,本文运用Swift3.0系统的实现其功能以及封装调用方法,写的不好,如有错误望各位提出宝贵意见,多谢 项目源码地址: 相关知识点都有标识 项目源码地址 废 ...
- 【55.70%】【codeforces 557A】Ilya and Diplomas
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...