Codeforces 723C.Polycarp at the Radio
来源:互联网 发布:手机变麦克风软件 编辑:程序博客网 时间:2024/05/30 23:10
题目:Polycarp at the Radio
题意:给定n个数,使得n个数都在1~m范围内,并且每个数的个数都等于或接近 n/m 个!
思路:1.将缺少的数都依次存放在一个数组里
2.遍历n个数,将不符合条件的换掉即可。
不符合条件:(1)大于m的数;(2)符合范围但个数大于n/m个数的元素。
代码:
#include <stdio.h>#include <stdlib.h>#include <string.h>#define INIT(x) memset(x,0,sizeof(x))int main(){ int i,n,m,a[2005],flag[2005]; while(scanf("%d%d",&n,&m)!=EOF) { INIT(flag);//初始化,用于累计每个元素出现的个数 for(i=0;i<n;i++) { scanf("%d",&a[i]); if(a[i] <= m)//将小于m的数计数 flag[a[i]]++; } int temp[2005],index = 0; for(i=1;i<=m;i++)//将缺少的数放入一个数组 { while(flag[i] < n/m) { temp[index++] = i; flag[i]++; } } int top = 0; for(i=0;i<n;i++) { if(top == index)//当所有缺少的元素全部放完跳出即可 break; if(a[i] > m)//将缺少的元素放到大于m的位置上 a[i] = temp[top++]; if(flag[a[i]] > n/m)//将缺少的元素放到符合元素的多余的位置上 { flag[a[i]]--; a[i] = temp[top++]; } } printf("%d %d\n",n/m,index); for(i=0;i<n;i++) printf("%d ",a[i]); printf("\n"); } return 0;}
0 0
- Codeforces 723 C. Polycarp at the Radio
- Codeforces 723C.Polycarp at the Radio
- 【23.48%】【codeforces 723C】Polycarp at the Radio
- codeforces 723C. Polycarp at the Radio [模拟]【杂类】
- CodeForces 723C Polycarp at the Radio (贪心+暴力)
- codeforces Polycarp at the Radio
- codefroces 723c Polycarp at the Radio
- Codeforce 723C - Polycarp at the Radio
- C. Polycarp at the Radio
- [codeforces] C. Polycarp at the Radio 数组统计
- Codeforces Round #375 (Div. 2)-C. Polycarp at the Radio
- Codeforces Round#375 C:Polycarp at the Radio(贪心)
- Codeforces Round #375 (Div. 2)C. Polycarp at the Radio
- Codeforces Round #375 (Div. 2) C Polycarp at the Radio
- [贪心] Codeforces 723C #375 (Div. 2) C. Polycarp at the Radio
- CodeForces 723C-Polycarp at the Radio(模拟 贪心 vector乱搞)
- 【codeforces 723 C】【暴力】C. Polycarp at the Radio【1~m中最少的数尽量多】
- Codeforces Round #375 (Div. 2) -- C. Polycarp at the Radio(水题)
- Mybatis错误:Parameter 'XXX' not found. Available parameters are [1, 0, param1, param2]
- CSS笔记
- 深度学习(十七)基于改进Coarse-to-fine CNN网络的人脸特征点定位-ICCV 2013
- spring boot 定时任务
- IDEA 常用快捷键(用关键字ctrl+F 搜一下搜一下)
- Codeforces 723C.Polycarp at the Radio
- 关于clojure的ns中的require,use,import等
- 深度学习(十八)基于R-CNN的物体检测-CVPR 2014
- hdu1242 Rescue bfs搜索
- Mac80211数据帧
- 深度学习(十九)基于空间金字塔池化的卷积神经网络物体检测-ECCV 2014
- easyUI记录
- iOS CollectionView制作立体广告栏
- 企业电子邮件系统全局地址簿管理及使用方法介绍