Hdu 5071 大模拟

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5071
兔犇犇推荐的大模拟,之前拿去给学弟面试,搞得他们有点自闭,自己就顺便做一下看看。改了半天bug,终于ac了。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <vector>
#include <functional>
#include <ctime>
#include <cstdlib>
#include <sstream>
#include <set>
#include <deque>
using namespace std;
typedef long long LL;
typedef pair<int,LL> p2;
vector<p2> p;
int x,now;
int find(int x)
{
	for (int i=0;i<p.size();i++)
	{
		if (p[i].first==x) return i;
	}
	return -1;
}
void Delete(int x)
{
	int i=0;
	for (vector<p2>::iterator it = p.begin();it!=p.end();it++,i++)
	if (i==x)
	{
		p.erase(it);
		return;
	}
}
void Add()
{
	scanf("%d",&x);
	if (find(x)!=-1) puts("same priority.");
    else
	{
        p.push_back(make_pair(x,0));
        puts("success.");
    }
}
void Close()
{
	scanf("%d",&x);
	int cl=find(x);
	if (cl==-1) puts("invalid priority.");
	else
	{
		if (x==now) now=0;
		printf("close %d with %lld.\n",p[cl].first,p[cl].second);
        Delete(cl);
	}
}
void Chat()
{
	scanf("%d",&x);
	if(p.size()==0)
	{
        puts("empty.");
        return ;
	}
	if (now)
	{
		p[find(now)].second+=x;
			puts("success.");
	}
	else
	{
		p[0].second+=x;
		puts("success.");
	}
}
void Rotate(int x)
{
	if(x<1 || x>p.size()) puts("out of range.");   
	else
	{     
	   p2 y=p[x-1];       
	   Delete(x-1);        
	   p.insert(p.begin(),y);        
	   puts("success.");    
	}
}
void Prior()
{
    int mx=0,mxx=-1;
    if(p.size()==0) puts("empty.");
    else
	{
        for(int i = 0;i<p.size();++i)
            if(p[i].first>mx)
                mx=p[i].first,mxx=i;
        Rotate(mxx+1);
    }
}
void Choose()
{
	scanf("%d",&x);
	int y=find(x);
	if (y!=-1) Rotate(y+1);
	else puts("invalid priority.");
}
void Top()
{
	scanf("%d",&x);
	if (find(x)!=-1) now=x,puts("success.");
	else  puts("invalid priority.");
}
void Untop()
{
	if(now) now = 0,puts("success.");
    else puts("no such person.");
}
void Bye()
{
	if (now)
	{
		x=find(now);
		if(p[x].second!=0) printf("Bye %d: %I64d\n",p[x].first,p[x].second);
	}
	for (int i=0;i<p.size();i++)
	{
		if (p[i].second && i!=x) printf("Bye %d: %I64d\n",p[i].first,p[i].second);
	}
}
int main()
{
	int t,n;
	scanf("%d",&t);
	while (t--)
	{
		scanf("%d",&n);
		string op;
		p.clear();
		now=0;
		for(int cas=1;cas<=n;++cas)
		{
			cin>>op;
			int y;
			printf("Operation #%d: ",cas);
			if(op=="Add") Add();
			else if(op=="Close") Close();
			else if(op=="Chat") Chat();
			else if(op=="Rotate") scanf("%d",&y),Rotate(y);
			else if(op=="Prior") Prior();
			else if(op=="Choose") Choose();
			else if(op=="Top") Top();
			else if(op=="Untop") Untop();
		}
		Bye();
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值