CF 1008C Reorder the Array
You are given an array of integers. Vasya can permute (change order) its integers. He wants to do it so that as many as possible integers will become on a place where a smaller integer used to stand. Help Vasya find the maximal number of such integers.
For instance, if we are given an array [10,20,30,40][10,20,30,40], we can permute it so that it becomes [20,40,10,30][20,40,10,30]. Then on the first and the second positions the integers became larger (20>1020>10, 40>2040>20) and did not on the third and the fourth, so for this permutation, the number that Vasya wants to maximize equals 22. Read the note for the first example, there is one more demonstrative test case.
Help Vasya to permute integers in such way that the number of positions in a new array, where integers are greater than in the original one, is maximal.
Input
The first line contains a single integer nn (1≤n≤1051≤n≤105) — the length of the array.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the elements of the array.
Output
Print a single integer — the maximal number of the array's elements which after a permutation will stand on the position where a smaller element stood in the initial array.
Examples
7
10 1 1 1 5 5 3
4
5
1 1 1 1 1
0
Note
In the first sample, one of the best permutations is [1,5,5,3,10,1,1][1,5,5,3,10,1,1]. On the positions from second to fifth the elements became larger, so the answer for this permutation is 4.
In the second sample, there is no way to increase any element with a permutation, so the answer is 0.
给你一段序列,经过重新排序后,问最多有几个位置上的数比原位置上的数大了。
类似与田忌赛马,但是我并不会操作。
原理是这样的:
假设给定的序列是 1 1 2 3 5 5 5 7
那么,符合题意的方法是,抽出两个子序列:1 2 3 5 7 和 1 5 ;或者是 1 3 5 7 和 1 2 5 。
即找长串,找若干条,直到所给序列中的数都被选过,或无法再选。
方法有了,但是暴力实现的话会超时或者RT,不知道怎么实现,直到发现了网友们的一个极其巧妙的解法,但是那种算法的原理不太理解。。。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0);
const double e=exp();
const int N = ; int con[];
int num[];
bool cmp(int a,int b)
{
return a<b;
}
int main()
{
int n,i,p,j;
int flag,ans=;
scanf("%d",&n);
for(i=;i<=n;i++)
{
scanf("%d",&con[i]);
}
sort(con+,con+n+,cmp);
int head=;
for(i=;i<=n;i++) //巧妙的方法开始了
{
if(con[i]>con[head])
{
head++;
ans++;
}
}
printf("%d\n",ans);
return ;
}
CF 1008C Reorder the Array的更多相关文章
- A. Reorder the Array
You are given an array of integers. Vasya can permute (change order) its integers. He wants to do it ...
- CF1007A Reorder the Array 题解
To CF 这道题是排序贪心,将原序列排序后统计答案即可. 但是直接统计会超时,因为排序后具有单调性,所以可以进行一点优化,这样,便可以通过此题. 而这道题的优化在于单调性,因为 \(a[i+1]\) ...
- CF 295A Greg and Array (两次建树,区间更新,单点查询)
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm& ...
- cf D. Levko and Array
http://codeforces.com/contest/361/problem/D 用二分搜索相邻两个数的差的绝对值,然后用dp记录数改变的次数.dp[i]表示在i之前改变的次数,如果|a[i]- ...
- cf C. Levko and Array Recovery
http://codeforces.com/contest/361/problem/C 这道题倒着一次,然后正着一次,在正着的一次的时候判断合不合法就可以. #include <cstdio&g ...
- [CF 295A]Grag and Array[差分数列]
题意: 有数列a[ ]; 操作op[ ] = { l, r, d }; 询问q[ ] = { x, y }; 操作表示对a的[ l, r ] 区间上每个数增加d; 询问表示执行[ x, y ]之间的o ...
- Codeforces Round #497 (Div. 2) C. Reorder the Array
Bryce1010模板 http://codeforces.com/contest/1008/problems #include <bits/stdc++.h> using namespa ...
- CodeForces-1007A Reorder the Array 贪心 田忌赛马
题目链接:https://cn.vjudge.net/problem/CodeForces-1007A 题意 给个数组,元素的位置可以任意调换 问调换后的元素比此位置上的原元素大的元素个数最大多少 思 ...
- [CF 718C] Sasha and Array
传送门 Solution 用线段树维护矩阵 第一个操作相当于区间乘 第二个操作相当于区间求和 Code #include<bits/stdc++.h> #define ll long l ...
随机推荐
- swusec的构想,顺便送开学福利——校园网一号多登录演示
前言: 我不是什么大牛,我只想通过我的努力,打造swu网络安全爱好者的圈子.期待你加入. swusec是什么? swusec (SouthWestUniversity SecurityTeam),西南 ...
- Construct BST from given preorder traversal
Given preorder traversal of a binary search tree, construct the BST. For example, if the given trave ...
- windows结束端口对应的进程
netstat -ano |findstr " //window查看端口占用 taskkill /pid 54828taskkill /F /pid 54828 //强制中止 转载请注明博客 ...
- 【Python】python 2 map() reduce()
利用map()函数,把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字.输入:['adam', 'LISA', 'barT'],输出:['Adam', 'Lisa', 'Bart']. ...
- java super
用法: 1.子类构造器里面默认调用父类构造器 2.调用父类的属性,方法
- HDU 6184 Counting Stars
Problem Description Little A is an astronomy lover, and he has found that the sky was so beautiful!S ...
- P4433 [COCI2009-2010#1] ALADIN
题目描述 给你 n 个盒子,有 q 个操作,操作有两种: 第一种操作输入格式为"1 L R A B",表示将编号为L到R的盒子里的石头数量变为(X−L+1)×A mod B,其中 ...
- VS2013 UML 如何复制文件
如:复制活动图,文件复制了显示不了 正确做法:新建活动图,打开源活动图,全选,复制,在新建的活动图粘贴,以此实现复制
- 【刷题】洛谷 P3613 睡觉困难综合征
题目背景 刚立完Flag我就挂了WC和THUWC... 时间限制0.5s,空间限制128MB 因为Claris大佬帮助一周目由乃通过了Deus的题,所以一周目的由乃前往二周目世界找雪辉去了 由于二周目 ...
- 详细MATLAB 中BP神经网络算法的实现
MATLAB 中BP神经网络算法的实现 BP神经网络算法提供了一种普遍并且实用的方法从样例中学习值为实数.离散值或者向量的函数,这里就简单介绍一下如何用MATLAB编程实现该算法. 具体步骤 这里 ...