第十届蓝桥杯省赛C++A组

外卖店优先级(模拟)

1241. 外卖店优先级
“饱了么”外卖系统中维护着 N 家外卖店,编号 1∼N。

每家外卖店都有一个优先级,初始时 (0 时刻) 优先级都为 0。

每经过 1 个时间单位,如果外卖店没有订单,则优先级会减少 1,最低减到 0;而如果外卖店有订单,则优先级不减反加,每有一单优先级加 2。

如果某家外卖店某时刻优先级大于 5,则会被系统加入优先缓存中;如果优先级小于等于 3,则会被清除出优先缓存。

给定 T 时刻以内的 M 条订单信息,请你计算 T 时刻时有多少外卖店在优先缓存中。

输入格式
第一行包含 3 个整数 N,M,T。

以下 M 行每行包含两个整数 ts 和 id,表示 ts 时刻编号 id 的外卖店收到一个订单。

输出格式
输出一个整数代表答案。

数据范围
1≤N,M,T≤105,
1≤ts≤T,
1≤id≤N
输入样例:

2 6 6
1 1
5 2
3 1
6 2
2 1
6 2

输出样例:

1

样例解释 6 时刻时,1 号店优先级降到 3,被移除出优先缓存;2 号店优先级升到 6,加入优先缓存。

所以是有 1 家店 (2 号) 在优先缓存中。

  1. 输入完数据后按店铺从小到大排序,店铺相同按时间从小到大排序;
  2. 用while循环得出每家店铺最后的优先级,并加以处理判断是否在优先缓存中;
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int N=100005;
const int inf=0x3f3f3f3f;
typedef long long ll;
struct node{
	int ts,td;
}s[N];
int cmp(node a,node b){
	if(a.td!=b.td) return a.td<b.td;
	if(a.ts!=b.ts) return a.ts<b.ts;	
}
int a[N],vis[N],isv[N];
vector<int>v;
int main()
{
	int i,j,n,m,t,k,x,y,ans=0;
	scanf("%d %d %d",&n,&m,&t);
	for(i=0;i<m;i++){
		scanf("%d %d",&s[i].ts,&s[i].td);
	}
	sort(s,s+m,cmp);
	int d,tot=0,time;
	while(tot<m){
		time=2;
		d=s[tot].td;
		tot++;
		while(tot<m&&d==s[tot].td){
			x=s[tot].ts-s[tot-1].ts-1;//计算两次订单的时间差
			if(x<0) x=0;//相邻时间有多个订单
			time=time-x;
		//	printf("%d %d\n",s[tot].ts,time);
			if(time<0) time=0;
			time+=2;
			tot++;					
		}
		int flag=0;
		if(time>5) flag=1;//之前在优先缓存中的才算
		time=time-(t-s[tot-1].ts);
	//	printf("%d\n",time);
		if(flag&&time>3) ans++;				
	}
	printf("%d",ans);
	return 0;
}

按照时间的顺序循环会超时,做题之前应好好想想,大意了。。

#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int N=100005;
const int inf=0x3f3f3f3f;
typedef long long ll;
struct node{
	int ts,td;
}s[N];
int cmp(node a,node b){
	if(a.ts!=b.ts) return a.ts<b.ts;
	return a.td<b.td;
}
int a[N],vis[N],isv[N];
vector<int>v;
int main()
{
	int i,j,n,m,t,k,x,y,ans=0;
	scanf("%d %d %d",&n,&m,&t);
	for(i=0;i<m;i++){
		scanf("%d %d",&s[i].ts,&s[i].td);
	}
	sort(s,s+m,cmp);
	int tot=0;
	for(i=1;i<=t&&tot<m;i++){	
		while(tot<m&&s[tot].ts==i){
			int d=s[tot].td;
			a[d]+=2;
			vis[d]=1;
			if(isv[d]==0){
				v.push_back(d);
				isv[d]=1;
			}			
			tot++;	
		}
		for( vector<int>::iterator j=v.begin();j!=v.end();j++){
			int d=*j;
			if(vis[d]==1){
				if(a[d]>5) isv[d]=2;
				vis[d]=0;
			}			
			else if(a[d]>0) {
				a[d]--;
				if(a[d]<=3) isv[d]=1;
				else if(a[d]==0){
					isv[d]=0;
					j=v.erase(j);
					j--;					
				}
			}
		}
	}
	for(i=0;i<v.size();i++){
//		printf("%d\n",a[v[i]]);
		if(a[v[i]]>3&&isv[v[i]]==2) ans++;
	}
	printf("%d",ans);
	return 0;
}

修改数组(递归)

1242. 修改数组
给定一个长度为 N 的数组 A=[A1,A2,⋅⋅⋅AN],数组中有可能有重复出现的整数。

现在小明要按以下方法将其修改为没有重复整数的数组。

小明会依次修改 A2,A3,⋅⋅⋅,AN。

当修改 Ai 时,小明会检查 Ai 是否在 A1∼Ai−1 中出现过。

如果出现过,则小明会给 Ai 加上 1;如果新的 Ai 仍在之前出现过,小明会持续给 Ai 加 1,直到 Ai 没有在 A1∼Ai−1 中出现过。

当 AN 也经过上述修改之后,显然 A 数组中就没有重复的整数了。

现在给定初始的 A 数组,请你计算出最终的 A 数组。

输入格式
第一行包含一个整数 N。

第二行包含 N 个整数 A1,A2,⋅⋅⋅,AN。

输出格式
输出 N 个整数,依次是最终的 A1,A2,⋅⋅⋅,AN。

数据范围
1≤N≤105,
1≤Ai≤106
输入样例:

5
2 1 1 3 4

输出样例:

2 1 3 4 5

暴力的话会超时,运用递归求解,每次递归完记录当前数的最高被标记(用过)的数,下次查询可大幅度减少查询时间。

#include<cstdio>
const int N=1000005;
int v[N];
int dfs(int x){
	if(v[x]==0) {
		v[x]=x;
		return x;
	}
	else return v[x]=dfs(v[x]+1);
}
int main()
{
	int i,j,n,m,t,k,x,y,ans=0;
	scanf("%d",&n);
	for(i=0;i<n;i++){
		scanf("%d",&x);
		printf("%d ",dfs(x));
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值