题目链接: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】排序的更多相关文章

  1. Codeforces 549G Happy Line[问题转换 sort]

    G. Happy Line time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  2. CodeForces 549G Happy Line

    Happy Line Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit  ...

  3. Codeforces 549G. Happy Line 馋

    非常有趣的贪婪: Let's reformulate the condition in terms of a certain height the towers, which will be on t ...

  4. CF 915 D 拓扑排序

    #include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; const int mod = 14285 ...

  5. [转载]config文件的一个很好的实现

    以下是转载于网上的一个很好的config文件的实现,留存以备案 //Config.h #pragma once #include <string> #include <map> ...

  6. [转]C++编写Config类读取配置文件

    //Config.h #pragma once #include <string> #include <map> #include <iostream> #incl ...

  7. Python高级编程–正则表达式(习题)

    原文:http://start2join.me/python-regex-answer-20141030/ ############################################## ...

  8. C++编写Config类读取配置文件

    老外写的一段代码,在Server中编写这个类读取配置文件比较实用 //Config.h #pragma once #include <string> #include <map> ...

  9. Pivotal Cloud Foundry学习笔记(1)

    PCF是一个PAAS平台 注册PCF账号 https://account.run.pivotal.io/sign-up 安装cf CLI 访问 https://console.run.pivotal. ...

随机推荐

  1. eclipse3.7 安装maven插件与scm

    转自:http://blacksonny.iteye.com/blog/1900275 最近要使用maven进行开发,之前的eclipse3.7 使用一下两个地址安装好了插件,如下: maven插件 ...

  2. C# 二分查询

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. pyqt lineedit右边显示按钮效果

    from PyQt4 import QtGui, QtCore class ButtonLineEdit(QtGui.QLineEdit): buttonClicked = QtCore.pyqtSi ...

  4. MongoDB 的 MapReduce 大数据统计统计挖掘

    MongoDB虽然不像我们常用的mysql,sqlserver,oracle等关系型数据库有group by函数那样方便分组,但是MongoDB要实现分组也有3个办法: * Mongodb三种分组方式 ...

  5. compareTo简介

    compareTo()方法是用来比较字符串大小,该方法用来判断一个字符串是大于,等于还是小于另一个字符串.判断字符串大小的依据是根据他们在字典中的顺序决定的 语法 Str1.compareTo(Str ...

  6. java开发之基础篇2

    一.java开发环境的搭建 下载和安装jdk.版本自己看着办! 1 JAVA_HOME C:\Program Files\Java\jdk1.7.0_25 2 path C:\Program File ...

  7. Property与Attribute的区别

    Property属于面向对象的范畴----属性 Attribute则是编程语言文法层面的东西----特征          Property属于面向对象的范畴.在使用面向对象编程的时候,常常需要对客观 ...

  8. React-nwb的使用

    一.查看nwb的版本 nwb -v 二.创建一个react项目 nwb new react-app react-demo 三.启动项目 nwb serve

  9. silverlight visifire控件图表制作——silverlight 后台方法画图

    1.调用wcf 获取信息 private void svc_GetSingleChartDataCompleted(object sender, GetSingleChartDataCompleted ...

  10. (五)CodeMirror - 关于htmlmixed中包含script脚本

    最近发现个问题,场景如下: 当创建的mode类型为htmlmixed,且内容中包含javascript脚本,且是闭包立即执行: 如果内容是使用JQuery函数.html()插入到DOM中后再创建cod ...