【刷题】AtCoder Regular Contest 003
A.GPA計算
题意:\(n\) 个人,一个字符串表示每个人的等第,每种等第对应一种分数。问平均分
做法:算
#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
#define REP(a,b,c) for(register int a=(b),a##end=(c);a<=a##end;++a)
#define DEP(a,b,c) for(register int a=(b),a##end=(c);a>=a##end;--a)
const int MAXN=100+10;
int n,ans,val[]={4,3,2,1,0};
char s[MAXN];
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
int main()
{
freopen("A.in","r",stdin);
freopen("A.out","w",stdout);
read(n);scanf("%s",s+1);
REP(i,1,n)ans+=val[s[i]-'A'];
printf("%.14f\n",(db)ans/n);
return 0;
}
B.さかさま辞書
题意:字符串按字典序从后往前排序
做法:reverse后sort就好了
#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
#define REP(a,b,c) for(register int a=(b),a##end=(c);a<=a##end;++a)
#define DEP(a,b,c) for(register int a=(b),a##end=(c);a>=a##end;--a)
const int MAXN=100+10;
int n;
std::string s[MAXN];
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
int main()
{
read(n);
REP(i,1,n)std::cin>>s[i],std::reverse(s[i].begin(),s[i].end());
std::sort(s+1,s+n+1);
REP(i,1,n)std::reverse(s[i].begin(),s[i].end()),std::cout<<s[i]<<'\n';
return 0;
}
C.暗闇帰り道
题意:给一个 \(n*m\) 的矩阵,每个位置上除起点、终点、障碍之外都有一个权值。
先在要从起点走到终点,不经过障碍。一步要一秒的时间,一条路径的价值是路径上所有位置价值的最小值。在 \(t\) 秒时,一个位置的价值是它的权值乘上 \(0.99^t\)
做法:从起点开始做要考虑时间问题,不够方便。于是从终点开始往回走。每走一步,记录的最小值乘上 \(0.99\) 与新到达的点的权值取min就好了
#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
#define REP(a,b,c) for(register int a=(b),a##end=(c);a<=a##end;++a)
#define DEP(a,b,c) for(register int a=(b),a##end=(c);a>=a##end;--a)
const int MAXN=500+10;
const ld eps=1e-10;
int n,m,G[MAXN][MAXN],sx,sy,tx,ty,dr[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
ld V[MAXN][MAXN];
char s[MAXN];
struct node{
int x,y;
ld val;
};
std::queue<node> q;
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
int main()
{
read(n);read(m);
REP(i,1,n)
{
scanf("%s",s+1);
REP(j,1,m)
if(s[j]=='s')sx=i,sy=j,G[i][j]=10;
else if(s[j]=='g')tx=i,ty=j,G[i][j]=10;
else if(s[j]=='#')G[i][j]=-1;
else G[i][j]=s[j]^'0';
}
REP(i,1,n)REP(j,1,m)V[i][j]=-1.0;
V[tx][ty]=10.0;
q.push((node){tx,ty,10.0});
while(!q.empty())
{
node pr=q.front();
q.pop();
int x=pr.x,y=pr.y;
ld now=pr.val*0.99;
REP(i,0,3)
{
int dx=x+dr[i][0],dy=y+dr[i][1];
if(dx<1||dx>n||dy<1||dy>m||G[dx][dy]==-1)continue;
ld nxt=min(now,(ld)G[dx][dy]);
if(nxt-V[dx][dy]>eps)V[dx][dy]=nxt,q.push((node){dx,dy,nxt});
}
}
if(V[sx][sy]==-1)puts("-1");
else printf("%.10Lf\n",V[sx][sy]);
return 0;
}
D.シャッフル席替え
题意:开始时,\(n\) 个人从小到大围坐在一个圆桌旁边。现在 \(K\) 次操作,每次
等概率选择两个人,将他们两个交换座位。给出 \(m\) 个限制,\(u\) 和 \(v\) 不能坐在一起。问 \(K\)次操作后满足所有限制的概率是多少。
做法:数据范围过小,所以多次rand后模拟就好了,做 \(2e6\) 次左右可以出误差较小的答案了
#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
#define REP(a,b,c) for(register int a=(b),a##end=(c);a<=a##end;++a)
#define DEP(a,b,c) for(register int a=(b),a##end=(c);a>=a##end;--a)
int n,m,k,Count=2e6,cnt,a[20],b[20],p[20],ps[20];
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline void rand_solve()
{
REP(i,0,n-1)p[i]=i;
REP(i,1,k)
{
int u=rand()%n,v=rand()%n;
if(u==v){--i;continue;}
std::swap(p[u],p[v]);
}
}
inline void calc_ans()
{
REP(i,0,n-1)ps[p[i]]=i;
REP(i,1,m)if(abs(ps[a[i]]-ps[b[i]])==1||abs(ps[a[i]]-ps[b[i]])==n-1)return ;
cnt++;
}
int main()
{
srand(time(0));
read(n);read(m);read(k);
REP(i,1,m)read(a[i]),read(b[i]);
REP(i,1,Count)rand_solve(),calc_ans();
printf("%.5f\n",(db)cnt/(db)Count);
return 0;
}