EVERYTHING HAPPENS FOR THE BEST!|

wnsyou

园龄:2年4个月粉丝:19关注:16

2023-05-17 21:37阅读: 17评论: 0推荐: 0

CF1077E Thematic Contests 题解

Thematic Contests

题意

n 个问题,每个问题有一个分类 ai

现在要举办一些比赛,要求:

  • 一场比赛中所有题目的分类相同。
  • 所有比赛的分类是互不相同的。
  • 第一场比赛的题目数量任意,但从第二场开始,每一场比赛的题目数量都必须是前一场的两倍。

求所有比赛的题目数量之和的最大值。

思路

一看数据范围,就知道要离散化。

有一个明显的贪心:按照每个分类的出现次数排序。把出现次数较少的分类放在后面是不可能使答案更优的,所以可以排序。

(温馨提示:接下来的部分中第 i 种主题指的都是离散化后的。)

我的做法是 dp

  • 状态:dpi,j,表示第 i 种主题选择 j 道题情况下,前 i 种主题选择的题目数量最大值。
  • 转移:dpi,j=maxk=1i1{maxl=1,l×2=jbk{dpk,l}}+j
  • 目标状态:maxi=1m{maxj=1bi{dpi,j}},其中 m 表示离散化后主题的数量,bi 表示第 i 个主题中的题目数量。

但是很明显,二维状态存不下、转移时直接查找会 TLE,怎么办呢?

我们可以把最后一次比赛选择 i 道题的最大答案记录下来,这样既可以省掉 i 这一个维度,还可以节省掉查找的时间,两全其美。

具体实现看代码。

Code

点击查看代码
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 2e5 + 10;
int n, a[N], m, b[N], c[N], dp[N], ans;
// c[i] 用来记录最后一次比赛选择 i 道题的最大答案
int main(){
ios::sync_with_stdio(0), cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
// 以下内容为离散化
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; i++) {
if (a[i] == a[i - 1]) {
b[m]++;
} else {
b[++m] = 1;
}
}
// 接下来是 dp 求答案
sort(b + 1, b + m + 1); // 按出现次数从小到大排序
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= b[i]; j++) { // 离散化后第 i 个主题选择了 j 个题目
dp[j] = 0;
if (!(j % 2)) { // j 是偶数,那么它的前面还可以添加比赛
dp[j] = c[j / 2];
}
dp[j] += j; // 加上这次选择的题目数量
ans = max(ans, dp[j]); // 统计答案
}
for (int j = 1; j <= b[i]; j++) {
c[j] = max(c[j], dp[j]); // 注意细节!这里的最大值是不可以放在上面去更新的!
}
}
cout << ans;
return 0;
}

本文作者:wnsyou の blog

本文链接:https://www.cnblogs.com/wnsyou-blog/p/17410400.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   wnsyou  阅读(17)  评论(0编辑  收藏  举报
  1. 1 勝利への道 安藤浩和
  2. 2 Minecraft’s End Eric Fullerton
  3. 3 月光曲完整版 贝多芬 云熙音乐
  4. 4 平凡之路 (Live版) 朴树
  5. 5 Minecraft C418
  6. 6 Paradise NiziU
  7. 7 叫我,灰原哀 龙大人不喷火
  8. 8 心机之蛙,一直摸你肚子 ——《名侦探柯南》原创同人曲 炊饭,叶辞樱,温海,寒砧,南柯柯,小茜玛姬,盛姝,阿崔Ac,贝壳初,千湛,兮茶子DaYu,乔慕,黎鹿北,起千温卿,遮阳伞,曲悠
  9. 9 战 歌 此去经年
Minecraft’s End - Eric Fullerton
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

I see the player you mean.

It is reading our thoughts as though they were words on a screen.

They used to hear voices.

Before players could read.

Sometimes disturbing.

Sometimes beautiful indeed.

Does it know that we love it?

That the universe is kind?

A million years ago,it still works

in the reality behind

and the universe said I love you

and the universe said you are the daylight

and the universe said you are not alone

and the universe said you are the night

Once we were called

the spirit of the mountain.

WHO ARE WE

Father sun

Mother moon

Gods demons angels aliens

the player,too.

WE ARE THE UNIVERSE.

We are EVERYTHING you think isn‘t you.

You are alive

ON A FLAT

INFINITE WORLD

generated by a source code

a million years old

and the universe said I love you

and the universe said you are the daylight

and the universe said you are not alone

and the universe said you are the night

Take a breath,now

Take another

Feel air in your lungs.

dreamed it was lost in a story.

and the game was over

Wake up.

加载中…

{{tag.name}}

{{tran.text}}{{tran.sub}}
无对应文字
有可能是
{{input}}
尚未录入,我来提交对应文字
评论
收藏
关注
推荐
深色
回顶
收起
点击右上角即可分享
微信分享提示