题目链接: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. ZBrush中2.5D笔刷

    ZBrush®是一个数字雕刻和3维建模软件,它不仅有着强大的3D雕刻功能,对于2.5D笔刷的应用也毫不逊色.本文主要讲解2.5D笔刷的一些使用方法,2.5D笔刷是针对贴图绘画的增效画笔工具和其他一些工 ...

  2. LeetCode Golang 3. 无重复字符的最长子串

    3. 无重复字符的最长子串 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串 ...

  3. css鼠标滑过出现文字效果

    模仿淘宝上鼠标移动到商品图片时,出现的文字效果. 1.效果图                                鼠标移动到粉红色的区域,则出现黄色部分. 2.代码 <!DOCTYPE ...

  4. Cookie和Session有什么区别

    1. 由于HTTP协议是无状态的协议,所以服务端需要记录用户的状态时,就需要用某种机制来识别具体的用户,这个机制就是Session.   典型的场景比如购物车,当你点击下单按钮时,由于HTTP协议无状 ...

  5. debian系统包管理工具aptitude

    注意:aptitude与 apt-get 一样,是 Debian 及其衍生系统中功能极其强大的包管理工具.与 apt-get 不同的是,aptitude在处理依赖问题上更佳一些.举例来说,aptitu ...

  6. 树形dp复习 树上依赖背包问题

    选课 今天又看了一下这道题,竟然AC不了了 自己的学习效率有点低下 要明白本质,搞透彻 #include<bits/stdc++.h> #define REP(i, a, b) for(r ...

  7. ASP.NET-属性与过滤器

    目的:在调用操作之前或者之后执行特定的逻辑代码 系统定义: 1.日志记录 2.防图像盗链  3.爬虫 4.本地化,用于设定区域设置 5.动态操作,用于将操作注入到控制器当中 用来过滤HTTP请求 高级 ...

  8. COGS——T 1175. [顾研NOIP] 旅游电车

    http://www.cogs.pro/cogs/problem/problem.php?pid=1175 ★★☆   输入文件:buss.in   输出文件:buss.out   简单对比时间限制: ...

  9. MongoDB创建\更新\删除文档操作

     一.插入\创建文档 --当插入一个不存在的文档时,会自己主动创建一个文档 [root@racdb ~]# mongo MongoDB shell version: 2.4.14 connecti ...

  10. MySQL高可用系列之MHA(二)

    一.參数说明 MHA提供了一系列配置參数.深入理解每一个參数的详细含义,对优化配置.合理使用MHA非常重要.非常多高可用性也都是通过合理配置一些參数而实现的. MHA包含例如以下配置參数,分别说明例如 ...