最近状态极差...想用这套题练手结果B C都FST了...
一开始2分钟切掉A我还以为会很顺利...
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
给你一个n位密码锁初始状态。问你最少要转动几次可以开锁
这题我其实根本没看题目。看了一下样例和样例解释直接就开始写了
每一位分两种情况讨论然后把小的那种加进答案就可以了
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
有一串长度为n的数列。给你了其中的k个。让你构造剩下n-k个使得他们总和小于x且中位数不小于y
构造题。。先求出给你的k个中位数然后和y比较分情况讨论
加数的时候尽量加小的就可以了
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
C.Ice Cave
类似口袋妖怪双鹿市冰系道馆,“."表示没走过的冰,"x"表示走过一次的冰。x再走一次就会掉下去。问你可否从起点出发走到终点并且掉下去
dfs或者bfs都可做。。终点如果是.就再特判下旁边是否有两个. 然后如果起点和终点相邻就特判一下就可以了
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
给你若干个石头,剪刀和布。每次选两个决出胜负。问你最后只剩下石头,剪刀或者布的概率分别是多少
记忆划搜索。。
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
给你一个无限长的数列1,2,…… 有n个操作。每次交换i j位置上的数,问你操作结束后的逆序对数
离散化后直接做就可以了
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
code:
#include<cmath>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int lx;
cin>>lx;
string x1,x2;
cin>>x1>>x2;
int s=0;
int i;
for(i=0;i<lx;i++)
{
int xt=min(abs(x1[i]-x2[i]),abs(x1[i]+10-x2[i]));
s+=min(xt,abs(x2[i]+10-x1[i]));
}
printf("%d\n",s);
return 0;
}
B. School Marks
#include<cmath>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int a[100001],b[100001];
int main()
{
int n,k,p,x,y;
scanf("%d%d%d%d%d",&n,&k,&p,&x,&y);
int i;
int sum=0;
for(i=1;i<=k;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
sort(a+1,a+1+k);
int xt=a[(k+1)/2],xi=(k+1)/2;
int s=k;
if(xt<y)
{
while(xt<y&&xi<=k)
{
if(s%2==0)
xi++;
xt=a[xi];
s++;
b[s]=y;
sum+=y;
}
if(xt<y&&xi==k+1&&n-k<k||s>n)
{
printf("-1\n");
return 0;
}
int xtt=n-s;
for(i=1;i<=xtt;i+=2)
{
s++;
b[s]=y;
sum+=y;
s++;
b[s]=1;
sum+=1;
}
if(i==xtt+2&&xt==y)
{
s--;
b[s]=1;
sum-=y;
}
if(sum>x)
printf("-1\n");
else
for(i=k+1;i<=n;i++)
printf("%d ",b[i]);
}
else
{
while(xt>y&&xi>0&&s<n)
{
s++;
b[s]=1;
// if(s%2==1)
xi--;
xt=a[xi];
sum+=1;
}
if(xi==0)
xt=a[1];
if(xt<y)
{
xi++;
xt=a[xi];
sum-=1;
s--;
}
int xtt=n-s;
for(i=1;i<=xtt;i+=2)
{
s++;
b[s]=y;
sum+=y;
s++;
b[s]=1;
sum+=1;
}
if(i==xtt+2)
{
s--;
b[s]=1;
sum-=y;
}
if(sum>x||s>n)
printf("-1\n");
else
for(i=k+1;i<=n;i++)
printf("%d ",b[i]);
}
return 0;
}
C.Ice Cave
#include<cmath>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int map[1001][1001];
bool v[1001][1001];
int n,m;
int flag=false;
inline void dfs(int x,int y,int xx,int yy)
{
if(v[x][y])
return ;
v[x][y]=true;
if(x>n||x<1||y>m||y<1)
return ;
if(x==xx&&y==yy)
{
flag=true;
return ;
}
if(map[x][y]<=0)
return ;
map[x+1][y]--;
dfs(x+1,y,xx,yy);
if(flag)
return ;
map[x+1][y]++;
map[x][y+1]--;
dfs(x,y+1,xx,yy);
if(flag)
return ;
map[x][y+1]++;
map[x-1][y]--;
dfs(x-1,y,xx,yy);
if(flag)
return ;
map[x-1][y]++;
map[x][y-1]--;
dfs(x,y-1,xx,yy);
if(flag)
return ;
map[x][y-1]++;
}
int main()
{
scanf("%d%d",&n,&m);
int i,j;
string x;
for(i=1;i<=n;i++)
{
cin>>x;
for(j=1;j<=m;j++)
{
if(x[j-1]=='.')
map[i][j]=2;
else
map[i][j]=1;
}
}
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
bool flag2=false;
if(x2+1==x1&&y2==y1)
if((map[x2+1][y2]==2)+(map[x2-1][y2]==2)+(map[x2][y2+1]==2)+(map[x2][y2-1]==2)>=1)
flag2=true;
if(x2-1==x1&&y2==y1)
if((map[x2+1][y2]==2)+(map[x2-1][y2]==2)+(map[x2][y2+1]==2)+(map[x2][y2-1]==2)>=1)
flag2=true;
if(x2==x1&&y2+1==y1)
if((map[x2+1][y2]==2)+(map[x2-1][y2]==2)+(map[x2][y2+1]==2)+(map[x2][y2-1]==2)>=1)
flag2=true;
if(x2==x1&&y2-1==y1)
if((map[x2+1][y2]==2)+(map[x2-1][y2]==2)+(map[x2][y2+1]==2)+(map[x2][y2-1]==2)>=1)
flag2=true;
if((map[x2+1][y2]==2)+(map[x2-1][y2]==2)+(map[x2][y2+1]==2)+(map[x2][y2-1]==2)>=2)
flag2=true;
// map[x1][y1]=0;
dfs(x1,y1,x2,y2);
//bfs(x1,y1,x2,y2);
if(map[x2][y2]==0)
printf("YES\n");
else
{
if(!flag)
printf("NO\n");
else if(flag2||map[x2+1][y2]==2||map[x2-1][y2]==2||map[x2][y2+1]==2||map[x2][y2-1]==2)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
#include<cmath>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int map[101][101];
int ans[201][201];
bool v[101][101];
int main()
{
int n;
scanf("%d",&n);
int i,j;
string x;
for(i=1;i<=n;i++)
{
cin>>x;
for(j=1;j<=n;j++)
{
if(x[j-1]=='o')
map[i][j]=2;
else if(x[j-1]=='x')
map[i][j]=1;
}
}
int ii,jj;
for(i=1;i<=n*2-1;i++)
{
for(j=1;j<=n*2-1;j++)
{
int dx=i-n,dy=j-n;
bool flag=true;
for(ii=1;ii<=n;ii++)
{
for(jj=1;jj<=n;jj++)
{
if(map[ii][jj]==2)
{
if(ii+dx<1||ii+dx>n||jj+dy<1||jj+dy>n)
continue;
if(map[ii+dx][jj+dy]!=2&&map[ii+dx][jj+dy]!=1)
flag=false;
}
}
}
if(flag)
{
ans[i][j]=1;
for(ii=1;ii<=n;ii++)
{
for(jj=1;jj<=n;jj++)
{
if(map[ii][jj]==2)
{
if(ii+dx<1||ii+dx>n||jj+dy<1||jj+dy>n)
continue;
v[ii+dx][jj+dy]=true;
}
}
}
}
}
}
int flagx=true;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(map[i][j]==1&&!v[i][j])
flagx=false;
}
}
if(!flagx)
{
printf("NO\n");
return 0;
}
printf("YES\n");
ans[n][n]=2;
for(i=1;i<=n*2-1;i++)
{
for(j=1;j<=n*2-1;j++)
{
if(ans[i][j]==0)
printf(".");
else if(ans[i][j]==1)
printf("x");
else
printf("o");
}
printf("\n");
}
return 0;
}
#include<cstdio>
#include<algorithm>
using namespace std;
int tr[500001];
int pp;
inline int lowbit(int x)
{
return x&(-x);
}
inline int add(int x,int xx)
{
int i;
for(i=x;i<=pp;i+=lowbit(i))
tr[i]+=xx;
}
inline int sum(int x)
{
int i;
int ans=0;
for(i=x;i>=1;i-=lowbit(i))
ans+=tr[i];
return ans;
}
struct number
{
int x,y;
}a[100001];
int c[500001],s[500001];
int b[500001];
int xt[500001];
inline int find(int x)
{
int l=1,r=pp;
while(l<=r)
{
int mid=(l+r)/2;
if(c[mid]<x)
l=mid+1;
else
r=mid-1;
}
return l;
}
int main()
{
int n;
scanf("%d",&n);
int i;
int p=0;
for(i=1;i<=n;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
p++;
b[p]=a[i].x;
p++;
b[p]=a[i].y;
}
sort(b+1,b+1+p);
pp=0;
for(i=1;i<=p;i++)
{
if(b[i]!=b[i-1])
{
if(b[i]==b[i-1]+1)
{
pp++;
c[pp]=b[i];
s[pp]=1;
}
else
{
pp++;
c[pp]=b[i-1]+1;
s[pp]=b[i]-b[i-1]-1;
pp++;
c[pp]=b[i];
s[pp]=1;
}
}
}
for(i=1;i<=pp;i++)
xt[i]=i;
for(i=1;i<=n;i++)
{
int xx=find(a[i].x),yy=find(a[i].y);
int tt=xt[xx];
xt[xx]=xt[yy];
xt[yy]=tt;
}
long long sumx=0;
for(i=pp;i>=1;i--)
{
sumx+=(long long)sum(xt[i]-1)*(long long)s[xt[i]];
add(xt[i],s[xt[i]]);
}
printf("%I64d\n",sumx);
return 0;
}