【题目链接】:http://hihocoder.com/problemset/problem/1499

【题意】

【题解】



贪心,模拟;

从左往右对于每一列;

如果上下两个格子;



有一个格子超过了所需;

另外一个格子小于所需;

则把超过了的格子转移一些到小于的那个,让上下两个互补;

然后把剩下的往右传(肯定是要往右传的);



如果两个格子都超过了所需;

则把两个格子多余的硬币都往右传.



如果两个格子都小于所需;

则两个格子都从右边借一些

【Number Of WA】



1(输入是一列一列地输入的…)



【完整代码】

#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 ms(x,y) memset(x,y,sizeof 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 = 100500; int n;
LL a[3][N],sum,goal,ans = 0; int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
cin >> n;
rep1(i,1,n)
{
cin >> a[1][i] >> a[2][i];
sum+=a[1][i],sum+=a[2][i];
}
goal = sum/(2*n);
rep1(j,1,n)
{
if (a[1][j]>=goal && a[2][j]>=goal)
{
ans+=a[1][j]-goal,ans+=a[2][j]-goal;
a[1][j+1]+=a[1][j]-goal;
a[2][j+1]+=a[2][j]-goal;
continue;
}
if (a[1][j]>=goal && a[2][j]<goal)
{
LL need = goal-a[2][j],have = a[1][j]-goal;
if (have>=need)
{
ans+=have;
a[1][j+1]+=have-need;
}
else
{
ans+=have;
a[2][j]+=have;
a[2][j+1]-=(goal-a[2][j]);
ans+=(goal-a[2][j]);
}
continue;
}
if (a[1][j]<goal && a[2][j]>=goal)
{
LL need = goal-a[1][j],have = a[2][j]-goal;
if (have>=need)
{
ans+=have;
a[2][j+1]+=have-need;
}
else
{
ans+=have;
a[1][j]+=have;
a[1][j+1]-=(goal-a[1][j]);
ans+=goal-a[1][j];
}
continue;
}
if (a[1][j]<goal && a[2][j] < goal)
{
ans+=goal-a[1][j],ans+=goal-a[2][j];
a[1][j+1]-=(goal-a[1][j]),a[2][j+1]-=(goal-a[2][j]);
continue;
}
}
cout << ans << endl;
return 0;
}

【hihocoder 1499】A Box of Coins的更多相关文章

  1. 【hihocoder 1298】 数论五·欧拉函数

    [题目链接]:http://hihocoder.com/problemset/problem/1298 [题意] [题解] 用欧拉筛法; 能够同时求出1..MAX当中的所有质数和所有数的欧拉函数的值; ...

  2. 【hihocoder 1297】数论四·扩展欧几里德

    [题目链接]:http://hihocoder.com/problemset/problem/1297 [题意] [题解] 问题可以转化为数学问题 即(s1+v1*t)%m == (s2+v2*t)% ...

  3. 【hihocoder 1296】数论三·约瑟夫问题

    [题目链接]:http://hihocoder.com/problemset/problem/1296 [题意] [题解] [Number Of WA] 0 [完整代码] #include <b ...

  4. 【hihocoder 1295】Eular质数筛法

    [题目链接]:http://hihocoder.com/problemset/problem/1295 [题意] [题解] 可以在O(N)的复杂度内求出1..N里面的所有素数; 当然受空间限制,N可能 ...

  5. 【hihocoder 1287】 数论一·Miller-Rabin质数测试

    [题目链接]:http://hihocoder.com/problemset/problem/1287 [题意] [题解] 取的底数必须是小于等于n-1的; 那12个数字能通过2^64以内的所有数字; ...

  6. 【hihocoder 1333】平衡树·Splay2

    [题目链接]:http://hihocoder.com/problemset/problem/1333 [题意] [题解] 伸展树; 要求提供操作: 1.插入一个元素,两个权值,id作为查找的比较权值 ...

  7. 【hihocoder 1329】平衡树·Splay(Splay做法)

    [题目链接]:http://hihocoder.com/problemset/problem/1329 [题意] [题解] 插入操作:-,记住每次插入之后都要把它放到根节点去就好; 询问操作:对于询问 ...

  8. 【hihocoder 1329】 平衡树·Splay(set做法)

    [题目链接]:http://hihocoder.com/problemset/problem/1329 [题意] [题解] 因为一开始是空的树,所以; n其实就代表了树中的最多元素个数; 则最坏的情况 ...

  9. 【hihocoder 1476】矩形计数

    [题目链接]:http://hihocoder.com/problemset/problem/1476 [题意] [题解] 首先不考虑黑格子,计算出一共有多少个矩形: 枚举矩形的大小r×c,这样大小的 ...

随机推荐

  1. Codeforces Round #250 Div. 2(C.The Child and Toy)

    题目例如以下: C. The Child and Toy time limit per test 1 second memory limit per test 256 megabytes input ...

  2. Codeforces Round #277 (Div. 2) D. Valid Sets DP

    D. Valid Sets   As you know, an undirected connected graph with n nodes and n - 1 edges is called a  ...

  3. 2498 IncDec Sequence

    2498 IncDec Sequence  时间限制: 1 s  空间限制: 64000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description 给 ...

  4. samba笔记

    ############ 1.安装网络yum ############ 2.安装createrepo [root@localhost ~]# yum install createrepo-0.9.8- ...

  5. B1024 生日快乐 递归。。。

    bzoj1024叫生日快乐,其实很简单,但是没看出来就很尴尬... Description windy的生日到了,为了庆祝生日,他的朋友们帮他买了一个边长分别为 X 和 Y 的矩形蛋糕.现在包括win ...

  6. bzoj4004 [JLOI2015]装备购买——线性基+贪心

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4004 今天讲课讲到的题,据说满足拟阵的性质,所以贪心是正确的: 总之就贪心,按价格从小到大排 ...

  7. Python 37 进程池与线程池 、 协程

    一:进程池与线程池 提交任务的两种方式: 1.同步调用:提交完一个任务之后,就在原地等待,等任务完完整整地运行完毕拿到结果后,再执行下一行代码,会导致任务是串行执行 2.异步调用:提交完一个任务之后, ...

  8. Python 35 进程间的通信(IPC机制)、生产者消费者模型

    一:进程间的通信(IPC):先进先出  管道:队列=管道+锁 from multiprocessing import Queue q=Queue(4) q.put(['first',],block=T ...

  9. 【SQL】CONNECT BY 层次化查询

    层次化查询,顾名思义就是把查询结果有层次的呈现出来.层次化查询结果类似于树状结构,最顶端的是“根节点”,下面是“父节点”,没有子节点的是“叶节点”. 为了让一个或多个表具有层次关系,必须使用相关的字段 ...

  10. MySQL 优化之 index_merge (索引合并)

    深入理解 index merge 是使用索引进行优化的重要基础之一.理解了 index merge 技术,我们才知道应该如何在表上建立索引. 1. 为什么会有index merge 我们的 where ...