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. linux /etc/profile bashrc bash_profile

    文件: /etc/profile  ~/.bashrc  和  ~/.bash_profile 的使用区别: /etc/profile: 全局 环境变量等,在机器重启后执行一次, 用于设置环境变量,更 ...

  2. ssh自动添加hostkey到know_hosts

    The authenticity of host ‘git.sws.com (10.42.1.88)’ can’t be established. ECDSA key fingerprint is 5 ...

  3. Hyperledger Fabric 安全基础:身份系统 PKIs

    什么是身份系统 区块链网络中的角色包括对等节点(peer),订购着,客户端应用程序,管理员等等.这些参与者的身份都封装在X.509数字证书中.这些身份信息真的非常重要,因为他们决定了在网络中参与者具体 ...

  4. Hadoop学习笔记之三 数据流向

    http://hadoop.apache.org/docs/r1.2.1/api/index.html 最基本的: 1. 文本文件的解析 2. 序列文件的解析 toString会将Byte数组中的内存 ...

  5. 【IDEA】格式化代码技巧汇总

    1.格式化 Java 代码 快捷键:Ctrl+Alt+L 2.格式化 Mapper 文件中的 SQL 关联到数据库,让 IDEA 认识你的 SQL.如何关联?选择右侧的database,添加数据库即可 ...

  6. 通过IP得到IP所在地省市

    /// <summary> /// 通过IP得到IP所在地省市(Porschev) /// </summary> /// <param name="ip&quo ...

  7. PHP新特性

    1.太空船操作符<=> 2.变量类型限定 3.$a = $b??$c 4.常量数组,define($arr,['a','b']) 5.namespace批量导入 等等

  8. CentOS7安装MySQL 密码方面

    1.为了加强安全性,MySQL5.7为root用户随机生成了一个密码,在error log中,关于error log的位置,如果安装的是RPM包,则默认是/var/log/mysqld.log. 可通 ...

  9. SVN常用命令--Mac端【转载】

    * 版本库布局 1. trunk主干 trunk就是开发的主线,一般项目都是导入到主线来开发的. 2. branches分支 branches一般是trunk某个版本的拷贝,如果你想在某一段时间单独对 ...

  10. php 处理数字为金钱格式

    number_format(需要转换的数字,保留小数个数,小数点符号,每三位的分隔符) echo number_format("1000000")."<br> ...