昨天是暑假集训的第二天。训练内容是2014年鞍山现场赛的题目。可能太久没有训练。大家都有点生疏。基本都是自己做自己的题,全程没什么交流。五个小时,自己坑出来两道。一道DP和一道水题。晚上又看了一下B题,是一道简单的模拟题。但是写了二十多遍一直都WA。一直到早上都没有找到问题在哪里。后来模块测试发现问题在于查找位置那里,把查找方法改了之后就过了。具体WA的原因不知道╮(╯▽╰)╭ 可能是边界数据吧。
大概解释一下B题。这道题就是说要自己模拟一个聊天系统。有八种功能:
Add u:是添加一个聊天窗口,添加到列表的最后。需要特别判定列表是否为空。
Close u:关闭优先级为u的窗口,关闭之后,这个窗口之后的每个窗口都要向前移动一位,需要特别判定是否为top和是否存在u。
Chat u:和top girl聊天,句数为u,如果没有top girl就和列表第一个聊天,需要特别判定是否为空。
Rotate u:将第u位的聊天窗口调到第一位,u之前的全部向后移动一位。需要判断所给的u是否在1-s之间。
Prior:将优先级最大的移到第一位,需要特别判定是否为空。
Choose u:选择优先级为u的窗口移到第一位,需要判定是否存在u。
top u:将优先级为u的窗口设定为top,top只是一种状态,而不是真的移到第一位。
untop:如果存在top girl就取消,如果不存在就输出no such person。
最后说Bye的时候,首先和top girl说,如果没有top girl的话就按照队列顺序依次和女孩说Bye。需要注意的是必须之前说过话才会说Bye。
输出要注意格式。
先附上AC代码:
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <iostream>
#define N 5010
using namespace std;
int s=0,to=0;
struct girl
{
int u;
long long w;
void init(int x)
{
u=x;
w=0;
}
};
girl m[5010];
void totop(int w)
{
girl temp=m[w];
for(int i=w-1;i>=0;i--)
m[i+1]=m[i];
m[0]=temp;
}
void add(int x)
{
int i;
for(i=0;i<s;i++)
{
if(m[i].u==x)
break;
}
if(i!=s)
printf("same priority");
else
{
m[s++].init(x);
printf("success");
}
}
void close(int x)
{
int i,j;
for(i=0;i<s;i++)
{
if(m[i].u == x)
break;
}
if(i==s)
printf("invalid priority");
else
{
if(to==x)
to=0;
printf("close %d with %I64d",m[i].u,m[i].w);
for(j=i;j<s-1;j++)
{
m[j]=m[j+1];
}
s--;
}
}
void chat(int x)
{
int i=0;
if(s==0)
printf("empty");
else if(to!=0)
{
for(i=0;i<s;i++)
{
if(m[i].u==to)
{
m[i].w+=x;
printf("success");
break;
}
}
}
else
{
m[i].w+=x;
printf("success");
}
}
void rotate(int x)
{
if(x<1||x>s)
printf("out of range");
else
{
totop(x-1);
printf("success");
}
}
void prior()
{
int i,max=0;
if(s==0)
printf("empty");
else
{
for(i=1;i<s;i++)
{
if(m[i].u>m[max].u)
max=i;
}
totop(max);
printf("success");
}
}
void choose(int x)
{
int i;
for(i=0;i<s;i++)
{
if(m[i].u==x)
break;
}
if(i!=s)
{
totop(i);
printf("success");
}
else
printf("invalid priority");
}
void top(int x)
{
int i;
for(i=0;i<s;i++)
{
if(m[i].u==x)
break;
}
if(i!=s)
{
to=x;
printf("success");
}
else
printf("invalid priority");
}
void untop()
{
if(to==0)
printf("no such person");
else
{
to=0;
printf("success");
}
}
void close_all()
{
int i,w=-1;
if(to)
{
for(i=0;i<s;i++)
{
if(m[i].u==to&&m[i].w!=0)
{
w=i;
break;
}
}
if(w!=-1)
printf("Bye %d: %I64d\n",to,m[w].w);
}
for(i=0;i<s;i++)
{
if(i!=w&&m[i].w!=0)
{
printf("Bye %d: %I64d\n",m[i].u,m[i].w);
}
}
}
int main()
{
int T;
int n;
int k;
int m;
string str;
scanf("%d",&T);
while(T--)
{
s=0;
to=0;
scanf("%d",&n);
for(k=1;k<=n;k++)
{
cin>>str;
if(str=="Add")
{
scanf("%d",&m);
printf("Operation #%d: ",k);
add(m);
}
if(str=="Close")
{
scanf("%d",&m);
printf("Operation #%d: ",k);
close(m);
}
if(str=="Chat")
{
scanf("%d",&m);
printf("Operation #%d: ",k);
chat(m);
}
if(str=="Rotate")
{
scanf("%d",&m);
printf("Operation #%d: ",k);
rotate(m);
}
if(str=="Prior")
{
printf("Operation #%d: ",k);
prior();
}
if(str=="Choose")
{
scanf("%d",&m);
printf("Operation #%d: ",k);
choose(m);
}
if(str=="Top")
{
scanf("%d",&m);
printf("Operation #%d: ",k);
top(m);
}
if(str=="Untop")
{
printf("Operation #%d: ",k);
untop();
}
printf(".\n");
}
close_all();
}
return 0;
}
再附上改正查找方式之前的代码:
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <iostream>
#define N 5010
using namespace std;
int s=0,to=0;
struct girl
{
int u;
long long w;
void init(int x)
{
u=x;
w=0;
}
};
girl a[N];
void totop(int x)
{
int i;
girl temp=a[x];
for(i=x-1;i>0;i--)
a[i+1]=a[i];
a[0]=temp;
}
int find(int x)
{
int i;
for(i=0;i<s;i++)
{
if(a[i].u==x)
return i;
}
return -1;
}
void add(int x)
{
if(find(x)!=-1)
printf("same priority.\n");
else
{
a[s].init(x);
printf("success.\n");
s++;
}
}
void close(int x)
{
int i;
if(find(x)==-1)
printf("invalid priority.\n");
else
{
printf("close %d with %I64u\n",a[find(x)].u,a[find(x)].w);
if(x==to)
to=0;
for(i=find(x);i<s-1;i++)
a[i]=a[i+1];
s--;
}
}
void chat(int x)
{
if(s<=0)
printf("empty.\n");
else
{
if(to!=0)
{
a[find(to)].w+=x;
}
else
a[0].w+=x;
printf("success.\n");
}
}
void rotate(int x)
{
if(x>s||x<1)
printf("out of range.\n");
else
{
totop(x-1);
printf("success.\n");
}
}
void prior()
{
if(s==0)
printf("empty.\n");
else
{
int max=0,i;
for(i=0;i<s;i++)
{
if(a[i].u>a[max].u)
max=i;
}
totop(max);
printf("success.\n");
}
}
void choose(int x)
{
if(find(x)==-1)
printf("invalid priority.\n");
else
{
totop(find(x));
printf("success.\n");
}
}
void top(int x)
{
if(find(x)==-1)
printf("invalid priority.\n");
else
{
to=x;
printf("success.\n");
}
}
void untop()
{
if(to==0)
printf("no such person.\n");
else
{
to=0;
printf("success.\n");
}
}
void close_all()
{
int i,w=-1;
if(to)
{
for(i=0;i<s;i++)
{
if(a[i].u==to&&a[i].w!=0)
{
w=i;
break;
}
}
if(w!=-1)
printf("Bye %d: %I64d\n",to,a[w].w);
}
for(i=0;i<s;i++)
{
if(i!=w&&a[i].w!=0)
{
printf("Bye %d: %I64d\n",a[i].u,a[i].w);
}
}
}
int main()
{
int T;
int n;
int k;
int m;
string str;
scanf("%d",&T);
while(T--)
{
s=0;
to=0;
scanf("%d",&n);
for(k=1;k<=n;k++)
{
cin>>str;
if(str=="Add")
{
scanf("%d",&m);
printf("Operation #%d: ",k);
add(m);
}
if(str=="Close")
{
scanf("%d",&m);
printf("Operation #%d: ",k);
close(m);
}
if(str=="Chat")
{
scanf("%d",&m);
printf("Operation #%d: ",k);
chat(m);
}
if(str=="Rotate")
{
scanf("%d",&m);
printf("Operation #%d: ",k);
rotate(m);
}
if(str=="Prior")
{
printf("Operation #%d: ",k);
prior();
}
if(str=="Choose")
{
scanf("%d",&m);
printf("Operation #%d: ",k);
choose(m);
}
if(str=="Top")
{
scanf("%d",&m);
printf("Operation #%d: ",k);
top(m);
}
if(str=="Untop")
{
printf("Operation #%d: ",k);
untop();
}
}
close_all();
}
return 0;
}