有问题自己问,看不懂可以看看别的题解,我个人认为讲的比较清楚。
皇后
模拟程序
ll Map[1025][1025];
void solve(){
int n;
cin>>n;
ffor(i,1,n){
ffor(j,1,n){
cout<<Map[i][j]<<__;
}cout<<endl;
}
bool flag = 1;
while(flag){
int x,y;
cin>>x>>y;
if(Map[x][y]%2 == 0){
Map[x][y] = -1;
}else{
cout<<"ERROR INPUT"<<endl;
continue;
}
ffor(i,1,n){
if(Map[i][y] != -1)Map[i][y]++;
if(Map[x][i]!=-1)Map[x][i]++;
if(x-i>0 && y-i>0 && Map[x-i][y-i]!=-1){
Map[x-i][y-i] ++;
}if(x+i<=n && y+i<=n && Map[x+i][y+i]!=-1){
Map[x+i][y+i] ++;
}if(x-i>0 && y+i<=n && Map[x-i][y+i]!=-1){
Map[x-i][y+i] ++;
}if(x+i<=n && y-i>0 && Map[x+i][y-i]!=-1){
Map[x+i][y-i] ++;
}
}flag = 0;
ffor(i,1,n){
ffor(j,1,n){
cout<<setw(5)<<Map[i][j]<<__;
if(Map[i][j]%2 == 0){
flag = 1;
}
}cout<<endl;
}
}
cout<<"No More Free Point!"<<endl;
}你如果自己随便玩玩的话,就会发现 似乎没有什么规律,但从 开始就可以推导后续的放入方式。
首先,打表易得若 为奇数时,答案为 ,若 为偶数时,答案为 ,当然 是个特例。
考虑怎么处理 的情况,容易发现: 可以从 的情况推导而来。我们可以填充掉外面的两层,把 变成 的情况。画图容易知道,如果外面两层全部被渲染,则中间的部分恰好全部为偶数,与 子问题无异。
故问题有解:问题可以预处理 的构造方案,随后递归处理即可。构造方案可以自己手玩。
代码
void dfs(int x){
if(x <= 2){
cout<<"1 1"<<endl;
return ;
}else if(x == 3){
cout<<"2 3"<<endl;
cout<<"3 1"<<endl;
cout<<"2 2"<<endl;
cout<<"1 1"<<endl;
cout<<"3 3"<<endl;
cout<<"3 2"<<endl;
cout<<"1 2"<<endl;
cout<<"1 3"<<endl;
cout<<"2 1"<<endl;
return ;
}else if(x == 4){
cout<<"1 1"<<endl;
cout<<"2 4"<<endl;
cout<<"2 1"<<endl;
cout<<"3 1"<<endl;
cout<<"1 2"<<endl;
cout<<"1 3"<<endl;
cout<<"4 3"<<endl;
cout<<"4 2"<<endl;
cout<<"3 4"<<endl;
cout<<"3 2"<<endl;
cout<<"4 1"<<endl;
cout<<"4 4"<<endl;
cout<<"2 3"<<endl;
cout<<"2 2"<<endl;
return ;
}
cout<<"1 "<<x-1<<endl;
cout<<x-1<<" 2"<<endl;
cout<<"1 "<<x<<endl;
cout<<x-1<<" 1"<<endl;
cout<<"2 "<<x<<endl;
cout<<"2 "<<x-1<<endl;
cout<<x<<" 1"<<endl;
cout<<x<<" 2"<<endl;
ffor(i,3,x-2){
if(i%2 == 1){
cout<<i<<__<<x-1<<endl;
cout<<i<<__<<x<<endl;
cout<<x<<__<<i<<endl;
cout<<x-1<<__<<i<<endl;
}else{
cout<<i<<__<<x<<endl;
cout<<i<<__<<x-1<<endl;
cout<<x-1<<__<<i<<endl;
cout<<x<<__<<i<<endl;
}
}
cout<<x-1<<__<<x-1<<endl;
cout<<x-1<<__<<x<<endl;
cout<<x<<__<<x<<endl;
cout<<x<<__<<x-1<<endl;
dfs(x-2);
}
void solve(){
int n;
cin>>n;
if(n == 2){//不加只有94分,别问,自己试的
cout<<1<<endl;
cout<<"1 1"<<endl;
return ;
}
if(n%2 == 0){
cout<<n*n-2<<endl;
}else{
cout<<n*n<<endl;
}
dfs(n);
}奇偶矩阵
我还没写,让我写会……
最罕见的昆虫
从二分角度考虑如何放置昆虫。
首先正着扫一遍你就可以知道一共有多少种昆虫。
考虑进行二分答案:我们中值处理出现次数。判断剩下的数字是否可以填充满即可。
由于会存在神秘情况导致被卡,所以你需要 shuffle 一下。
代码
//#define Local
#ifdef Local
#include "insects.h"
#else
void move_inside(int i);
void move_outside(int i);
int press_button();
#endif
const int N = 2005;
ll h[N];
bool chk[N],chk2[N];
mt19937 bblb(time(0));
int min_cardinality(int n) {
ffor(i,1,n)h[i] = i-1;
shuffle(h+1,h+1+n,bblb);
int cnts = 0;
ffor(i,1,n){
move_inside(h[i]);
chk[h[i]] = 1;
if(press_button() == 2){
move_outside(h[i]);
chk[h[i]] = 0;
}else cnts++;
}
if(cnts == 1)return n;
int l=1,r=n/cnts,mid,c;
c = cnts;
while(l<r){
shuffle(h+1,h+1+n,bblb);
mid = (l+r+1)>>1;
memset(chk2,0,sizeof chk2);
ll cnt2 = 0;
ffor(i,1,n){
if(!chk[h[i]]){
move_inside(h[i]);
if(press_button()>mid){
move_outside(h[i]);
}else{
cnts ++;
chk2[h[i]] = 1;
chk[h[i]] = 1;
}if(cnts >= c*mid)break;
}
}
if(cnts < c*mid){
r = cnts/c;
ffor(i,1,n){
if(chk2[h[i]]){
chk[h[i]] = 0;
move_outside(h[i]);
cnts--;
}else{
chk[h[i]] = 1;
}
}
}else{
l = mid;
}
}
return l;
}落花雨等 MX 比赛
自己看题解。