FZU1894 志愿者选拔(队列) ~TLE

Problem 1894 志愿者选拔

Accept: 1573    Submit: 4888
Time Limit: 1500 mSec    Memory Limit : 32768 KB

 Problem Description

世博会马上就要开幕了,福州大学组织了一次志愿者选拔活动。
参加志愿者选拔的同学们排队接受面试官们的面试。参加面试的同学们按照先来先面试并且先结束的原则接受面试官们的考查。
面试中每个人的人品是主要考查对象之一。(提高人品的方法有扶老奶奶过街,不闯红灯等)
作为主面试官的John想知道当前正在接受面试的同学队伍中人品值最高的是多少。于是他请你帮忙编写一个程序来计算。

 Input

输入数据第一行为一整数T,表示有T组输入数据。
每组数据第一行为”START”,表示面试开始
接下来的数据中有三种情况:
 输入含义
1C NAME RP_VALUE名字为NAME的人品值为RP_VALUE的同学加入面试队伍。(名字长度不大于5,0 <= RP_VALUE <= 1,000,000,000)
2G排在面试队伍最前面的同学面试结束离开考场。
3Q主面试官John想知道当前正在接受面试的队伍中人品最高的值是多少。
最后一行为”END”,表示所有的面试结束,面试的同学们可以依次离开了。
所有参加面试的同学总人数不超过1,000,000

 Output

对于每个询问Q,输出当前正在接受面试的队伍中人品最高的值,如果当前没有人正在接受面试则输出-1。

 Sample Input

2
START
C Tiny
1000000000
C Lina 0
Q
G
Q
END
START
Q
C ccQ 200
C cxw 100
Q
G
Q
C wzc 500
Q
END

 Sample Output

1000000000
0
-1
200
100
500

 Hint

数据较大建议使用scanf,printf 不推荐使用STL

 Source

福州大学第七届程序设计竞赛

刚学队列,单调队列还不太清楚,写了个普通的队列,没有排序,输出的时候来排序,结果果然TLE了,不过也算用队列写了点东西。至少sample都过了 。。

#include<iostream>
#include<stdio.h>
using namespace std;
int N;<span style="white-space:pre">		</span>//几个队伍
int num = 0;<span style="white-space:pre">	</span>//可变的下标用来读入数据
int front = 0;<span style="white-space:pre">	</span>//队头 必须定义为全局变量
int rear = 0;<span style="white-space:pre">	</span>//队尾 同上
const int sz = 100;<span style="white-space:pre">	</span>//字符串长度
struct queue{
	char name[sz];<span style="white-space:pre">		</span>//名字,其实并没有用
	int rp;<span style="white-space:pre">			</span>//人品值
}q[100005];<span style="white-space:pre">			</span>//直接定义一个数组的形式就不用写规矩的函数了
int getmax(queue* q, int front, int rear)<span style="white-space:pre">	</span>//警察叔叔 就是它让我超时了
{
	if(front == rear) 
		return -1;
	int max = 0;
	for(int i = front; i < rear; i ++)
	{
		if(q[i] . rp > max)
			max = q[i] . rp;
	}
	return max;
}
void init()<span style="white-space:pre">		</span>//判断另一个队列先进行的初始化,所修改的变量均为全局变量
{
	front = rear = 0;
	num = 0;
}
int main()
{
	char  c[sz];
	char judge[sz];
	
	scanf("%d", &N) ;
	while(N --)
	while(1)<span style="white-space:pre">		</span>//这个结构是参考别人的 - - 自己写的并不行 基础太弱
	{
		scanf("%s", judge);
		getchar();
		if(judge[0] == 'S')
			continue;
		if(judge[0] == 'E')
		{
			init();
			break;
		}
		if(judge[0] == 'C')
		{
			char curname[sz];
			scanf("%s", &curname);
			strcpy(q[num].name , curname);
			long long currp;<span style="white-space:pre">	</span>//current rp value 数据范围要注意
			scanf("%lld", &currp);
			q[num++].rp = currp;
			rear++;<span style="white-space:pre">		</span>// 每增加一个数据,队尾增加1
		}
		else if(judge[0] == 'Q')
		{
			printf("%d\n", getmax(q, front, rear));
		}
		else if(judge[0] == 'G')
			front ++;<span style="white-space:pre">	</span>//每出列一个人,队头往后推1个
		else printf("error\n");<span style="white-space:pre">	</span>//测试用
	}
}
		
			
			 

下午又参照着码了一遍
#include<stdio.h>
const int sz = 10;
const int sum = 100000;
struct queue{
	int index;
	long long rp;
}q[100005];
int front, rear;
int insert(long long rp, int index)
{
	while(rp > q[rear].rp && rear > front)
		rear --;
	q[++rear].rp = rp;
	q[rear].index = index;
	return 1;
}

void init()
{
	front = 0;
	rear = -1;
}

int main()
{
	char s[sz];
	char name[sz];
	int T;
	long long currp;
	init();
	scanf("%d", &T);
	getchar();
	while(T--)
	{
		int outnum = 0;
		int index = 0;									//---------
		while(1)
		{
			
			scanf("%s", s);
			getchar();
			if(s[0] == 'C')
			{
				
				scanf("%s", name); 
				scanf("%lld", &currp);
				getchar();
				insert(currp, index++);
			}
			
			else if(s[0] == 'G')
			{
				outnum ++;
				while(q[front].index <= outnum && front < rear)//--------
					front ++;
			}
			
			else if(s[0] == 'Q')
			{
				if(front - 1== rear )
					printf("-1\n");
				else
				{
					printf("%lld\n", q[front].rp);			 //-------
				}
			}
			else if(s[0] == 'E')
			{
				init();
				break;
			}
			else if(s[0] == 'S')
			{
				continue; 
			}
		}
	}
}


特别注意的是初始化的时候 rear = -1,front = 0 才能避免很多麻烦,我参照de代码用rear = front = 0,以后各种front + 1,看起来很麻烦

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值