OpenJudge POJ C19C 贪心
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 贪心的更多相关文章
- OpenJudge / Poj 2141 Message Decowding
1.链接地址: http://poj.org/problem?id=2141 http://bailian.openjudge.cn/practice/2141/ 2.题目: Message Deco ...
- OpenJudge/Poj 2105 IP Address
1.链接地址: http://poj.org/problem?id=2105 http://bailian.openjudge.cn/practice/2105 2.题目: IP Address Ti ...
- OpenJudge/Poj 2027 No Brainer
1.链接地址: http://bailian.openjudge.cn/practice/2027 http://poj.org/problem?id=2027 2.题目: 总Time Limit: ...
- OpenJudge/Poj 2013 Symmetric Order
1.链接地址: http://bailian.openjudge.cn/practice/2013 http://poj.org/problem?id=2013 2.题目: Symmetric Ord ...
- OpenJudge/Poj 1088 滑雪
1.链接地址: bailian.openjudge.cn/practice/1088 http://poj.org/problem?id=1088 2.题目: 总Time Limit: 1000ms ...
- OpenJudge/Poj 2001 Shortest Prefixes
1.链接地址: http://bailian.openjudge.cn/practice/2001 http://poj.org/problem?id=2001 2.题目: Shortest Pref ...
- OpenJudge/Poj 2000 Gold Coins
1.链接地址: http://bailian.openjudge.cn/practice/2000 http://poj.org/problem?id=2000 2.题目: 总Time Limit: ...
- OpenJudge/Poj 1936 All in All
1.链接地址: http://poj.org/problem?id=1936 http://bailian.openjudge.cn/practice/1936 2.题目: All in All Ti ...
- OpenJudge/Poj 1661 帮助 Jimmy
1.链接地址: bailian.openjudge.cn/practice/1661 http://poj.org/problem?id=1661 2.题目: 总Time Limit: 1000ms ...
随机推荐
- ASP.NET 拼多多用户登录授权后使用code去换取access_token
一.拼多多开放平台 由于本人刚毕业进公司实习 遇到一些问题然后想通过博客来记录和分享给大家一起学习. 第一次写博客没什么经验不是写的很好 请大家多多关照 嘴下留情哈哈 谢谢! 好了 话不多说直接进入主 ...
- WhatsApp Group vs WhatsApp Broadcast for Business
WhatsApp Group vs WhatsApp Broadcast for Business By Iaroslav Kudritskiy If you've read our Ultimate ...
- Docker存储容易忽略的使用细节
一.Docker容器使用前其实有个非常重要的步骤就是规划好部署的磁盘区域,因为docker容器默认存储的路径是在/var/lib/docker的根目录内,随着使用时间越长部署的内容越多,基本的根目录的 ...
- SQL SERVER2014 加密 备份恢复 学习 (一)
SQL 自2008(还是2005)之后,推出加密功能,可以一定程度上保护数据库的备份安全.以下测试环境为:sql server 2014主要目的:将备份的文件加密,在其它电脑上恢复时必须有证书和密钥才 ...
- 洛谷 题解 P2280 【[HNOI2003]激光炸弹】
一道很好的二维前缀和模板题. 什么是二维前缀和? 从这张图可以看出前缀和的求法: Map[i][j]=Map[i-1][j]+Map[i][j-1]-Map[i-1][j-1]+Map[i][j]; ...
- 【gcd】辗转相除法
#include<stdio.h> int gcd(int a, int b) { int c; while(b) { c = a % b; a = b; b = c; } return ...
- PID程序实现
传统PID(位置式PID控制)调节: 这种算法的缺点是,由于全量输出,每次输出均与过去的状态有关,计算时要对 e(k) 进行累加,计算机运算工作量大.而且,因为计算机输出的 u(k) 对应的是执行机构 ...
- PHP学习之PHP的语法糖
PHP的语法糖 计算机语言中添加的某种语法,这种语法对语言的功能并没有影响,但是更方便程序员使用. 常见的PHP的语法糖 echo(),print(),die(),isset(),unset(),i ...
- Django新手入门必看
pip install django==2.1.7 (现在Django3.0出来,推荐大家可以使用一下Django3.0) pip list查看
- easyui datagrid 合并相同行
$.extend($.fn.datagrid.methods, { autoMergeCells: function (jq, fields) { return jq.each(function () ...