提高组比赛小计·2

T1窗外的星

题意

一个滑动窗口,在意数轴上运动,使窗口的值最大。

思路

很明显是求一个区间最大值,数据显示要在O(n)的时间复杂度左右。由于窗口是个定值(作者比喻太形象), 因此可以想到前缀和
我们一次可以想到用后端前缀和-前端前缀和来维护一个区间的最大值
代码如下:

#include<bits/stdc++.h>
#define N 10050
using namespace std;
short a[N],s[N];
int main(){
	int n,k,ans=0,p=0;
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		int x,y;
		cin>>x>>y;
		a[x]=y;
		p=max(p,x);
	}
	for(int i=1;i<=p;i++){
		s[i]=s[i-1]+a[i];
	}
	for(int i=k;i<=p;i++){
		ans=max(ans,s[i]-s[i-k]);
	}
	cout<<ans;
}

系统提示:恭喜获得10pts
观察哪里有误可以发现,一个点有多颗星星,注意是亮度之和而不是亮度的最大值

代码

#include<bits/stdc++.h>
#define N 1000050
using namespace std;
int a[N],s[N];
int main() {
	int n,k,ans=0,p=0;
	cin>>n>>k;
	if(k==0) {
		cout<<0;
		return 0;
	}
	for(int i=1; i<=n; i++) {
		int x,y;
		cin>>x>>y;
		a[x]+=y;
		p=max(p,x);
	}
	for(int i=1; i<=p; i++)s[i]=s[i-1]+a[i];
	if(k<=p)for(int i=k; i<=p; i++)ans=max(ans,s[i]-s[i-k]);
	cout<<ans;
}

T2操作系统

题意

假设该系统只有一个 CPU,每一个进程的到达时间,执行时间和运行优先级都是已知的。其中运行优先级用自然数表示,数字越大,则优先级越高。

如果一个进程到达的时候 CPU 是空闲的,则它会一直占用 CPU 直到该进程结束。除非在这个过程中,有一个比它优先级高的进程要运行。在这种情况下,这个新的(优先级更高的)进程会占用 CPU,而老的只有等待。

如果一个进程到达时,CPU 正在处理一个比它优先级高或优先级相同的进程,则这个(新到达的)进程必须等待。

一旦 CPU 空闲,如果此时有进程在等待,则选择优先级最高的先运行。如果有多个优先级最高的进程,则选择到达时间最早的。

思路

无需多言,模拟求解,用优先队列维护

代码

#include<bits/stdc++.h>
#define N 1000050
using namespace std;
struct node {
	int id, l, st, yx;
	bool operator < (const node &a) const {
		if (yx != a.yx) return yx < a.yx;
		else return l > a.l;
	}
};
long long ti;
priority_queue<node> q;
node a;
int main() {
	while (cin >> a.id >> a.l >> a.st >> a.yx) {
		while (!q.empty() && ti <= a.l) {
			if (ti + q.top().st <= a.l) {
				node x = q.top();
				q.pop();
				ti += x.st;
				cout << x.id << ' ' << ti << '\n';
			} else {
				node c = q.top();
				q.pop();
				c.st -= a.l - ti;
				q.push(c);
				ti = a.l;
				break;
			}
		}
		if (q.empty() && ti < a.l) ti = a.l;
		q.push(a);
	}
	while (!q.empty()) {
		node d = q.top();
		q.pop();
		ti += d.st;
		cout << d.id << ' ' << ti << '\n';
	}
}
posted @ 2025-10-08 10:28  爱做牢笼的老龙  阅读(6)  评论(0)    收藏  举报