题意:给出n,m,n个数,对其中的一些数进行修改,要求1..m中出现次数最少的数最大,输出这个最少的数最大是多少,以及修改的次数。
思路:最小的数最多出现n/m次。
竟然因为排序后下标变乱不知所措40分钟。。。我也是醉了。。。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
/* *********************************************** Author :111qqz Created Time :2016年10月03日 星期一 19时29分52秒 File Name :code/cf/#375/C.cpp ************************************************ */ #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <deque> #include <map> #include <string> #include <cmath> #include <cstdlib> #include <bitset> #define fst first #define sec second #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define ms(a,x) memset(a,x,sizeof(a)) typedef long long LL; #define pi pair < int ,int > #define MP make_pair using namespace std; const double eps = 1E-8; const int dx4[4]={1,0,0,-1}; const int dy4[4]={0,-1,1,0}; const int inf = 0x3f3f3f3f; const int N=2005; int n,m; int a[N]; struct node { int id; int val; bool operator < (node b)const { return val<b.val; } }cnt[N]; map<int,int>mp; int main() { #ifndef ONLINE_JUDGE freopen("code/in.txt","r",stdin); #endif cin>>n>>m; ms(cnt,0); int C = 0 ; for ( int i = 1 ; i <= n ; i++) { cin>>a[i]; if (a[i]<=m) { cnt[a[i]].val++; }else C++; } for ( int i = 1 ; i <= m ; i++) cnt[i].id = i ; int num = n/m; sort(cnt+1,cnt+m+1); for ( int i = 1 ; i <= m ; i++) mp[cnt[i].id] = i; int head = 1; int ans = 0 ; for ( int i = 1 ; i <= n ; i++) { if (a[i]>m&&C>n-m*num) { ans++; C--; cnt[head].val++; a[i] = cnt[head].id; if (cnt[head].val==num) head++; }else if (cnt[mp[a[i]]].val>num&&cnt[head].val<num) { ans++; cnt[mp[a[i]]].val--; cnt[head].val++; a[i] = cnt[head].id; if (cnt[head].val==num) head++; } } printf("%d %d\n",num,ans); for ( int i = 1; i <= n ; i++) printf("%d ",a[i]); #ifndef ONLINE_JUDGE fclose(stdin); #endif return 0; } |
说点什么
您将是第一位评论人!