【CF 549G Happy Line】排序
题目链接:http://codeforces.com/problemset/problem/549/G
题意:给定一个n个元素的整数序列a[], 任意时刻对于任一对相邻元素a[i-1]、 a[i],若a[i-1] < a[i] 则要依次执行如下两个操作:
1. a[i-1]--, a[i]++;
2. 交换a[i-1]和a[i]的位置。
经过若干次1、2操作后,若能使整个序列变成非降的,则输出最终的序列;否则输出":("。
数据范围:n 属于 [1, 2*10^5], a[i] 属于[0, 10^9]
思路:首先想到交换排序,但n 在10^5所以n^2的排序不可取。后来模拟快排的过程推出了样例,如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define CLEAR(A, X) memset(A, X, sizeof(A))
#define REP(N) for(int i=0; i<(N); i++)
#define REPE(N) for(int i=1; i<=(N); i++)
#define FREAD(FN) freopen((FN), "r", stdin)
#define pb(a) push_back(a)
#define pf() pop_front()
using namespace std; const int MAX_N = ;
int n;
int a[MAX_N];
int flag;
void partition(int s, int e){
if(s == e) return ;
int i = s, j = e - ;
//printf("i %d j %d*******\n", i, j);
while(i < j){
while(i < j && a[j] >= a[i]) j--;
if(i < j){
a[j] += j - i;
if(a[i] == a[j]){
flag = ;
return ;
}
a[i] -= j - i;
swap(a[i], a[j]);
i++;
} while(i < j && a[i] <= a[j]) i++;
if(i < j){
a[i] -= j - i;
if(a[i] == a[j]){
flag = ;
return ;
}
a[j] += j - i;
swap(a[i], a[j]);
j--;
}
// for(int k=0; k<n; k++) printf("%d ", a[k]);
// printf("\n");
}//i == j
partition(s, i);
partition(i+, e);
} int main()
{
scanf("%d", &n);
REP(n) scanf("%d", &a[i]);
flag = ;
partition(, n);
if(flag) printf(":(\n");
else{
REP(n) printf("%d ", a[i]);
printf("\n");
}
return ;
}
quickSort,i, j相对往中间走
但对于第六个test(
5
15 5 8 6 3
)得到的结果是错的,尝试改用i, j 指针同方向走来构造轴点,如下,但还是构造不出正确的结果。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define CLEAR(A, X) memset(A, X, sizeof(A))
#define REP(N) for(int i=0; i<(N); i++)
#define REPE(N) for(int i=1; i<=(N); i++)
#define FREAD(FN) freopen((FN), "r", stdin)
#define pb(a) push_back(a)
#define pf() pop_front()
using namespace std; const int MAX_N = ;
int n;
int a[MAX_N];
int flag;
void partition(int s, int e){
if(s == e) return ;
int i = s, j = s + ;
int cur = s;
//printf("i %d j %d*******\n", i, j);
while(i < e && j < e){
while(i < j && j < e && a[j] >= a[i]) j++;
if(i < j && j < e){
a[j] += j - i;
if(a[i] == a[j]){
flag = ;
return ;
}
a[i] -= j - i;
swap(a[i], a[j]);
cur = j;
i = j + ;
} while(j < i && i < e && a[i] >= a[j]) i++;
if(j < i && i < e){
a[j] -= i - j;
if(a[i] == a[j]){
flag = ;
return ;
}
a[i] += i - j;
swap(a[i], a[j]);
cur = j;
j = i + ;
}
// for(int k=0; k<n; k++) printf("%d ", a[k]);
// printf("\n");
}//i == j
partition(s, cur);
partition(cur+, e);
} int main()
{
scanf("%d", &n);
REP(n) scanf("%d", &a[i]);
flag = ;
partition(, n);
if(flag) printf(":(\n");
else{
REP(n) printf("%d ", a[i]);
printf("\n");
}
return ;
}
quickSort,i, j从左往右走
于是看题解了,以下是题解的思路,思想仍是排序,(虽然tag上写了greedy,但我没想明白哪里用到了贪心策略):
由于swap(a[i-1], a[i])时,向左的a[i]在数值上"收益"了1,向右的a[i-1]在数值上"消耗"了1,现在把由交换产生的“收益/消耗”变化量从a[i]的原始数值中分离开来。
如左图,每一列对应一个位置 i ,其中黑色的“台阶”加上黄色的“塔”为原始的a[i]值,现在规定从左到右台阶的高度从n 均匀递减到 1, 记黄色的塔高 b[i] = 原始高度a[i] - 台阶高度(n - i)(i从0起始);这样每个a[i] 向左交换相当于上一个台阶,向右交换为下一个台阶,对应的塔高b[i]是不变的,如右图。所以我们只需计算出序列b[i]并把它排成非降序,然后再加上对应位置的台阶高度就是最终结果了。对于":("的情况,只需得到结果后扫描一遍检查是否确实非降序即可。


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define CLEAR(A, X) memset(A, X, sizeof(A))
#define REP(N) for(int i=0; i<(N); i++)
#define REPE(N) for(int i=1; i<=(N); i++)
#define FREAD(FN) freopen((FN), "r", stdin)
#define pb(a) push_back(a)
#define pf() pop_front()
using namespace std; const int MAX_N = ;
int n;
int a[MAX_N];
int flag; int main()
{
scanf("%d", &n);
REP(n) scanf("%d", &a[i]);
flag = ;
REP(n) a[i] -= n - i;
sort(a, a+n);
a[] += n;
for(int i=; i<n; i++){
a[i] += n - i;
if(a[i] < a[i-]){
flag = ;
break;
}
} if(flag) printf(":(\n");
else{
REP(n) printf("%d ", a[i]);
printf("\n");
}
return ;
}
【CF 549G Happy Line】排序的更多相关文章
- Codeforces 549G Happy Line[问题转换 sort]
G. Happy Line time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- CodeForces 549G Happy Line
Happy Line Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit ...
- Codeforces 549G. Happy Line 馋
非常有趣的贪婪: Let's reformulate the condition in terms of a certain height the towers, which will be on t ...
- CF 915 D 拓扑排序
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; const int mod = 14285 ...
- [转载]config文件的一个很好的实现
以下是转载于网上的一个很好的config文件的实现,留存以备案 //Config.h #pragma once #include <string> #include <map> ...
- [转]C++编写Config类读取配置文件
//Config.h #pragma once #include <string> #include <map> #include <iostream> #incl ...
- Python高级编程–正则表达式(习题)
原文:http://start2join.me/python-regex-answer-20141030/ ############################################## ...
- C++编写Config类读取配置文件
老外写的一段代码,在Server中编写这个类读取配置文件比较实用 //Config.h #pragma once #include <string> #include <map> ...
- Pivotal Cloud Foundry学习笔记(1)
PCF是一个PAAS平台 注册PCF账号 https://account.run.pivotal.io/sign-up 安装cf CLI 访问 https://console.run.pivotal. ...
随机推荐
- [HEOI 2013 day2] 钙铁锌硒维生素 (线性代数,二分图匹配)
题目大意 给定两个n阶方阵,方阵B的行i能匹配方阵A的行j当且仅当在第一个方阵中用行向量i替换行向量j后,第一个方阵满秩,显然这是个二分图匹配问题,问是否存在完美匹配,如果存在,还要输出字典序最小的方 ...
- ubuntu14.04 cocos2d-x-3.6 glfw编译出错解决方案
lib/libcocos2d.a(CCGLViewImpl-desktop.cpp.o): In function `cocos2d::GLViewImpl::GLViewImpl()': /home ...
- HTML5新增的一些属性和功能之八——web Worker
Web Workers 为什么用web workers? 浏览器的原理中决定了页面打开只有一个主线程--UI渲染线程,如果线程中有耗时的程序(js)会阻塞线程,使得页面中其他的UI无法渲染,我们一般把 ...
- 贪心-hdu-1789-Doing Homework again
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1789 题目意思: 有n个作业,每个作业有一个截止日期,每个作业如果超过截止日期完成的时候有一个惩罚值 ...
- Android开发之DatePickerDialog与TimePickerDialog的功能和使用方法具体解释
DatePickerDialog与TimePickerDialog的功能比較简单,使用方法也非常easy.仅仅要以下两步就可以. Ø 通过newkeyword创建DatePickerDialog.T ...
- IOS详解TableView——选项抽屉(天猫商品列表)
在之前的有篇文章讲述了利用HeaderView来写类似QQ好友列表的表视图. 这里写的天猫抽屉其实也可以用该方法实现,具体到细节每个人也有所不同.这里采用的是点击cell对cell进行运动处理以展开“ ...
- eclipse注释模板修改
http://swiftlet.net/archives/1199 以下为模板文件 <?xml version="1.0" encoding="UTF-8" ...
- windows服务器性能监控工具、方法及关键指标
原文:http://www.cnblogs.com/liulun/p/3543777.html 监控方法 推荐使用windows自带的“性能监视器”(老版本的windows叫性能计数器)来监控服务器的 ...
- Js弹性漂浮广告代码
<html><head><meta http-equiv="Content-Type" content="text/html; charse ...
- android*API19
android android.accessibilityservice android.accounts android.animation android.app android.app.adm ...