https://cn.vjudge.net/contest/309482#problem/C

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = ;
int a[MAXN][];
int ans;
int main() {
int T;
scanf("%d", &T);
while (T--) {
int n;
scanf("%d", &n);
for (int i = ; i < n; i++) {
a[i][] = a[i][] = ;
}
long long ans = ;
for (int i = , x, y; i < * n; i++) {
scanf("%d %d", &x, &y);
x--;
if (y >= ) { //Y>2的全部移动到2
ans += y - ;
y = ;
} else { //Y<1的全部移动到1
ans += - y;
y = ;
}
if (x < ) { //X<1的全部移动到1
ans -= x;
x = ;
}
if (x >= n) { //X>N的全部移动到N
ans += x - (n - );
x = n - ;
}
a[x][y]++;
}
//cout<<" : "<<ans<<endl;
for (int i = ; i < n; i++) {
//每个位置固定留一个
a[i][]--;
a[i][]--;
if (1LL * a[i][]*a[i][] < ) { //如果出现上下一个多的一个少的
int t = min(abs(a[i][]), abs(a[i][]));
if (a[i][] > ) {
a[i][] -= t;
a[i][] += t;
} else {
a[i][] -= t;
a[i][] += t;
}
ans += t;
}
//有多的就从左边移动到右边且如果缺的话也将缺的值传递给右边
ans += abs(a[i][]);
a[i + ][] += a[i][];
ans += abs(a[i][]);
a[i + ][] += a[i][];
}
cout << ans << endl;
}
return ;
}

OpenJudge POJ C19C 贪心的更多相关文章

  1. OpenJudge / Poj 2141 Message Decowding

    1.链接地址: http://poj.org/problem?id=2141 http://bailian.openjudge.cn/practice/2141/ 2.题目: Message Deco ...

  2. OpenJudge/Poj 2105 IP Address

    1.链接地址: http://poj.org/problem?id=2105 http://bailian.openjudge.cn/practice/2105 2.题目: IP Address Ti ...

  3. OpenJudge/Poj 2027 No Brainer

    1.链接地址: http://bailian.openjudge.cn/practice/2027 http://poj.org/problem?id=2027 2.题目: 总Time Limit: ...

  4. OpenJudge/Poj 2013 Symmetric Order

    1.链接地址: http://bailian.openjudge.cn/practice/2013 http://poj.org/problem?id=2013 2.题目: Symmetric Ord ...

  5. OpenJudge/Poj 1088 滑雪

    1.链接地址: bailian.openjudge.cn/practice/1088 http://poj.org/problem?id=1088 2.题目: 总Time Limit: 1000ms ...

  6. OpenJudge/Poj 2001 Shortest Prefixes

    1.链接地址: http://bailian.openjudge.cn/practice/2001 http://poj.org/problem?id=2001 2.题目: Shortest Pref ...

  7. OpenJudge/Poj 2000 Gold Coins

    1.链接地址: http://bailian.openjudge.cn/practice/2000 http://poj.org/problem?id=2000 2.题目: 总Time Limit: ...

  8. OpenJudge/Poj 1936 All in All

    1.链接地址: http://poj.org/problem?id=1936 http://bailian.openjudge.cn/practice/1936 2.题目: All in All Ti ...

  9. OpenJudge/Poj 1661 帮助 Jimmy

    1.链接地址: bailian.openjudge.cn/practice/1661 http://poj.org/problem?id=1661 2.题目: 总Time Limit: 1000ms ...

随机推荐

  1. php代码调试的重要性

    从去年开始做PHP,基本上有的集成环境用了一个遍,XAMPP,WAMP,phpStudy.都是部署一个环境,就在环境下的默认访问目录去创建项目运行.用PHPStorm一直没能在本地做过什么调试.要么不 ...

  2. [译]如何使用Python构建指数平滑模型:Simple Exponential Smoothing, Holt, and Holt-Winters

    原文连接:How to Build Exponential Smoothing Models Using Python: Simple Exponential Smoothing, Holt, and ...

  3. Mysql的binlog 和InnoDB的redo-log

    来源:https://www.jianshu.com/p/4bcfffb27ed5 mysql日志系统之redo log和bin log Mr林_月生关注 12018.12.02 01:35:06字数 ...

  4. leetcode907 Sum of Subarray Minimums

    思路: 对于每个数字A[i],使用单调栈找到A[i]作为最小值的所有区间数量,相乘并累加结果.时间复杂度O(n). 实现: class Solution { public: int sumSubarr ...

  5. Optional的理解和使用

    1.Optional 到底是什么? Optional 是一个包装类.类中包装的对象可以为 NULL 或非 NULL.简单说就是把 NULL 包了一层,防止直接对 NULL 操作报NPE. 2.Opti ...

  6. jupyter notebook 常用快捷操作

    Shift-Enter 执行当前cell,并自动跳到下一个cell Ctrl-Enter 执行当前cell,执行后不自动调转到下一个cell DD 删除当前的cell L 为当前的cell加入line ...

  7. HTML 全局属性 = HTML5 中添加的属性。

    属性 描述 accesskey 规定激活元素的快捷键. class 规定元素的一个或多个类名(引用样式表中的类). contenteditable 规定元素内容是否可编辑. contextmenu 规 ...

  8. Java虚拟机内存管理小结

  9. NOIP2012 借教室 题解 洛谷P1083

    一看就是暴力 好吧,其实是线段树或差分+二分,这里用的是差分+二分的做法. 二分部分的代码,套个二分板子就行 ,right=m; while(left<right)//二分 { ; ; else ...

  10. 怎样理解 display:none 和 visibility:hidden

    1. display: none会使元素节点 "消失" , 就像 死亡后灰飞烟灭了. 它是不占位置的. 2. visibility: hidden会使元素节点 "隐藏&q ...