贪心——Happy Line (CodeForces 549G)
来源:互联网 发布:自学网c语言 编辑:程序博客网 时间:2024/05/21 06:26
题目链接:
http://codeforces.com/problemset/problem/549/G分析:数据范围很大,n为200000,ai为10^9,所以相直接模拟做肯定会TLE(半小时成功T了QAQ) 。
以下转载自
(http://blog.csdn.net/qq_24451605/article/details/46586533)
题解:
我们假设两个人a和b,a,b的钱分别是x,y元,a,b的初始坐标为i1,j1;末位坐标为i2,j2;那么如果i2 < j2;
那么x-(i2-i1) <= y - (j2-j1)
=>x-i2+i1 <= y - j2 + j1
=>x+i1 <= y + j1 + (i2-j2)
因为i2-j2 < 0,
所以x1+i1 < y + j1,
(如果之前是 i2 > j2 , 那么结果刚好相反,反正是不能取 = )所以按照x+id排序,如果出现相等的情况,那 么不可能有解,如果不出现负数,也没有相等的数,那么排完序的x-(id2-id1)就是每个点的值,输出即可。
代码:
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#define MAX 200007using namespace std;struct Node{ int x; int id; bool operator < ( const Node& a ) const { return x+id < a.x+a.id; }}p[MAX];int n;int main ( ){ while ( ~scanf ( "%d" , &n ) ) { bool flag = true; for ( int i = 0 ; i < n ; i++ ) { scanf ( "%d" , &p[i].x ); p[i].id = i; } sort ( p , p+n ); for ( int i = 1 ; i < n ; i++ ) if ( p[i-1].x + p[i-1].id == p[i].x + p[i].id ) flag = false; for ( int i = 0 ; i < n ; i++ ) { p[i].x = p[i].x - ( i-p[i].id ); if ( p[i].x < 0 ) flag = false; } if ( !flag ) { puts (":("); continue; } printf ( "%d" , p[0].x ); for ( int i = 1 ; i < n ; i++ ) printf ( " %d" , p[i].x ); puts ( "" ); }}
0 0
- 贪心——Happy Line (CodeForces 549G)
- codeforces 549G G. Happy Line(贪心)
- Codeforces 549G. Happy Line 贪心
- Codeforces 549G Happy Line [贪心] [数学]
- 【Codeforces 549G】【贪心】Happy Line
- codeforces #549G Happy Line
- Looksery Cup 2015 G. Happy Line
- Looksery Cup 2015-G. Happy Line
- 贪心算法—Problem G
- CodeForces 698A—Vacations(贪心)
- poj3617Best Cow Line(贪心)
- Best Cow Line (贪心)
- POJ 3617 Best Cow Line——贪心
- codeforces 808C——Tea Party(贪心)
- codeforces 888G.Xor-MST(01字典树+贪心+最小异或生成树)
- 【codeforces #3】A——BFS+贪心
- Codeforces —— 358A Dima and Continuous Line
- LeetCode202—Happy Number
- Can't adjust the time of day: Invalid argument
- 企业SEO:你要排名还是要收入?
- OpenGLES Android篇零基础系列(三):OpenGL ES的渲染管道及VertexShader与FragmentShader
- VS2015不能单步调试
- Android_开源框架_AndroidUniversalImageLoader网络图片加载
- 贪心——Happy Line (CodeForces 549G)
- websocket 心跳包重连
- js获得昨天、今天、明天等时间
- debug and release
- halcon各章节归纳
- yii2权限管理
- LeetCode 371 Sum of Two Intergers
- Spring+Websocket实现消息的推送
- Selenium多层级的iframe中元素的定位