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 ...
随机推荐
- Failed to bind properties under 'spring.datasource' to javax.sql.DataSource
这是我的配置文件 # 国际化配置文件(包名.基础名) spring.messages.basename=i18n.login server.tomcat.uri-encoding=UTF- sprin ...
- php5.6.x到php7.0.x特性
php5.6.x到php7.0.x特性 1.标量类型声明 字符串(string), 整数 (int), 浮点数 (float), 布尔值 (bool),callable,array,self,Clas ...
- composer 下载包慢的解决方法
方法一: 修改 composer 的全局配置文件(推荐方式) 打开命令行窗口(windows用户)或控制台(Linux.Mac 用户)并执行如下命令: composer config -g repo. ...
- 在python中定义二维数组
发表于 http://liamchzh.0fees.net/?p=234&i=1 一次偶然的机会,发现python中list非常有意思. 先看一段代码 [py]array = [0, 0, 0 ...
- jenkins 邮箱设置
一.先设置管理员邮箱地址 二.设置邮箱
- Java8新特性之Collectors
参考:Java8新特性之Collectors 在第二天,你已经学习了Stream API能够让你以声明式的方式帮助你处理集合.我们看到collect是一个将管道流的结果集到一个list中的结束操作.c ...
- python学习 第二天
一.变量 1.变量名: 数字,字母,下划线 alex1=123 sb=“alex” a_lex=“sb” 不能以数字开头 lalex 变量名不是python内部的关键字 {‘and’,'as','as ...
- $mount(“#app”)手动挂载
没有el属性时,证明vue还没绑定到特定的dom上,需要延迟加载,则使用.$mount("")进行手动挂载 https://blog.csdn.net/longzhoufeng/a ...
- 【Python练习题】程序3
3.题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?x+100 = n*nx+100+168 = m * m 所以(m+n)*(m-n) =168 #题 ...
- 51nod-1445-变色DNA(最短路)
题意:题目是说从0到n-1,我还是习惯从1到n,所以以下我都这么写,大概题意就是(i, j)==‘Y’表示可以从i颜色变成j颜色,然后问我们最少删除几个会影响结果的‘Y’,能到n这个颜色: 没有意义的 ...