洛谷P2827蚯蚓

题目

堆+模拟,还有一个小优化(优化后跟堆关系不大,而是类似于贪心)。

如果不加优化的话,卡常可以卡到85。

思路是对于对每一秒进行模拟,用堆来维护动态的最大值,然后对于每个长度都加q的情况可以用一个中间变量temp来处理。

85ptsCode :

#include <iostream>
#include <cmath>
#include <stack>
#include <cstdio>	
#include <algorithm>
#include <cstring>
#include <queue>
#define N 8001001
using namespace std;
priority_queue <int> que;
int n, m, q, u, v, t;
int data[N], ans1[N], ans2[N];
double p;
bool cmp(int a, int b)
{
	return a > b;
}
inline int read() {
    char ch = getchar(); int x = 0, f = 1;
    while(ch < '0' || ch > '9') {
        if(ch == '-') f = -1;
        ch = getchar();
    } while('0' <= ch && ch <= '9') {
        x = x * 10 + ch - '0';
        ch = getchar();
    } return x * f;
}
int main()
{
	n = read(), m = read(), q = read(), u = read(), v = read(), t = read(); 
	p = (double) u / v;
	for (register int i = 1; i <= n; i++) data[i] = read(), que.push(data[i]);
	int temp = 0;
	for (register int i = 1; i <= m; i++)
	{
		int tes = que.top(); que.pop();
		ans1[i] = tes + temp;
		int a1 = (int) (ans1[i] * p); int a2 = tes + temp- a1;//先按真实的算,然后再减去 
		temp += q; a1 -= temp, a2 -= temp;//这俩不增加.//也可以写为 a1 -= temp, a2 -= temp; temp += q; a1 -= q, a2 -= q;先减去刚才加上的,然后由于a1和a2都不增加,所以要再减去q 
		que.push(a1), que.push(a2);
	}	
	int cnt = 0;
	for (int i = t; i <= m; i += t)	
		printf("%d ", ans1[i]);
	puts("");
	while ( !que.empty() )
	{	
		int a = que.top(); cnt++;
		que.pop(); 
		if (!(cnt % t)) printf("%d ", a + temp);
	}	
}

而仔细看数据表可以发现,这个题可以优化,我们发现这个堆似乎没什么用因为其实蚯蚓的长度是满足单调性的,所以先加进去的数要比后加进去的数要大的。

因此我们可以不需要这个堆,转而用三个单调数组每次比较这三个数组的最大值,就能求出答案

100ptsCode :

#include <bits/stdc++.h>
#define N 8000007
using namespace std;	
priority_queue <int> que;	
int n, m, q, u, v, t, temp, tes, s = 1, s1 = 1, s2 = 1;//tes表示每次选的数, temp表示中间数组。 s, s1, s2分别表示每个数组的起始指针。 
int ans1[N], ans2[N], data[N], lef[N], righ[N];
bool cmp(int a, int b) {
	return a > b;
}		
inline int r()
{
	char ch = getchar(); int x = 0, f = 1;
    while(ch < '0' || ch > '9') {
        if(ch == '-') f = -1;
        ch = getchar();
    } while('0' <= ch && ch <= '9') {
        x = x * 10 + ch - '0';
        ch = getchar();
    } return x * f;
}
inline void read()
{
	n = r(), m = r(), q = r(), u = r(), v = r(), t = r();
	for (int i = 1; i <= n; i++) data[i] = r();
	sort(data + 1, data + 1 + n, cmp);	
}		
inline void print()
{	
	for (int i = t; i <= m; i += t)
		printf("%d ", ans1[i]);
	puts("");
	for (int i = 1; que.size(); i++)
	{
		if ((i % t) == 0)
			printf("%d ", que.top() + temp); 
		que.pop();
	}
}
signed main()
{
	read();		
	double p = (double) u / v;
	for (register int i = 1; i <= m; i++)
	{			
		if (s > n)//说明data[s]切完
		{		
			if (lef[s1] > righ[s2]) tes = lef[s1++];
			else tes = righ[s2++];
	 	}		
	 	else if (data[s] >= lef[s1] && data[s] >= righ[s2])//这几个if里一定要加等号,否则会WA或T 
	 		tes = data[s++];
	 	else if (lef[s1] >= data[s] && lef[s1] >= righ[s2])
	 		tes = lef[s1++];
	 	else if (righ[s2] >= data[s] && righ[s2] >= lef[s1])
	 		tes = righ[s2++]; 
	 	ans1[i] = tes + temp;
	 	int a1 = (int) (ans1[i] * p), a2 = ans1[i] - a1;
	 	temp += q;
	 	a1 -= temp, a2 -= temp;
	 	lef[i] = a1, righ[i] = a2;//两个数组分别表示每一次切得蚯蚓的两端长度。
	}			
	for (register int i = s; i <= n; i++)  que.push(data[i]);//寻找没有被切的data
	for (register int i = s1; i <= m; i++) que.push(lef[i]);//寻找没有被切的lef
	for (register int i = s2; i <= m; i++) que.push(righ[i]);//寻找没有被切的righ
	print();	
	return 0;	
}
posted @   DAGGGGGGGGGGGG  阅读(298)  评论(0编辑  收藏  举报
编辑推荐:
· 后端思维之高并发处理方案
· 理解Rust引用及其生命周期标识(下)
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
阅读排行:
· 2025成都.NET开发者Connect圆满结束
· 后端思维之高并发处理方案
· 千万级大表的优化技巧
· 在 VS Code 中,一键安装 MCP Server!
· 10年+ .NET Coder 心语 ── 继承的思维:从思维模式到架构设计的深度解析
点击右上角即可分享
微信分享提示