题目链接:https://cn.vjudge.net/problem/CodeForces-1007A

题意

给个数组,元素的位置可以任意调换

问调换后的元素比此位置上的原元素大的元素个数最大多少

思路

一开始想了半天,最后想出来田忌赛马

田忌赛马经典题,一共5种可能性,详见HDU-1052 Tian Ji -- The Horse Racing 贪心 考虑特殊位置(首尾元素)的讨论

提交过程

AC

代码

#include <cstdio>
#include <algorithm>
using namespace std;
int main(void){
int n, a[100000+20], b[100000+20]; scanf("%d", &n);
for (int i=0; i<n; i++) scanf("%d", &a[i]), b[i]=a[i];
sort(a, a+n); sort(b, b+n); int alast=n-1, head=0, last=n-1, i=0, cnt=0;
while (i<=alast){
if (b[i]<a[head]){
head++; cnt++; i++;
}else if (b[i]>a[head]){
head++; alast--;
}else if (b[i]==a[head]){
if (b[alast]<a[last]){
alast--; last--; cnt++;
}else if (b[alast]>a[last]){
alast--; head++;
}else{
if (a[head]<b[alast]);
else if (a[head]>b[alast]) cnt++;
alast--; head++;
}
}
}printf("%d\n", cnt); return 0;
}
Time Memory Length Lang Submitted
62ms 604kB 812 GNU G++ 5.1.0 2018-07-23 19:41:44

CodeForces-1007A Reorder the Array 贪心 田忌赛马的更多相关文章

  1. Codeforces 442C Artem and Array(stack+贪婪)

    题目连接:Codeforces 442C Artem and Array 题目大意:给出一个数组,每次删除一个数.删除一个数的得分为两边数的最小值,假设左右有一边不存在则算作0分. 问最大得分是多少. ...

  2. Codeforces Round #504 D. Array Restoration

    Codeforces Round #504 D. Array Restoration 题目描述:有一个长度为\(n\)的序列\(a\),有\(q\)次操作,第\(i\)次选择一个区间,将区间里的数全部 ...

  3. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  4. [Codeforces 1214A]Optimal Currency Exchange(贪心)

    [Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...

  5. Codeforces 754A Lesha and array splitting(简单贪心)

    A. Lesha and array splitting time limit per test:2 seconds memory limit per test:256 megabytes input ...

  6. Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心

    D. Maxim and Array 题目连接: http://codeforces.com/contest/721/problem/D Description Recently Maxim has ...

  7. Codeforces Round #258 (Div. 2) . Sort the Array 贪心

    B. Sort the Array 题目连接: http://codeforces.com/contest/451/problem/B Description Being a programmer, ...

  8. CodeForces ---596B--Wilbur and Array(贪心模拟)

    Wilbur and Array Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Su ...

  9. codeforces 442C C. Artem and Array(贪心)

    题目链接: C. Artem and Array time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

随机推荐

  1. oc与swift比较

    swift试是用语言层面的雕虫小技和oc的机制大道进行pk. 从整体上来说,oc是一个更加优秀的语言.

  2. laravel报错:MassAssignmentException

    报这种错误是因为没有设置白名单或者黑名单.在使用fill填充时,需要设置白/黑名单. $model->fill($params);return $model->save(); 找到对应的m ...

  3. WePy--记录使用过程中的一些坑

    讲真, 官方文档还是要好好看, 都是细节啊... 1- 使用wepy框架中封装好的小程序api  <wepy 对小程序的API进行Promise处理>   ①: 首先需要声明 开启使用we ...

  4. Java web课程学习之会话(Session)

    Session会话   l web应用中的会话是指一个客户端浏览器与web服务器之间连续发生一系列请求和响应过程 l web应用的会话状态是指web服务器与浏览器在会话过程中产生的状态信息,借助会话状 ...

  5. HDU 1465 不容易系列之一( 错排水题 )

    链接:传送门 思路:错排模板题,水题是非常浪费时间的 /************************************************************************ ...

  6. SparkSql初级编程实践

    1.Spark SQL 基本操作将下列 JSON 格式数据复制到 Linux 系统中,并保存命名为 employee.json.{ "id":1 , "name" ...

  7. 使用InstelliJ IDEA创建Spring MVC应用程序

    环境版本 Windows 8.1 IDE:InstelliJ IDEA 13    Spring:Spring 4.1.1 & Spring MVC 4.1.1    WebLogic 10. ...

  8. 2015 Multi-University Training Contest 1 hdu 5296 Annoying problem

    Annoying problem Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  9. jQuery判断浏览器类型和版本

    jquery 判断浏览器类型 例:   if($.browser.msie) { alert("这是一个IE浏览器"); }else if($.browser.opera) { a ...

  10. 疯狂Java学习笔记(70)-----------挚爱Java

    与大家分享! 挚爱Java 10个使用Java最广泛的现实领域 写好Java代码的30条经验总结 Java字符串的substring真的会引起内存泄露么? Java内存的原型及工作原理深度剖析 Jav ...