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. 关于RNN(Recurrent Neural Network)的一篇文章

    文章链接:https://blog.csdn.net/zhaojc1995/article/details/80572098 写的很好!

  2. 【编程开发】opencv实现对Mat中某一列或某一行的元素进行normalization

    [编程开发]opencv实现对Mat中某一列或某一行的元素进行normalization 标签: [编程开发] [机器学习] 声明:引用请注明出处http://blog.csdn.net/lg1259 ...

  3. Vue父子组件传值$parent , ref,$refs,props大总结

    子组件: <template> <div class="child"> <slot name='meiyong'></slot> & ...

  4. luogu P1115 最大子段和 (dp)

    链接: https://www.luogu.org/problemnew/show/P1115 题面: 题目描述 给出一段序列,选出其中连续且非空的一段使得这段和最大. 输入输出格式 输入格式: 第一 ...

  5. loback的介绍与配置-(通俗易通)

    一.logback的配置介绍 Logback的配置分为三个内容:Logger.appender及layout Logger:作为日志的记录器,主要用于存放日志对象,也可以定义日志类型.级别. appe ...

  6. 20190805-Python基础 第二章 列表和元组(2)列表

    1. list函数,用于将字符串转换为列表 2. 基本的列表操作 修改列表 - 给元素赋值,使用索引表示法给特定的元素赋值,如x[1] = 2 删除元素 - 使用del语句即可 name1 = ['a ...

  7. 第5章:Linux系统管理

    1.文件读写 1).Python内置的open函数 f = open('data.txt', 'w') f.write('hello, world') f.close() 2).避免文件句柄泄露 tr ...

  8. VS2013 + Nunit 安装搭建

    Nunit 官方给我提供了Nunit 3的四种安装方式 第一种 通过NuGet进行Full Nunit安装 第二种 通过NuGet进行轻量级 NunitLite 安装 第三种 通过Zip 压缩包下载安 ...

  9. JVM描述符标识字符含义

    标识字符 含义 B byte C char D double F float I int J long S short Z boolean V void L 对象类型,如Ljava/lang/Obje ...

  10. c# 爬虫和组件HtmlAgilityPack处理html

    测试当前爬虫的User-Agent:http://www.whatismyuseragent.net/ 大佬的博客地址:https://www.cnblogs.com/jjg0519/p/670274 ...