http://acm.hdu.edu.cn/showproblem.php?pid=5071
Chat Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 702 Accepted Submission(s): 163

1 18 Prior Add 1 Chat 1 Add 2 Chat 2 Top 2 Chat 3 Untop Chat 4 Choose 2 Chat 5 Rotate 2 Chat 4 Close 2 Add 3 Prior Chat 2 Close 1
Operation #1: empty. Operation #2: success. Operation #3: success. Operation #4: success. Operation #5: success. Operation #6: success. Operation #7: success. Operation #8: success. Operation #9: success. Operation #10: success. Operation #11: success. Operation #12: success. Operation #13: success. Operation #14: close 2 with 8. Operation #15: success. Operation #16: success. Operation #17: success. Operation #18: close 1 with 11. Bye 3: 2HintThis problem description does not relate to any real person in THU.
事实上这几天真的不想面对这题。弱成逗比。真是令人忧伤。现场赛没出这题全然是由于自己太弱,就是一道SB模拟题,当时就想优化常数。结果优成死循环。看来丽洁说得没错,我果然还是sometimes naive。。
。
题意:
自己读。
分析:
直接用数组O(n^2)模拟。能够用红黑树优化常数,现场赛手写双向链SB了结果死循环。要注意的是最后Bye的时候要先考虑Top。然后就是注意单词拼写,不要搞混下标,敲的时候不SB即可。
/*
*
* Author : fcbruce <fcbruce8964@gmail.com>
*
* Time : Sat 25 Oct 2014 09:30:24 AM CST
*
*/
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10
#ifdef _WIN32
#define lld "%I64d"
#else
#define lld "%lld"
#endif
#define maxm
#define maxn 8964
using namespace std;
struct __chat
{
int u;
long long c;
}chat[maxn];
int TOP,cnt;
map<int,int> MAP;
void Add()
{
int u;
scanf("%d",&u);
if (MAP.count(u)!=0)
{
puts("same priority.");
return ;
}
MAP[u]=cnt;
chat[cnt].u=u;
chat[cnt].c=0;
cnt++;
puts("success.");
}
void Close()
{
int u;
scanf("%d",&u);
if (MAP.count(u)==0)
{
puts("invalid priority.");
return ;
}
if (TOP==u) TOP=-1;
int the_one=MAP[u];
printf("close %d with "lld".\n",chat[the_one].u,chat[the_one].c);
for (int i=the_one;i<cnt-1;i++)
{
chat[i]=chat[i+1];
MAP[chat[i].u]=i;
}
cnt--;
MAP.erase(u);
}
void Chat()
{
int w;
scanf("%d",&w);
if (cnt==0)
{
puts("empty.");
return ;
}
if (TOP!=-1)
chat[MAP[TOP]].c+=w;
else
chat[0].c+=w;
puts("success.");
}
void Rotate()
{
int x;
scanf("%d",&x);
if (x<1 || x>cnt)
{
puts("out of range.");
return ;
}
__chat temp=chat[x-1];
for (int i=x-1;i>0;i--)
{
chat[i]=chat[i-1];
MAP[chat[i].u]=i;
}
chat[0]=temp;
MAP[temp.u]=0;
puts("success.");
}
void Prior()
{
if (cnt==0)
{
puts("empty.");
return ;
}
int the_one=0;
for (int i=1;i<cnt;i++)
if (chat[the_one].u<chat[i].u) the_one=i;
__chat temp=chat[the_one];
for (int i=the_one;i>0;i--)
{
chat[i]=chat[i-1];
MAP[chat[i].u]=i;
}
chat[0]=temp;
MAP[temp.u]=0;
puts("success.");
}
void Choose()
{
int u;
scanf("%d",&u);
if (MAP.count(u)==0)
{
puts("invalid priority.");
return ;
}
int the_one=MAP[u];
__chat temp=chat[the_one];
for (int i=the_one;i>0;i--)
{
chat[i]=chat[i-1];
MAP[chat[i].u]=i;
}
chat[0]=temp;
MAP[temp.u]=0;
puts("success.");
}
void Top()
{
int u;
scanf("%d",&u);
if (MAP.count(u)==0)
{
puts("invalid priority.");
return ;
}
TOP=u;
puts("success.");
}
void Untop()
{
if (TOP==-1)
puts("no such person.");
else
{
TOP=-1;
puts("success.");
}
}
char op[233];
int main()
{
#ifdef FCBRUCE
freopen("/home/fcbruce/code/t","r",stdin);
#endif // FCBRUCE
int T_T;
scanf("%d",&T_T);
while (T_T--)
{
int n;
scanf("%d",&n);
TOP=-1;
cnt=0;
MAP.clear();
for (int i=0;i<n;i++)
{
printf("Operation #%d: ",i+1);
scanf("%s",op);
if (strcmp(op,"Add")==0)
Add();
else if(strcmp(op,"Close")==0)
Close();
else if(strcmp(op,"Chat")==0)
Chat();
else if(strcmp(op,"Rotate")==0)
Rotate();
else if(strcmp(op,"Prior")==0)
Prior();
else if(strcmp(op,"Choose")==0)
Choose();
else if(strcmp(op,"Top")==0)
Top();
else
Untop();
}
if (TOP!=-1)
if (chat[MAP[TOP]].c!=0)
printf("Bye %d: "lld"\n",TOP,chat[MAP[TOP]].c);
for (int i=0;i<cnt;i++)
if (chat[i].u!=TOP && chat[i].c>0)
printf("Bye %d: "lld"\n",chat[i].u,chat[i].c);
}
return 0;
}
原文:http://www.cnblogs.com/bhlsheji/p/5354978.html