样例
in:
5 1 1
out:
1
算法1
(暴力枚举、模拟) $O((n+1)/2)$
这道题很简单,不用找很多规律,太难了,更不要开二维数组(mle),其实只要模拟一下,并且找一个简单规律即可
首先我们发现一个矩阵有固定圈数,偶数时为n/2,奇数时(n+1)/2,整合得(n+1)/2
然后找到发现每一圈的格数为4(t-1)(t为一行的格数)
再发现从最外一圈每向内缩一圈,t就-=2
每一圈只有4个情况:
1.第i行第e列(i为每圈首行)
2.第e行第i列(i为每圈首列)
3.第e行第in列(in为每圈末列)
4.第in行第e列(in为每圈末行)
所以枚举即可
时间复杂度
总共进行2次循环,第一次为圈数$(n+1)/2$,第二次为i,j所在圈数次,所以总时间复杂度约等于2*n,舍掉2得$O(n)$
C++ 代码
#include<iostream>//can`t
#include<cstdio>//can`t
#include<cstring>//can`t
#include<string>//can`t
#include<cmath>
#include<fstream>
#include<sstream>
#include<ctime>//can`t
#include<cstdlib>//can`t
#include<cfloat>//can`t
#include<climits>//can`t
#include<iomanip>
#include<stack>//can`t
#include<queue>//can`t
#include<deque>//can`t
#include<map>//can`t
#include<set>//can`t
#include<bitset>//can`t
#include<vector>//can`t
#include<algorithm>//can`t
#define ll long long
#define ui unsigned
#define re register
#define fop(a) freopen(#a".in","r",stdin),freopen(#a".out","w",stdout)
#define fc() fclose(stdin),fclose(stdout)
using namespace std;//can`t
inline void myread(int&x){
char a,f=1;
x=0;
a=getchar();
if(a>='0'&&a<='9')x=a-48;
else if(a=='-')f=-1;
while(1){
a=getchar();
if(a>='0'&&a<='9')x=x*10+a-48;
else {x=x*f;return;
}
}
}
inline void mywrite(int x){
if(x<0)putchar('-'),x=-x;
if(x>9)mywrite(x/10);
putchar(x%10+48);
}
int main(){
#ifdef DEBUG
fop(matrix);
#endif
int n,x,y,fl=-1,fl1=-1,fl2=-1;
cin>>n>>x>>y;
int q=(n+1)/2;
for(re int i=1,j=n;i<=q;++i,--j){
if(x==i&&y>=i&&y<=j){
fl=i;
fl1=j;
fl2=1;
break;
}else if(y==j&&x>=i&&x<=j){
fl=i;
fl1=j;
fl2=3;
break;
}else if(x==j&&y>=i&&y<=j){
fl=i;
fl1=j;
fl2=4;
break;
}else if(y==i&&x>=i&&x<=j){
fl=i;
fl1=j;
fl2=2;
break;
}
}
int ans=0;
for(re int i=1,j=n;i<fl;++i,j-=2){
ans+=4*j-4;
}
if(fl2==1){
cout<<ans+1+(y-fl);
}else if(fl2==3){
int r=ans+1+(fl1-fl);
cout<<x-fl+r;
}else if(fl2==4){
int rs=ans+1+(fl1-fl);
int rss=rs+fl1-fl;
cout<<rss+(fl1-y);
}else if(fl2==2){
int ra=ans+1+(fl1-fl);
int raa=ra+fl1-fl;
int raaa=raa+fl1-fl;
cout<<fl1-x+raaa;
}
fc();
return 0;
}//hicode002所有,禁止抄袭