CF341E Candies Game
题意
有\(n\)个盒子,第\(i\)个盒子里面有\(a_i\)个糖果。每次选择两个盒子\(i,j\),假设\(a_i \le a_j\)。然后从第\(j\)个盒子中拿出\(a_i\)个糖果,放到第\(i\)个盒子里面(显然,如果\(a_i=a_j\),那么第\(j\)个盒子会变成空的)。你可以这样操作任意多次。要求最后只有\(2\)个盒子里面有糖果。输出方案。如果无论如何操作也无法满足条件,输出"\(-1\)"
思路
考虑当前有\(3\)个盒子\(i,j,k (a_i\le a_j\le a_k)\)。其实可以发现,一定能够使得这三个盒子中的某个变为空。操作如下:
设\(t = \lfloor\frac{a_j}{a_i}\rfloor\)。对于第\(i\)次操作,如果\(t\)的二进制中第\(i-1\)位为\(1\)的话,就操作\(a_i,a_j\), 否则操作\(a_i,a_k\)。然后\(a_j\)就会变成\(a_j\%a_i\)。然后再重新排序,重复上面的操作,直到有一个盒子变为空.
所以只要每次选出三个盒子进行上面的操作就行了。
代码
/*
* @Author: wxyww
* @Date: 2019-02-13 10:00:24
* @Last Modified time: 2019-02-13 10:17:54
*/
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<ctime>
using namespace std;
typedef long long ll;
const int N = 1000010;
ll read() {
ll x=0,f=1;char c=getchar();
while(c<'0'||c>'9') {
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9') {
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
struct node {
int x,id;
}a[N],ans[N];
int ansjs;
bool cmp(node x,node y) {
if(x.x != y.x)
return x.x > y.x;
return x.id < y.id;
}
void solve() {
int k = a[2].x / a[3].x;
while(k) {
if(k & 1) {
a[2].x -= a[3].x;
a[3].x <<= 1;
// printf("%d %d\n",a[])
ans[++ansjs].x = a[3].id,ans[ansjs].id = a[2].id;
}
else {
a[1].x -= a[3].x;
a[3].x <<= 1;
ans[++ansjs].x = a[3].id,ans[ansjs].id = a[1].id;
}
k >>= 1;
}
}
int main() {
int n = read();
for(int i = 1;i <= n;++i) a[i].id = i,a[i].x = read();
sort(a + 1,a + n + 1,cmp);
if(!a[2].x) {
puts("-1");return 0;
}
while(a[3].x) {
// printf("%d %d %d\n",a[1].id,a[2].id,a[3].id);
solve();
sort(a + 1,a + n + 1,cmp);
}
printf("%d\n",ansjs);
for(int i = 1;i <= ansjs;++i) printf("%d %d\n",ans[i].x,ans[i].id);
return 0;
}
CF341E Candies Game的更多相关文章
- 【POJ2886】Who Gets the Most Candies?-线段树+反素数
Time Limit: 5000MS Memory Limit: 131072K Case Time Limit: 2000MS Description N children are sitting ...
- poj 3159 Candies 差分约束
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 22177 Accepted: 5936 Descrip ...
- Who Gets the Most Candies?(线段树 + 反素数 )
Who Gets the Most Candies? Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%I64d &am ...
- poj---(2886)Who Gets the Most Candies?(线段树+数论)
Who Gets the Most Candies? Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 10373 Acc ...
- poj3159 Candies(差分约束,dij+heap)
poj3159 Candies 这题实质为裸的差分约束. 先看最短路模型:若d[v] >= d[u] + w, 则连边u->v,之后就变成了d[v] <= d[u] + w , 即d ...
- HDU 5127 Dogs' Candies
Dogs' Candies Time Limit: 30000/30000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) T ...
- C. Om Nom and Candies 巧妙优化枚举,将复杂度控制在10e6
C. Om Nom and Candies 无线超大背包问题 #include <iostream> #include <cstdio> #include <cstrin ...
- POJ 3159 Candies (栈优化spfa)
Candies 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description During the kinderga ...
- POJ 3159 Candies(差分约束,最短路)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 20067 Accepted: 5293 Descrip ...
随机推荐
- springmvc配置文件
1 springMVC的配置文件路径问题 https://www.cnblogs.com/ysloong/p/6071450.html
- php5.6.x到php7.0.x特性
php5.6.x到php7.0.x特性 1.标量类型声明 字符串(string), 整数 (int), 浮点数 (float), 布尔值 (bool),callable,array,self,Clas ...
- Swagger2常用注解及其说明 (转)
Api 用在Controller中,标记一个Controller作为swagger的文档资源 属性名称 说明 value Controller的注解 description 对api资源的描述 hid ...
- HTML5开发之meta标签的viewport使用说明
随着高端手机(Andriod,Iphone,Ipod,WinPhone等)的盛行,移动互联应用开发也越来越受到人们的重视,用html5开发移动应用是最好的选择.然而每一款手机有不同的分辨率,不同屏幕大 ...
- $mount(“#app”)手动挂载
没有el属性时,证明vue还没绑定到特定的dom上,需要延迟加载,则使用.$mount("")进行手动挂载 https://blog.csdn.net/longzhoufeng/a ...
- 今日头条移动app广告激活数据API对接完整Java代码实现供大家参考》》》项目随记
这是自毕业后的第一篇博客,希望自己今后能养成写博客的一个好习惯.最近公司为了加速APP推广,采取在外部平台(如:今日头条)进行广告投放的方式,进行用户引流.因此我们需要对广告的激活数据进行一个检测,跟 ...
- 搭建Hexo博客(一)-创建Hexo环境
Hexo配合github,可以创建自己的博客.基本原理是使用Hexo生成静态页面,发布到github上.在本地需要搭建Hexo环境. 1.安装nodejs 下载并安装NodeJS,官网地址:https ...
- ScheduledThreadPoolExecutor 吞异常
线程挂掉不一定会输出日志到控制台,比如ScheduledThreadPoolExecutor,如果在执行的任务中有未捕获的异常抛出,就行停止调度,没有任何错误输出到控制台或日志文件.在项目中这会导致一 ...
- python模块psutil的使用
介绍 psutil是一个跨平台库(http://code.google.com/p/psutil/),能够轻松实现获取系统运行的进程和系统利用率(包括CPU.内存.磁盘.网络等)信息.它主要应用于系统 ...
- DRF 分页组件
Django Rest Framework 分页组件 DRF的分页 为什么要使用分页 其实这个不说大家都知道,大家写项目的时候也是一定会用的, 我们数据库有几千万条数据,这些数据需要展示,我们不可能直 ...