【题目链接】: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. java 将byte[]转为各种进制的字符串

    public void test() { byte[] bytes = new byte[10000000]; for (int i = 0; i < 10000000; i++) { if ( ...

  2. job调度时间格式

    */5 * * * * ?---------------每隔5秒执行一次0 */1 * * * ?---------------每隔1分钟执行一次0 0 23 * * ?--------------- ...

  3. spring:为javabean的集合对象注入属性值

    spring:为JavaBean的集合对象注入属性值 在 spring 中可以对List.Set.Map 等集合进行配置,不过根据集合类型的不同,需要使用不同的标签配置对应相应的集合. 1.创建 Ts ...

  4. Privoxy shadowscocks代理

    ubuntu已经启动好了sock5的代理, 代理为: 127.0.0.1:1080. #使用Privoxy将sock5代理映射为http代理. 安装Privoxy sudo apt-get updat ...

  5. C++_homework_EraseComment

    顾名思义就是删除程序中的注释,不清楚fsm的机制,完全是自己的思路做. 开头先读取一个字符确定是否到文件结尾,如果读取成功,是换行的话就换行,并继续读取,不是的话,用putback放回缓冲区,并整行读 ...

  6. Java-java-com-util-common-service:ServiceException.java

    ylbtech-Java-java-com-util-common-service:ServiceException.java 1.返回顶部 1. package com.shineyoo.manag ...

  7. 初识Java,Java语言概述

    Java语言是SUN(斯坦福大学网络公司)公司1995年推出的一门高级编程语言,由此James Gosling被公认为Java语言之父.Java语言起初运用在小型的家用电子产品上,后来随着互联网的发展 ...

  8. leetcode数学相关

    目录 166分数到小数 169/229求众数 238除自身以外数组的乘积 69Sqrt(x) 求平方根 231Power of Two 166分数到小数 给定两个整数,分别表示分数的分子 numera ...

  9. E20170905-mk

    recursive   adj. 回归的,递归的;

  10. 4.Flask-alembic数据迁移工具

    alembic是用来做ORM模型与数据库的迁移与映射.alembic使用方式跟git有点类似,表现在两个方面,第一个,alemibi的所有命令都是以alembic开头: 第二,alembic的迁移文件 ...