跳到正文

题解

P2827 [NOIP2016 提高组] 蚯蚓

AC_Leather约 6 分钟
原文
文章信息与版本

文章信息

发布
2024年8月25日
收录
2025年12月12日
更新
2026年11月15日
篇幅
约 6 分钟
获赞
2
题目
P2827

版本

  1. v1当前

P2827 [NOIP2016 提高组] 蚯蚓

(结尾有彩蛋)

这道题第一眼优先队列,但是 O(mlog⁡(n+m))O(m\log{(n+m)}) 的时间被卡的死死的,所以只能另辟蹊径,那就是——单调性。

首先,通过题意可知:先切的蚯蚓一定比后切的长。知道这个有什么用呢?因为我们可以推出来两个很重要的性质,咱们先来推推看:

设 aa 为先切的蚯蚓,bb 为后切的蚯蚓,那么:

性质1:

已知 a≥ba \ge b,
那么两边同乘 pp(pp 的定义见原题):ap≥bpap \ge bp

而 apap 和 bpbp 分别为 aa 的左段和 bb 的左段,所以可以得出:先切的蚯蚓的左段长度一定大于等于后切的蚯蚓的左段长度。


性质2:

已知 a≥ba \ge b,
那么两边同乘 1−p1-p: (1−p)a≥(1−p)b(1-p)a \ge (1-p)b
分配律:a−pa≥b−pba-pa \ge b-pb

而 a−paa-pa 和 b−bpb-bp 分别为 aa 的右段和 bb 的右段,所以可以得出:先切的蚯蚓的右段长度一定大于等于后切的蚯蚓的右段长度。

同时,先切的和后切的都少长了一个 qq,所以无需其它判断。

所以啊,知道了这两个性质之后,单调性也出来了,共有三个:

  1. 原长度单调(需要排序)
  2. 左段长度单调
  3. 右段长度单调

那么我们怎么处理呢?很简单,用三个普通队列即可,分别存储这三种情况,这样的话,找最大的只需要比较这三个队列的头即可。总的时间复杂度为 O(nlog⁡n+m+(n+m))O(n \log{n}+m+(n+m))

接下来要处理的问题就是每一秒都长 qq 的这个问题,由于只有切开的部分不加,而其它部分一起加,差不会变,所以可以只用把切开的两端都减 qq 即可。

易错点:

  1. 计算两端长度的时候需要还原长度(原长 +(i−1)∗q+(i-1)*q)
  2. 输出的时候需要还原(加上 q∗mq*m)

完整代码:

C++
#include <iostream>
#include <queue>
#include <algorithm>
#include <fstream>
char buf[1<<20],*p1,*p2;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?0:*p1++)
using namespace std;

bool cmp(int x, int y){
	return x > y;
}
inline int read(){
	int x = 0, f = 1;
	char c = getchar();
	while (c < '0' || c > '9'){
		if (c == '-')
			f = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9'){
		x = x * 10 + (c - '0');
		c = getchar();
	}
	return x * f;
}
inline void write(int x){
	if (x == 0) return;
	write(x / 10);
	putchar(x % 10 + '0');
}
queue<int> a[3]; // a[0]代表未切,a[1]代表左端,a[2]代表右端,保证单调性 
int tot[100005]; 
signed main(){
	int n = read(), m = read(), q = read(), u = read(), v = read(), t = read();
	for (int i = 1; i <= n; i++)
		tot[i] = read();
	sort(tot+1, tot+n+1, cmp); // 排序 
	for (int i = 1; i <= n; i++)
		a[0].push(tot[i]);
	for (int i = 1; i <= m; i++){
		int maxn = -2e9, maxi;
		// 找出最长蚯蚓 
		if (!a[0].empty() && a[0].front() > maxn) maxn = a[0].front(), maxi = 0;
		if (!a[1].empty() && a[1].front() > maxn) maxn = a[1].front(), maxi = 1;
		if (!a[2].empty() && a[2].front() > maxn) maxn = a[2].front(), maxi = 2;
		a[maxi].pop();
		long long newx = maxn + i * q - q; // 还原maxn 
		int y1 = newx * u / v;
		int y2 = newx - y1;
		y1 -= i * q;
		y2 -= i * q;
		a[1].push(y1);
		a[2].push(y2);
		if (i % t == 0)
			write(newx), putchar(' ');
	}
	putchar('\n');
	int i = 1;
	while (!a[0].empty() || !a[1].empty() || !a[2].empty()){
		int maxn = -2e9, maxi;
		// 同上 
		if (!a[0].empty() && a[0].front() > maxn) maxn = a[0].front(), maxi = 0;
		if (!a[1].empty() && a[1].front() > maxn) maxn = a[1].front(), maxi = 1;
		if (!a[2].empty() && a[2].front() > maxn) maxn = a[2].front(), maxi = 2;
		a[maxi].pop();
		if (i % t == 0)
			write(maxn + m * q), putchar(' ');
		i++;
	}
	return 0;
}

彩蛋:

论CCF的数据:

当年65,现在90。

“什么magic数据啊?”

“CCF,考不考虑加强一下数据?”

可回应我的只有沉默。

“这不是对写优先队列的同学挺友好的嘛…”

可我还是克制不住心中的情绪。

“如果,我的正解只是多了10分的话,还有什么意义?!”

顿时,我感到后悔了。

.

.

.

.

.

.

“有什么可后悔的呢?”我问自己,“不就是感觉自己的付出得不到回报嘛。”

我的心情稍微平复了一点。

不过…

那我干嘛写题解啊??

“哎,不过是为了让更多人感受到这种情绪罢了。”

(哎不是!!我写这个干嘛!删掉删掉,继续continue。)

“同时,不也是为了让大家给我点个赞嘛。”

看啥看!说的就是你!(belike)

原文
存档评论1 条抓取于