队列

132. 小组队列

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
输入样例:

2
3 101 102 103
3 201 202 203
ENQUEUE 101
ENQUEUE 201
ENQUEUE 102
ENQUEUE 202
ENQUEUE 103
ENQUEUE 203
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
STOP
2
5 259001 259002 259003 259004 259005
6 260001 260002 260003 260004 260005 260006
ENQUEUE 259001
ENQUEUE 260001
ENQUEUE 259002
ENQUEUE 259003
ENQUEUE 259004
ENQUEUE 259005
DEQUEUE
DEQUEUE
ENQUEUE 260002
ENQUEUE 260003
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
STOP
0

输出样例:

Scenario #1
101
102
103
201
202
203

Scenario #2
259001
259002
259003
259004
259005
260001

arr[0]的队列记录每个组来的先后次序,之后的队列记录相应的组的成员
再按照arr[0]队列中的次序,依次使之后的队列出队

#include <iostream>
#include <queue>
#include <string>
#include <algorithm>
using namespace std;
const int N = 1e7;

int id[N];//组号 

int main(void)
{
	int t, n, x, y, idx = 1;
	string s;
	while (cin >> t && t != 0){
		queue<int> arr[1005];
		for (int i = 1; i <= t; i++){
			cin >> n;
			for (int j = 1; j <= n; j++){
				cin >> x;
				id[x] = i;//记录每个成员的组号 
			}
		}
		
		cout << "Scenario #" << idx++ << endl;
		while (cin >> s && s != "STOP"){
			if (s == "ENQUEUE"){
				cin >> x;
				if (arr[id[x]].size() == 0){//如果是新的组 
					arr[0].push(id[x]);  
				}
				arr[id[x]].push(x);
			}
			else if (s == "DEQUEUE"){
				x = arr[0].front();
				if (arr[x].size() == 0){
					arr[0].pop();
					x = arr[0].front();
				}
				y = arr[x].front();
				arr[x].pop();
				cout << y << endl;
			}
		}
		cout << endl;
	}
	return 0;
}

135. 最大子序和

在这里插入图片描述

#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 3e5 + 5, INF = 0x3f3f3f3f;

ll sum[N], q[N];

int main(void)
{
	int n, m;
	ll ans = -INF;
	cin >> n >> m;
	for (int i = 1; i <= n; i++){
		cin >> sum[i];
		sum[i] += sum[i - 1];
	}
	int l = 1, r = 1;
	q[1] = 0;
	for (int i = 1; i <= n; i++){
		while (l <= r && q[l] < i - m)
			l++;
		ans = max(ans, sum[i] - sum[q[l]]);
		while (l <= r && sum[q[r]] >= sum[i])
			r--;
		q[++r] = i;
	}
	cout << ans << endl;
	return 0; 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值