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

题目链接

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

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

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

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