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 ...
随机推荐
- java使用顺序存储实现队列
详细连接 https://blog.csdn.net/ljxbbss/article/details/78135993 操作系统:当电脑卡的时候,如果不停点击,还是卡死,最后终于电脑又好了以后,操作 ...
- 关于golang.org/x包问题
关于golang.org/x包问题 由于谷歌被墙,跟谷歌相关的模块无法通过go get来下载,解决方法: git clone https://github.com/golang/net.git $GO ...
- Django--ORM和单表查询
一 . ORM ORM是“对象-关系-映射”的简称.(Object Relational Mapping,简称ORM) 二. 单表操作 要想将模型转为mysql数据库中的表,需要在setting里面写 ...
- nginx反向代理proxy_pass的问题
起因:今天企业部署一个项目,用的nginx做的反向代理,配置如下: 测试结果令人失望,IP:端口 能访问项目,域名:端口 也能访问 ,但是 域名/接口名 访问失败 ################## ...
- BugFree 安装
BugFree基于PHP和MySQL开发,是免费且开发源代码的缺陷管理系统.服务器端在Linux和Windows平台上都可以运行:客户端无需安装任何软件,通过IE,FireFox等浏览器就可以自由使用 ...
- SQL约束(主键约束、外键约束、自动递增、不允许空值、值唯一、值默认、值限制范围)
NOT NULL 不允许空值约束 NOT NULL 约束强制列不接受 NULL 值(NULL值就是没有值或缺值).NOT NULL 约束强制字段始终包含值,即不向字段添加值,就无法插入新记录或者更新记 ...
- JSTL 之 <c:out>
jstl的<c:out value="${hello}"></c:out> EL表达式的${hello },两者一般没什么不同,但是EL表达式输出的时候回尝 ...
- Vuex的API文档
前面的话 本文将详细介绍Vuex的API文档 概述 import Vuex from 'vuex' const store = new Vuex.Store({ ...options }) [构造器选 ...
- codevs3044
codevs3044 题面 大意:给出n个矩形求覆盖的总面积 看了hzwer的blog 似懂非懂 链接 可能还要多练点吧qaq #include <bits/stdc++.h> using ...
- vi简短教程
1.模式 命令行模式:光标的移动.内容删除移动复制操作 插入模式:文字输入,即编辑状态 底行模式:文件保存或退出vi,设置编辑环境 2.基本操作 vi myfile,输入vi 文件名,则进入vi. 3 ...