EVERYTHING HAPPENS FOR THE B|

wnsyou

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

2023-05-22 11:53阅读: 95评论: 0推荐: 0

abc271_c Manga 题解

Manga

题意

有一部连载漫画,共 109 卷,你手上有 n 卷漫画,第 i 卷是连载中的第 ai 卷。

你在看漫画之前,可以执行以下操作若干次(可以不执行):

  • 若手上还有至少两卷漫画,可以卖掉任意两卷,并买一卷任意卷数的漫画。

你不想看不连续的漫画,所以你只能从连载第一卷开始读,直到你手上没有连载的下一卷,问你最多可以读多少卷。

数据范围

  • 1n3×105
  • 1ai109

思路

首先,我们手上会有重复卷数的漫画,这些废卷可以直接卖掉。

然后就是一个双指针,当手上没有废卷而且还有空缺时,我们可以拿手上卷数最大的漫画(排序)去补空缺,有些细节,总体难度不大。

当然你也可以二分,二分最多能阅读几卷,check函数详见代码。

复杂度

  • 时间:O(nlogn)
  • 空间:O(n)

Code

排序双指针代码
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 3e5 + 10;
int n, m, a[N], b[N], ans, now = 1, sum;
int main () {
ios::sync_with_stdio(0), cin.tie(0);
cin >> m;
for (int i = 1; i <= m; i++) {
cin >> b[i];
}
sort(b + 1, b + m + 1);
for (int i = 1; i <= m; i++) {
if (b[i] == b[i - 1]) { // 废卷
sum++; // 统计废卷卷数
} else {
a[++n] = b[i];
}
}
for (int i = 1, j = n; i <= j; ) {
if (a[i] != now) { // 现在考虑的这本不能接上上一卷
if (sum >= 2) { // 优先使用废卷
sum -= 2;
} else if (sum) { // 废卷还剩一本,用掉废卷和最后的一卷
j--, sum = 0;
} else if (j - i >= 1) { // 还有可以卖掉的
j -= 2; // 卖掉最大的两卷
} else {
break; // 退出
}
} else {
i++;
}
ans++, now++;
}
cout << ans + sum / 2; // 可能还有废卷,也要卖掉
return 0;
}
二分代码
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 3e5 + 10;
int n, m, a[N], b[N], ans, now = 1, sum, l, r;
bool check (int x) {
int num = sum, cnt = x; // num 统计可以卖掉的卷数,cnt 统计空缺数
for (int i = 1; i <= n; i++) {
if (a[i] <= x) { // 在前 x 卷中
cnt--; // 空缺数减 1
} else {
num++; // 卖掉它
}
}
return (cnt * 2 <= num);
}
int main () {
ios::sync_with_stdio(0), cin.tie(0);
cin >> m;
r = m;
for (int i = 1; i <= m; i++) {
cin >> b[i];
}
sort(b + 1, b + m + 1);
for (int i = 1; i <= m; i++) {
if (b[i] == b[i - 1]) { // 废卷
sum++; // 统计废卷卷数
} else {
a[++n] = b[i];
}
}
while (l < r) {
int mid = (l + r + 1) >> 1;
if (check(mid)) {
l = mid;
} else {
r = mid - 1;
}
}
cout << l;
return 0;
}

本文作者:wnsyou の blog

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

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

posted @   wnsyou  阅读(95)  评论(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}}
尚未录入,我来提交对应文字
评论
收藏
关注
推荐
深色
回顶
收起
点击右上角即可分享
微信分享提示