题目链接: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. 从有限状态机的角度去理解Knuth-Morris-Pratt Algorithm(又叫KMP算法)

    转载请加上:http://www.cnblogs.com/courtier/p/4273193.html 在开始讲这个文章前的唠叨话: 1:首先,在阅读此篇文章之前,你至少要了解过,什么是有限状态机, ...

  2. 【转】FLV视频封装格式详解

    Overview Flash Video(简称FLV),是一种流行的网络格式.目前国内外大部分视频分享网站都是采用的这种格式. File Structure 从整个文件上开看,FLV是由The FLV ...

  3. struts配置,略记

    <!-- <listener> <listener-class>org.springframework.web.context.ContextLoaderListener ...

  4. javaweb文件下载

    最近搞了一下struts文件上传下载了,一个是通过struts自带的类实现的下载方法,一个是通用的下载方法: struts实现: FileDownloadAction.java package com ...

  5. vmplayer中的fedora20无法进入图形界面

    新装了台fedora20的虚拟机,安装升级了一些东西后.启动时过了fedora的logo画面后就是黑屏. 也没提示不论什么错误,好在shell还能进去.查看/var/log/anaconda/sysl ...

  6. Android字数限制的EditText实现方案研究

    在应用开发中,有时需要实现有字数限制的EditText,首先来分析下市面上存在的类似实现方案吧,好有个感性的认识. [方案一:腾讯微博] 每个中文字符算一个字数,每两个英文字符算一个字数,当用户输入内 ...

  7. sqlserver中的rowversion

    rowversion 公开数据库中自动生成的唯一二进制数字的数据类型.rowversion通常用作给表行加版本戳的机制,存储大小为8字节.rowversion数据类型只是递增的数字,不保留日期或时间. ...

  8. BootStrap-validator 使用记录(JAVA SpringMVC实现)

    BootStrap 是一个强大的前面框架,它用优雅的方式解决了网页问题.最近正在使用其开发网站的表单验证,一点体会记录如下: 注:本文中借鉴了博客Franson 的文章<使用bootstrapv ...

  9. 微软HoloLens虚拟现实可以开发了。

    1.microsoft-hololens-now-available-to-developers 2.http://www.microsoft.com/microsoft-hololens/en-us ...

  10. vi 快捷键【转】【weber整理必出精品】

    光标的移动 命令 光标移动 h或^h 向左移一个字符 j或^j或^n 向下移一行 k或^p 向上移一行 l或空格 向右移一个字符 G 移到文件的最后一行 nG 移到文件的第n行 w 移到下一个字的开头 ...