太弱...假期就这么昏过去了
A.快速幂,没思路...
B.太长没看...
C.稳定排序
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
struct pp {
char aa[55];
int bb;
int cc;
}point[305];
bool cmp(pp a,pp b)
{
return (a.bb>b.bb)||(a.bb==b.bb&&a.cc<b.cc);
}
int main()
{
int tot;
char name[55];
int ans;
int judge1=0,judge2=0;
while(scanf("%d",&tot)!=EOF)
{
memset(point,0,sizeof(point));
judge1=0;
judge2=0;
for(int i=0;i<tot;i++)
{
point[i].cc=i;
scanf("%s%d",point[i].aa,&point[i].bb);
}
sort(point,point+tot,cmp);
for(int i=0;i<tot;i++)
{
scanf("%s%d",name,&ans);
if(ans!=point[i].bb)
judge1=1;
else if(strcmp(name,point[i].aa)!=0)
judge2=1;
}
if(judge1==1)
{
printf("Error\n");
for(int i=0;i<tot;i++)
printf("%s %d\n",point[i].aa,point[i].bb);
}
else if(judge2==1)
{
printf("Not Stable\n");
for(int i=0;i<tot;i++)
printf("%s %d\n",point[i].aa,point[i].bb);
}
else printf("Right\n");
}
}
D.优先队列
#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;
struct stone{
int dis,thr;
int now;
}ss;
int vv[100005];
struct stone2{
bool operator () (stone a,stone b)
{
return a.now>b.now||(a.now==b.now&&a.thr>b.thr);
}
};
int main()
{
int tot,tt;
int x,y;
scanf("%d",&tot);
priority_queue<stone,vector <stone>,stone2>s;
for(int i=0;i<tot;i++)
{
int ans=0;
int sum=0;
scanf("%d",&tt);
memset(vv,0,sizeof(vv));
for(int i=0;i<tt;i++)
{
scanf("%d%d",&x,&y);
ss.dis=x;
ss.thr=y;
ss.now=x;
s.push(ss);
}
int ii=0;
while(sum<tt)
{
ii++;
if(ii%2==0)
{
ans=s.top().now;
s.pop();
sum++;
}
else
{
ss.dis=s.top().dis;
ss.now=s.top().thr+s.top().now;
ss.thr=s.top().thr;
s.pop();
s.push(ss);
}
}
printf("%d\n",ans);
}
}
E.后来看的,点太多暴力T,凸包以后再搜边上的点,要<=top
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <math.h>
using namespace std;
const int MAXN=50050;
struct point
{
int x,y;
};
point list[MAXN];
int stack[MAXN],top;
int cross(point p0,point p1,point p2)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}
int dis(point p1,point p2)
{
return (p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y);
}
bool cmp(point p1,point p2)
{
int tmp=cross(list[0],p1,p2);
if(tmp>0) return true;
else if(tmp==0&&dis(list[0],p1)<dis(list[0],p2)) return true;
else return false;
}
void init(int n)
{
int i,k;
point p0;
scanf("%d%d",&list[0].x,&list[0].y);
p0.x=list[0].x;
p0.y=list[0].y;
k=0;
for(i=1;i<n;i++)
{
scanf("%d%d",&list[i].x,&list[i].y);
if( (p0.y>list[i].y) || ((p0.y==list[i].y)&&(p0.x>list[i].x)) )
{
p0.x=list[i].x;
p0.y=list[i].y;
k=i;
}
}
list[k]=list[0];
list[0]=p0;
sort(list+1,list+n,cmp);
}
void graham(int n)
{
int i;
if(n==1)
{top=0;stack[0]=0;}
if(n==2)
{
top=1;
stack[0]=0;
stack[1]=1;
}
if(n>2)
{
for(i=0;i<=1;i++) stack[i]=i;
top=1;
for(i=2;i<n;i++)
{
while(top>0&&cross(list[stack[top-1]],list[stack[top]],list[i])<=0)
top--;
top++;
stack[top]=i;
}
}
}
int main()
{
int tot;
while(scanf("%d",&tot)!=EOF)
{
memset(list,0,sizeof(list));
memset(stack,0,sizeof(stack));
int maxn=0;
init(tot);
graham(tot);
for(int i=0;i<=top;i++)
{
for(int j=i+1;j<=top;j++)
{
if(dis(list[stack[i]],list[stack[j]])>maxn)
maxn=dis(list[stack[i]],list[stack[j]]);}
}printf("%d\n",maxn);
}
}
F.BFS,开始时候用的DFS,一直没想明白为什么会wa..
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#define maxn 205
using namespace std;
int num[maxn];
int tot,a,b;
int judge;
int vv[maxn]={0};
struct point
{
int ff;
int step;
};
int bfs()
{
judge=0;
queue <point> ss;
point st,en;
point s1,s2;
st.ff=a;
st.step=0;
en.ff=b;
en.ff=tot+1;
ss.push(st);
vv[a]=1;
while(!ss.empty())
{
if(ss.front().step>=tot)
break;
if(ss.front().ff==b)
{
printf("%d\n",ss.front().step);
judge=1;
break;
}
s1=ss.front();
s2.ff=s1.ff+num[s1.ff];
s2.step=s1.step+1;
if(s2.ff<=tot&&s2.ff>=1&&!vv[s2.ff])
{
vv[s2.ff]=1;
ss.push(s2);
}
s2.ff=s1.ff-num[s1.ff];
s2.step=s1.step+1;
if(s2.ff<=tot&&s2.ff>=1&&!vv[s2.ff])
{
vv[s2.ff]=1;
ss.push(s2);
}
ss.pop();
}
}
int main()
{
while(scanf("%d",&tot)&&tot!=0)
{
scanf("%d%d",&a,&b);
memset(vv,0,sizeof(vv));
memset(num,0,sizeof(num));
for(int i=1;i<=tot;i++)
scanf("%d",&num[i]);
bfs();
if(judge==0)
printf("-1\n");
}
return 0;
}
G.SPFA?没学过呢,看看..