BeautifulPaintings
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit Status
Description
Thereare n picturesdelivered for the new exhibition. The i-thpainting has beauty ai.We know that a visitor becomes happy every time he passes from a painting to amore beautiful one.
Weare allowed to arranged pictures in any order. What is the maximum possiblenumber of times the visitor may become happy while passing all pictures fromfirst to last? In other words, we are allowed to rearrange elements of a inany order. What is the maximum possible number of indices i (1 ≤ i ≤ n - 1),such that ai + 1 > ai.
Input
Thefirst line of the input contains integer n (1 ≤ n ≤ 1000) —the number of painting.
Thesecond line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000),where ai meansthe beauty of the i-thpainting.
Output
Printone integer — the maximum possible number of neighbouring pairs, such that ai + 1 > ai,after the optimal rearrangement.
Sample Input
Input
5
20 30 10 50 40
Output
4
Input
4
200 100 100 200
Output
2
题意:给你一组数字,问能够组成多少对a[i+1]>a[i]
思路:引用一个标记数组,对已经参与比较的a[i]进行标记,最后统计即可。
参考代码:
/*=============================AC情况===============================*/
/*题目网址: */
/*时间: */
/*心得: */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#define G 1008
using namespace std;
int main() {
int n,a[G],vis[G];
int ans;
while(scanf("%d",&n)!=EOF) {
ans=0;
memset(vis,false,sizeof(vis));
for(int j=0; j<n; j++)
scanf("%d",&a[j]);
sort(a,a+n);
for(int j=0; j<n-1; j++) {
for(int k=j+1; k<=n-1; k++) {
if(vis[j+1]==false&&a[j+1]>a[j]) {
vis[j+1]=true;
ans++;
break;
}
}
}
printf("%d\n",ans);
}
return 0;
}
/*********************************测试数据*********************************
**************************************************************************/
一个for 搞不定的代码:
/*=============================AC情况===============================*/
/*题目网址: */
/*时间: */
/*心得: */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#define G 1008
using namespace std;
int main() {
int n,a[G],vis[G];
int ans;
while(scanf("%d",&n)!=EOF) {
ans=0;
memset(vis,false,sizeof(vis));
for(int j=0; j<n; j++)
scanf("%d",&a[j]);
sort(a,a+n);
for(int j=0; j<n-1; j++) {
if(vis[j+1]==false&&a[j+1]>a[j]) {
vis[j+1]=true;
ans++;
}
}
printf("%d\n",ans);
}
return 0;
}
/*********************************测试数据*********************************
**************************************************************************/