codeforces #375 C. Polycarp at the Radio (贪心)

题目链接

题意:给出n,m,n个数,对其中的一些数进行修改,要求1..m中出现次数最少的数最大,输出这个最少的数最大是多少,以及修改的次数。

思路:最小的数最多出现n/m次。

竟然因为排序后下标变乱不知所措40分钟。。。我也是醉了。。。

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年10月03日 星期一 19时29分52秒
 4File Name :code/cf/#375/C.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <deque>
15#include <map>
16#include <string>
17#include <cmath>
18#include <cstdlib>
19#include <bitset>
20#define fst first
21#define sec second
22#define lson l,m,rt<<1
23#define rson m+1,r,rt<<1|1
24#define ms(a,x) memset(a,x,sizeof(a))
25typedef long long LL;
26#define pi pair < int ,int >
27#define MP make_pair
28
29using namespace std;
30const double eps = 1E-8;
31const int dx4[4]={1,0,0,-1};
32const int dy4[4]={0,-1,1,0};
33const int inf = 0x3f3f3f3f;
34const int N=2005;
35int n,m;
36int a[N];
37struct node
38{
39    int id;
40    int val;
41    bool operator < (node b)const
42    {
43	return val<b.val;
44    }
45}cnt[N];
46map<int,int>mp;
47int main()
48{
49	#ifndef  ONLINE_JUDGE 
50	freopen("code/in.txt","r",stdin);
51  #endif
52	cin>>n>>m;
53	ms(cnt,0);
54	int C = 0 ;
55	for ( int i = 1 ; i <= n ; i++)
56	{
57	    cin>>a[i];
58	    if (a[i]<=m)
59	    {
60		cnt[a[i]].val++;
61	    }else C++;
62	}
63	for ( int i = 1 ; i <= m ; i++) cnt[i].id = i ;
64	int num = n/m;
65	sort(cnt+1,cnt+m+1);
66	for ( int i = 1 ; i <= m ; i++) mp[cnt[i].id] = i;
67	int head = 1;
68	int ans = 0 ;
69	for ( int i = 1 ; i <= n ; i++)
70	{
71	    if (a[i]>m&&C>n-m*num)
72	    {
73		ans++;
74		C--;
75		cnt[head].val++;
76		a[i] = cnt[head].id;
77		if (cnt[head].val==num) head++;
78	    }else if (cnt[mp[a[i]]].val>num&&cnt[head].val<num)
79		    {
80			ans++;
81			cnt[mp[a[i]]].val--;
82			cnt[head].val++;
83			a[i] = cnt[head].id;
84			if (cnt[head].val==num) head++;
85
86		    }
87	}
88	printf("%d %d\n",num,ans);
89	for ( int i = 1; i <= n ; i++) printf("%d ",a[i]);
90
91  #ifndef ONLINE_JUDGE  
92  fclose(stdin);
93  #endif
94    return 0;
95}