题目链接

题意

有\(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的更多相关文章

  1. 【POJ2886】Who Gets the Most Candies?-线段树+反素数

    Time Limit: 5000MS Memory Limit: 131072K Case Time Limit: 2000MS Description N children are sitting ...

  2. poj 3159 Candies 差分约束

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 22177   Accepted: 5936 Descrip ...

  3. Who Gets the Most Candies?(线段树 + 反素数 )

    Who Gets the Most Candies? Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%I64d &am ...

  4. poj---(2886)Who Gets the Most Candies?(线段树+数论)

    Who Gets the Most Candies? Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 10373   Acc ...

  5. poj3159 Candies(差分约束,dij+heap)

    poj3159 Candies 这题实质为裸的差分约束. 先看最短路模型:若d[v] >= d[u] + w, 则连边u->v,之后就变成了d[v] <= d[u] + w , 即d ...

  6. HDU 5127 Dogs' Candies

    Dogs' Candies Time Limit: 30000/30000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) T ...

  7. C. Om Nom and Candies 巧妙优化枚举,将复杂度控制在10e6

    C. Om Nom and Candies 无线超大背包问题 #include <iostream> #include <cstdio> #include <cstrin ...

  8. POJ 3159 Candies (栈优化spfa)

    Candies 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description During the kinderga ...

  9. POJ 3159 Candies(差分约束,最短路)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 20067   Accepted: 5293 Descrip ...

随机推荐

  1. 想在已创建的Vue工程里引入vux组件

    <1>. 在项目里安装vux npm install vux --save <2>. 安装vux-loader (这个vux文档似乎没介绍,当初没安装结果报了一堆错误) npm ...

  2. dentry path_lookat dput

    https://www.ibm.com/developerworks/cn/linux/l-cn-usagecounter/index.html https://blog.csdn.net/young ...

  3. centso7 安装redmine

    一.安装rvm ###安装rvm gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D3 ...

  4. 关于WPF中Popup中的一些用法的总结

    Popup控件是一个常用的非常有用的控件,顾明思义就是弹出式控件,首先我们来看看MSDN对它的解释吧,表示具有内容的弹出窗口,这个是非常重要的控件,我们看看它的继承关系吧: System.Object ...

  5. nginx反向代理proxy_pass的问题

    起因:今天企业部署一个项目,用的nginx做的反向代理,配置如下: 测试结果令人失望,IP:端口 能访问项目,域名:端口 也能访问 ,但是 域名/接口名 访问失败 ################## ...

  6. Chrome & QR Code Reader

    Chrome & QR Code Reader Allows to generate a QR Code for the current page and scan a QR Code usi ...

  7. MySQL 大数据量分页优化

    假设有一个千万量级的表,取1到10条数据: ,; ,; 这两条语句查询时间应该在毫秒级完成: ,; 你可能没想到,这条语句执行之间在5s左右: 为什么相差这么大? 可能mysql并没有你想的那么智能, ...

  8. 1.rabbitmq高可用方案

    采用标准集群模式  HAPROXY + rabbitmq 2个 ram  和  一个 disk 节点 主机规划: 192.168.157.128 haproxy keepalive 主 ram节点 1 ...

  9. Nginx 模块分类

    L:34

  10. AMD三代锐龙箭在弦上:如此家族堪称豪华

    AMD将于今年年中正式推出第三代Ryzen锐龙处理器,即不集成显示芯片的纯CPU产品,外界猜测可能的时间点会是5月底的台北电脑展. 虽说依然采用AM4接口,也就是X370/470老主板可无压力兼容,但 ...