CodeForces 489A (瞎搞) SwapSort
题意:
给n个整数(可能有重复),输出一个不超过n次交换的方案,使得经过这n次交换后,整个序列正好是非递减的。
分析:
首先说题解给的算法。
从左到右扫一遍,交换第i个数和它后面最小的那个数。
代码看起来大概是这个样子的:
for (int i = ; i < n; i++)
{
int j = i;
for (int t = i; t < n; t++)
if (a[j] > a[t])
j = t;
if (i != j)
answer.push_back(make_pair(i, j));
swap(a[i], a[j]);
}
代码君
当时看到这题一直没有明确的思路,于是开始乱搞。 =_=||
首先排了下序,然后从左到右逐个对比原序列和排序后的序列,如果有不一样的,就从原序列的后面把这个“正确”的数找出来交换,直到两个序列完全相同。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std; const int maxn = + ;
int a[maxn], b[maxn], ans[maxn][]; int main()
{
int n;
while(scanf("%d", &n) == && n){
for(int i = ; i < n; ++i)
{
scanf("%d", &a[i]);
b[i] = a[i];
}
sort(b, b + n); int cnt = ;
bool flag; //标记是否发生过交换
while()
{
flag = false;
for(int i = ; i < n; ++i)
{
if(a[i] != b[i])
{
flag = true;
for(int j = n-; j >= ; --j)
if(a[j] == b[i] && i != j)
{
swap(a[i], a[j]);
ans[cnt][] = i;
ans[cnt++][] = j;
break;
}
}
}
if(!flag) break;
}
printf("%d\n", cnt);
for(int i = ; i < cnt; ++i) printf("%d %d\n", ans[i][], ans[i][]);
} return ;
}
代码君
CodeForces 489A (瞎搞) SwapSort的更多相关文章
- Codeforces Gym 100610 Problem H. Horrible Truth 瞎搞
Problem H. Horrible Truth Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1006 ...
- 8VC Venture Cup 2016 - Elimination Round B. Cards 瞎搞
B. Cards 题目连接: http://www.codeforces.com/contest/626/problem/B Description Catherine has a deck of n ...
- URAL 1203. Scientific Conference(瞎搞)
题目链接 本来觉得这不是经典的贪心吗..果断水一次,wa了,看了看discuss,发现貌似不好水,土土的DP了一下,复杂度很高了,又T了...然后想想单调队列,二分什么的...不好往上加,直接搞了标记 ...
- B. Salty Fish Go! -期望题(瞎搞题)
链接:https://www.nowcoder.com/acm/contest/104/B来源:牛客网 题意:A few days ago, WRD was playing a small game ...
- HDU5532 Almost Sorted Array(最长上升子序列 or 瞎搞个做差的数组)
题目链接:点我 题意:给定一个序列,询问是否能删除一个数让它成为非递减或者非递增的序列. 比如说 删除后的序列是1 3 3 5 或者5 3 3 1 或者1 3 5 或者5 3 1 都可以.只要满足删掉 ...
- TOJ3097: 单词后缀 (字典树 or map瞎搞)
传送门 (<---可以点击的~) 时间限制(普通/Java):1000MS/3000MS 内存限制:65536KByte 描述 有些英语单词后缀都是一样的,现在我们需要从给定的一堆单词里 ...
- ubuntu--基础环境瞎搞集合
安装ubuntu系统后有很多东西需要自己瞎搞一下,这里把一些瞎搞的过程记录在这里,方便以后重新装系统后重新配置. 一.安装. 可以在windows下制作启动盘(软碟通),然后开机u盘启动即可安装,预留 ...
- Codeforces631C【栈维护+瞎搞】
题意: 百度. 思路: 如果该查询的R比前面的所有都大,那么前面所有都失效. 那么我先预处理出这些有效的. 那最坏的情况不就是栈里面元素(R)很多 n,n-1,n-2,n-3,n-4而且都是相反排序的 ...
- BZOJ 4236: JOIOJI map瞎搞
分别记录J,O,I,的个数 cnt[char][i] 表示处理到第i位,char的个数 显然当且仅当 cnt[J][i] - cnt[O][i] == cnt[J][j-1] - cnt[O][j-1 ...
随机推荐
- Educational Codeforces Round 11 C. Hard Process 前缀和+二分
题目链接: http://codeforces.com/contest/660/problem/C 题意: 将最多k个0变成1,使得连续的1的个数最大 题解: 二分连续的1的个数x.用前缀和判断区间[ ...
- HTML5网页制作教程:HTML5块级链接
网页制作Webjx文章简介:Web 标准中处处充满了打脸行为,这条规则现在已经失效了!在那篇文章发布一个月后,HTML5doctor 发表了 “Block-level” links in HTML5, ...
- Application、Session、Cookie、ViewState的特性
http://blog.csdn.net/zyw_anquan/article/details/7664132 Application的特性: 存储的物理位置:服务器端内存. 存储的类型限制:任意 ...
- [原]android不支持命名的semaphore
之前sem_open在iOS上, 创建命名的semaphore没有问题 (iOS不支持匿名的semaphore), 但是现在Android平台的sem_open时候报错,返回ENOSYS. 命名的se ...
- Implicitly Typed Local Variables
Implicitly Typed Local Variables It happens time and time again: I’ll be at a game jam, mentoring st ...
- hashtable用法
import java.util.Hashtable; public class HashTable { public static void main (String[] args) { Hasht ...
- SDUT1061Binomial Showdown(组合数)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1061 题意 : 表示这个题的英文没看懂,就看懂 ...
- 220 DIV2 B. Inna and Nine
220 DIV2 B. Inna and Nine input 369727 output 2 input 123456789987654321 output 1 题意:比如例子1:369727--& ...
- lintcode:Number of Islands 岛屿的个数
题目: 岛屿的个数 给一个01矩阵,求不同的岛屿的个数. 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右为相邻. 样例 在矩阵: [ [1, 1, 0, 0, 0], ...
- Project Euler 97 :Large non-Mersenne prime 非梅森大素数
Large non-Mersenne prime The first known prime found to exceed one million digits was discovered in ...