[codeforces723C]Polycarp at the Radio
来源:互联网 发布:乐乎公寓电话 编辑:程序博客网 时间:2024/06/09 21:03
点击打开链接
贪心题 场上没想出来 我太弱了
问的毛毛 毛毛回答的我(毛毛太神辣)
大体思路
首先你肯定要把数改到n/m下面去 这是必须的 题目要求
然后 你要尽量改少的次数让
首先 最后答案肯定是sigma(n/m-每个数出现的次数)(其实我也不知道为什么)
然后 开m个vector 每次将所有位置扔进去 然后暴力看一下这个数能不能减小
能减小 就把他变成每个数出现次数中最小的 贪心吗
然后修改原序列 输出三个数 完了
#include<cstdio>#include<cstdlib>#include<iostream>#include<cstring>#include<algorithm>#include<queue>#include<iomanip>#include<stack>#include<map>#include<set>#include<cmath>#define debug(x) cerr<<#x<<"="<<x<<endl;#define INF 0x7f7f7f7fusing namespace std;typedef pair<int,int> pii;inline int init(){int now=0,ju=1;char c;bool flag=false;while(1){c=getchar();if(ju=='-')ju=-1;else if(c>='0'&&c<='9'){now=now*10+c-'0';flag=true;}else if(flag)return now*ju;}}int n,m;int c[2001];int cctype,tmp=0;int a[2001];int eachpos[2001];vector<int> v[2001];vector<int> vans;int main(){n=init();m=init();int tar=n/m;int sum=0;for(int i=1;i<=n;i++){a[i]=init();if(a[i]<=m){c[a[i]]++;v[a[i]].push_back(i);}if(a[i]>m)vans.push_back(i);}for(int i=1;i<=m;i++){if(c[i]<tar){sum+=tar-c[i];}}int cnt=1;for(int i=0;i!=vans.size();i++){if(cnt==sum+1)break;tmp=INF;for(int j=1;j<=m;j++){if(c[j]<tmp){tmp=c[j];cctype=j;}}cnt++;a[vans[i]]=cctype;c[cctype]++;}int pos;bool flag=false;for(int j=cnt;j<=sum;j++){for(int i=1;i<=m;i++){if(c[i]>tar){if(eachpos[i]!=v[i].size()){c[i]--;pos=v[i][eachpos[i]];eachpos[i]++;break;}}}tmp=INF;for(int i=1;i<=m;i++){if(c[i]<tmp){tmp=c[i];cctype=i;}}a[pos]=cctype;c[cctype]++;}printf("%d %d\n",tar,sum);for(int i=1;i<=n;i++){printf("%d ",a[i]);}return 0;}/*srO xudyh davidlee1999WTK linkct1999 Orzcompiler TDM-GCC 5.9.2*/
代码写的丑
0 0
- [codeforces723C]Polycarp at the Radio
- codeforces723C- Polycarp at the Radio-greedy
- CodeForces723C Polycarp at the Radio 贪心
- codeforces723C Polycarp at the Radio 英语阅读题
- C. Polycarp at the Radio
- CF_C. Polycarp at the Radio
- codeforces Polycarp at the Radio
- codefroces 723c Polycarp at the Radio
- Codeforces 723 C. Polycarp at the Radio
- Codeforces 723C.Polycarp at the Radio
- Codeforce 723C - Polycarp at the Radio
- [codeforces] C. Polycarp at the Radio 数组统计
- Codeforces723 C. Polycarp at the Radio (模拟)
- 【23.48%】【codeforces 723C】Polycarp at the Radio
- Codeforces Round #375 (Div. 2)-C. Polycarp at the Radio
- codeforces 723C. Polycarp at the Radio [模拟]【杂类】
- Codeforces Round#375 C:Polycarp at the Radio(贪心)
- Codeforces Round #375 (Div. 2)C. Polycarp at the Radio
- leetcode 242. Valid Anagram
- leetcode---Single Number II---二进制
- setsockopt设置socket状态
- 【从C到C++学习笔记】内联成员函数/成员函数重载及缺省参数
- 第四章 第三节 插入留言
- [codeforces723C]Polycarp at the Radio
- MVC的简单搭建之medoo数据类
- Markdown使用笔记
- Android与服务器通信之socket通信
- [学习《算法导论》] 第一部分 基础知识
- (笔记)Linux下网络编程,采用TCP协议实现的C/S架构
- Struts+Hibernate的登录验证
- jsp中mysql驱动包的位置
- windows和liunx行尾结束符