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. QFramework 使用指南 2020 (一): 概述

    大家好,我是 QFramework 的作者 凉鞋,QFramework 从第一次代码提交到现在快 5 年了,期间陆陆续续增加了很多功能,在使用体验上做了大量的改进. 而市面上关于 QFramework ...

  2. httpurlConnection客户端发送文件与服务端接受文件

    import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; ...

  3. 【ARM-Linux开发】wubi.exe安装Ubuntu14.04出现为/检查磁盘时发生严重错误的解决方法

    原文:http://jingyan.baidu.com/article/0aa22375bbffbe88cc0d6419.html 我写过一篇叫做 如何硬盘装麒麟系统 的经验,介绍了如何不使用优盘的情 ...

  4. vue-cli 3.x搭建项目以及其中vue.config.js文件的配置

    参考链接:https://www.cnblogs.com/wxh0929/p/11271551.html vue-cli3全面配置详解:https://www.jb51.net/article/150 ...

  5. 洛谷 题解 CF903B 【The Modcrab】

    如果它在接下来一回合能一下就将你KO了,那么,你就十分需要回血(一直回到它一下敲不死你),否则你就一直打它就好了. #include<iostream> using namespace s ...

  6. 亿级Web系统搭建――单机到分布式集群 转载

    当一个Web系统从日访问量10万逐步增长到1000万,甚至超过1亿的过程中,Web系统承受的压力会越来越大,在这个过程中,我们会遇到很多的问题.为了解决这些性能压力带来问题,我们需要在Web系统架构层 ...

  7. axios设置请求头内容

    axios设置请求头中的Authorization 和 cookie 信息: GET请求 axios.get(urlString, { headers: { 'Authorization': 'Bea ...

  8. Vue.js + Element.ui 从搭建环境到打包部署

    一.搭建环境 由于新的node已经集成了npm,所以直接安装node,前往node官网下载最新版本的node,根据自己的操作系统选择相应的包,按照步骤一步步走就可以,这里不做过多介绍. 安装好后可以打 ...

  9. POJ3046ANT_COUNTING

    #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> ...

  10. mysqldump原理及实战

    使用mysqldump命令行工具创建逻辑备份: 注意mysqldump的版本和路径mysqldump命令创建的是逻辑备份,结果集有两种格式:一种是将数据转换成标准的SQL语句(一堆CREATE,DRO ...