http://codeforces.com/contest/723/problem/C

题目是给出一个序列

a[i]表示第i个歌曲是第a[i]个人演唱,现在选出前m个人,记b[j]表示第j个人演唱歌曲的数量,

现在你可以改变a[]这个数组,使得前m个人的演唱歌曲的数量的最小值最大。

很明显对于前m个人,每个人唱了多少首是很容易统计出来的,只需要对<=m的人进行统计即可,不用统计>m的,这样的话数组只需开到2000即可。

对于后面的人,可以改变,优先改变前m个人中演唱歌曲最小的那个,这个可以用优先队列维护。

然后如果前m个人本来分配不均匀,那么最小值的最大值得不到最大化。

要对前m个人均匀一下,这个我用了两个优先队列去做,因为思路就是把最大的分一次给最小的,这样的最优的。

hack点:注意到ans最多也只是n / m。就相当于把n分成m分,如果前m个人的答案已经是n / m,后面的人就不需要变化的,不然的话改变数量变大了,会错误。好像wa7就是这个坑。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
int n, m;
const int maxn = + ;
int book[maxn];
int a[maxn];
struct node {
int num;
int id;
bool operator < (const struct node &rhs) const {
return num > rhs.num;
}
node(int aa, int bb) : num(aa), id(bb) {}
};
struct GGnode {
int num;
int id;
bool operator < (const struct GGnode &rhs) const {
return num < rhs.num;
}
GGnode(int aa, int bb) : num(aa), id(bb) {}
};
priority_queue<struct node>que;
priority_queue<struct GGnode>GGque;
vector<int>pos[maxn];
void work() {
scanf("%d%d", &n, &m);
for (int i = ; i <= n; ++i) {
scanf("%d", &a[i]);
if (a[i] <= m) {
book[a[i]]++;
}
}
for (int i = ; i <= m; ++i) {
que.push(node(book[i], i));
}
int tochange = ;
int get = n / m;
for (int i = ; i <= n; ++i) {
if (a[i] <= m) continue;
struct node t = que.top();
if (t.num == get) break;
que.pop();
a[i] = t.id;
t.num++;
que.push(t);
tochange++;
}
memset(book, , sizeof book);
while (!que.empty()) {
struct node t = que.top();
que.pop();
book[t.id] = t.num;
}
int all = ;
int ans = inf;
for (int i = ; i <= m; ++i) {
ans = min(ans, book[i]);
all += book[i];
que.push(node(book[i], i));
GGque.push(GGnode(book[i], i));
}
for (int i = ; i <= n; ++i) {
if (a[i] <= m) {
pos[a[i]].push_back(i);
}
}
int gold = all / m;
if (ans != gold) {
while (true) {
struct node tt = que.top();
que.pop();
struct GGnode GGtt = GGque.top();
GGque.pop();
if (book[tt.id] == gold) break; book[tt.id]++;
book[GGtt.id]--;
int tpos = pos[GGtt.id].back();
pos[GGtt.id].pop_back();
a[tpos] = tt.id; tt.num = book[tt.id];
GGtt.num = book[GGtt.id]; que.push(tt);
GGque.push(GGtt);
tochange++;
}
}
ans = gold;
cout << ans << " " << tochange << endl;
for (int i = ; i <= n; ++i) {
printf("%d ", a[i]);
}
return;
} int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
work();
return ;
}

感觉写的有点麻烦,

所以重写了一次。

考虑到它一定要使得b[j]数组中的最小值最大,而这个最大值,必然是n / m的。所以,直接把1---m中数量小于n / m的统计起来,用其他去变即可。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = + ;
int a[maxn];
int book[maxn];
set<int>pos;
void work() {
int n, m;
scanf("%d%d", &n, &m);
int gold = n / m;
for (int i = ; i <= n; ++i) {
scanf("%d", &a[i]);
if (a[i] <= m) {
book[a[i]]++;
}
}
for (int i = ; i <= n; ++i) {
if (a[i] <= m && book[a[i]] < gold) {
pos.insert(a[i]);
}
}
for (int i = ; i <= m; ++i) {
if (book[i] < gold) pos.insert(i);
}
// printf("ff");
int toChange = ;
set<int> :: iterator it = pos.begin();
if (it != pos.end()) { //如果需要调整
int val = *it;
for (int i = ; i <= n; ++i) {
if (a[i] > m) {
book[val]++;
a[i] = val;
toChange++;
} else {
if (pos.find(a[i]) != pos.end()) continue;
if (book[a[i]] == gold) continue;
book[a[i]]--;
book[val]++;
a[i] = val;
toChange++;
}
if (book[val] == gold) {
it++;
if (it == pos.end()) break;
val = *it;
}
}
}
printf("%d %d\n", gold, toChange);
for (int i = ; i <= n; ++i) {
printf("%d ", a[i]);
}
return ;
} int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
work();
return ;
}

Codeforces Round #375 (Div. 2) Polycarp at the Radio 优先队列模拟题 + 贪心的更多相关文章

  1. Codeforces Round #375 (Div. 2)【A,B【模拟】,D【DFS】】

    PS_B:阿洗吧!B题卧槽数组开了250... PS_D:D题主要挂在了50*50口算得了250,数组开小,然后一开始还错了.= =哎,以后对于数据范围还是注意一点: 卧槽,这场可真二百五了... A ...

  2. Codeforces Round #234 (Div. 2) A. Inna and Choose Options 模拟题

    A. Inna and Choose Options time limit per test 1 second memory limit per test 256 megabytes input st ...

  3. Codeforces Round #368 (Div. 2) A. Brain's Photos (水题)

    Brain's Photos 题目链接: http://codeforces.com/contest/707/problem/A Description Small, but very brave, ...

  4. Codeforces Round #375 (Div. 2) C. Polycarp at the Radio 贪心

    C. Polycarp at the Radio time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  5. Codeforces Round #375 (Div. 2)

    A. The New Year: Meeting Friends 水 #include <set> #include <map> #include <stack> ...

  6. Codeforces Round #375 (Div. 2) A B C 水 模拟 贪心

    A. The New Year: Meeting Friends time limit per test 1 second memory limit per test 256 megabytes in ...

  7. Codeforces Round #375 (Div. 2) ABCDE

    A - The New Year: Meeting Friends 水 #include<iostream> #include<algorithm> using namespa ...

  8. Codeforces Round #281 (Div. 2) A. Vasya and Football 模拟

    A. Vasya and Football 题目连接: http://codeforces.com/contest/493/problem/A Description Vasya has starte ...

  9. Codeforces Round #375 (Div. 2) - D

    题目链接:http://codeforces.com/contest/723/problem/D 题意:给定n*m小大的字符矩阵.'*'表示陆地,'.'表示水域.然后湖的定义是:如果水域完全被陆地包围 ...

随机推荐

  1. poj 2000 Gold Coins(水题)

    一.Description The king pays his loyal knight in gold coins. On the first day of his service, the kni ...

  2. 《Kubernetes权威指南第2版》学习(三)RC学习

    1 RC文件介绍: kind: ReplicationController,表示是一个RC: spec.selector:  RC的Pod标签(Label)选择器,监控和管理拥有这些标签的Pod实例, ...

  3. Ok6410裸机驱动学习(一)开发工具

    1.GCC工具链 1.GCC默认处理的文件类型 文件类型 扩展名 文件说明 文本文件 *.c C语言源文件 *.C.*.cxx.*.cc C++源文件 *.i 预处理后的C语言源文件 *.ii 预处理 ...

  4. Java探索之旅(9)——数据和方法的可见性

    注意,在UML图中,public-protected-private分别用+,-,#表示. 类中成员修饰符 在同一类访问 在同一包访问 在子类内访问 在不同包可访问 Public √ √ √ √ Pr ...

  5. [poj1390]Blocks(方块消除)

    题目大意:给定一序列,可点击某一位置消除与其相邻且相同的方块,得分为$len*len$,求最大得分. 解题关键:关键是状态的构造,首先离散化一下,令$dp[i][j][k]$表示序列$i-j$且后面有 ...

  6. redis GEO地理位置命令介绍

    GEOADD keylongitude latitude member [longitude latitude member ...] Available since 3.2.0. Time comp ...

  7. VS编译器中设置 输出窗口 只显示error,不显示warning 要如何配置

    VS编译器中设置 输出窗口 只显示error,不显示warning 要如何配置 在编译大型项目的时候,总是VS编译器的输出窗口总是会出现一堆warning警告,要想在里面找到error错误,要使用鼠标 ...

  8. Object—C 块在函数中作为参数时的分析

    暂时对这个有了一些粗浅的理解,记下来一边后面学习时学习,改正. 先举个例子: A类: .h文件: @interface A  : NSObject - (void)Paly1:(void (^)(do ...

  9. DES的雪崩效应分析

    明文固定,密钥改变一个字节 假定明文为11111111(00000001 00000001 00000001 00000001 00000001 00000001 00000001 00000001) ...

  10. 转:PHP性能:序——谈ab(Apache Bench)压力测试工具

    PHP性能:序——谈ab(Apache Bench)压力测试工具 ab(Apache  Bench)是啥? ab是Apache自带的一个压力测试软件,可以通过ab命令和选项对某个URL进行压力测试.a ...