Ad-hoc小计
Ad-hoc小计
T1 Grid Xor
显然,这题可以转化为
对于点灯问题的解决,套路化的的,对于这种相邻格问题,应该黑白染色,然后发现黑格白格互不影响,因此可以只考虑黑格
我们可以两条斜线一起考虑,如下图,如何让绿色的两条斜线一起被染色
显然的,我们可以将标有红三角的格子染色,对于其它斜线组,也是同理
找规律即可发现,每组斜线上的点坐标
:::success[code]
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int,int>
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define ivoid inline void
ivoid wsp(int x){
cout<<x<<" ";
}
ivoid wln(int x){
cout<<x<<"\n";
}
ivoid wstr(string s){
cout<<s<<"\n";
}
#define cout cerr
const int maxn=1e3+10;
int n,a[maxn][maxn],ans=0;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int T;cin>>T;
while(T--){
ans=0;
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>a[i][j];
}
}
for(int sum=4;sum<=2*n;sum+=4){
if(sum<=n){
for(int i=1;i<=n;i+=2){
int j=sum-i;
if(j<1) continue;
// cout<<i<<" "<<j<<"\n";
ans^=a[i][j];
ans^=a[i][n-j+1];
}
}
else{
for(int i=2;i<=n;i+=2){
int j=sum-i;
if(j>n) continue;
// cout<<i<<" "<<j<<"\n";
ans^=a[i][j];
ans^=a[i][n-j+1];
}
}
}
wln(ans);
}
return 0;
}
:::
T2 Construct the Binary Tree
发现对于固定的
考虑从链开始调整(完全二叉树不好调),每次选择深度最深的节点(一定是叶子),枚举转移到的父亲,要求:深度在转移后深度和合法的情况下最小,且至多有一个儿子,复杂度
下面来证明为什么调整是对的:
如果不能调整,此时一定是一棵完全二叉树,若
如果能调整,显然不会出现在第
因此能去到
Q.E.D!
:::success[code]
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int,int>
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define ivoid inline void
ivoid wsp(int x){
cout<<x<<" ";
}
ivoid wln(int x){
cout<<x<<"\n";
}
ivoid wstr(string s){
cout<<s<<"\n";
}
#define cout cerr
const int maxn=5e3+10;
int n,d,fa[maxn],dep[maxn];
int son[maxn];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int T;cin>>T;
while(T--){
memset(fa,0,sizeof(fa));
memset(dep,0,sizeof(dep));
memset(son,0,sizeof(son));
cin>>n>>d;
int sum_dep=0;
for(int i=2;i<=n;i++) fa[i]=i-1,dep[i]=dep[i-1]+1,sum_dep+=dep[i],son[fa[i]]=1;
while(sum_dep>d){
int md=0,pos=0;
for(int i=1;i<=n;i++){
if(dep[i]>md){
md=dep[i],pos=i;
}
}
son[fa[pos]]--;
int mid=1e18,to=0;
for(int i=1;i<=n;i++){
if(sum_dep-d>=md-(dep[i]+1)&&dep[i]<mid&&son[i]<2) mid=dep[i],to=i;
}
// cout<<md<<" "<<pos<<" "<<mid<<" "<<to<<" "<<sum_dep<<" "<<md-mid-1<<" "<<to<<" "<<son[to]<<"\n";
if(mid>=md-1) break;
sum_dep-=(md-mid-1);
fa[pos]=to;son[to]++;
dep[pos]=dep[to]+1;
}
if(sum_dep!=d) wstr("NO");
else{
wstr("YES");
for(int i=2;i<n;i++) wsp(fa[i]);
wln(fa[n]);
}
}
return 0;
}
:::
T3 Lenient Vertex Cover
线段树分治做法被卡空间了,回头补题解
T4 Errich-Tac-Toe (Hard Version)
像这种有限制操作数不大于
对于这题,注意到按
- 余 0 组所有 O 变成 K,余 1 组所有 K 变成 O
- 余 1 组所有 O 变成 K,余 2 组所有 K 变成 O
- 余 2 组所有 O 变成 K,余 0 组所有 K 变成 O
容易发现总有一组的操作数小于等于
:::success[code]
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int,int>
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define ivoid inline void
ivoid wsp(int x){
cout<<x<<" ";
}
ivoid wln(int x){
cout<<x<<"\n";
}
ivoid wstr(string s){
cout<<s<<"\n";
}
#define cout cerr
const int maxn=310;
int T,n,a[maxn][maxn];
string s;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>T;
while(T--){
cin>>n;
for(int i=1;i<=n;i++){
cin>>s;
for(int j=1;j<=n;j++){
if(s[j-1]=='.') a[i][j]=-1;
else if(s[j-1]=='O') a[i][j]=0;
else a[i][j]=1;
}
}
int cnt1=0,cnt2=0,cnt3=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if((i+j)%3==0){
cnt1+=(a[i][j]==1);
cnt3+=(a[i][j]==0);
}
else if((i+j)%3==1){
cnt1+=(a[i][j]==0);
cnt2+=(a[i][j]==1);
}
else{
cnt2+=(a[i][j]==0);
cnt3+=(a[i][j]==1);
}
}
}
if(cnt1<=cnt2&&cnt1<=cnt3){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if((i+j)%3==0){
if(a[i][j]!=-1) putchar('O');
else putchar('.');
}
else if((i+j)%3==1){
if(a[i][j]!=-1) putchar('X');
else putchar('.');
}
else{
if(a[i][j]==-1) putchar('.');
else if(a[i][j]==0) putchar('O');
else putchar('X');
}
}
putchar('\n');
}
}
else if(cnt2<=cnt3){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if((i+j)%3==1){
if(a[i][j]!=-1) putchar('O');
else putchar('.');
}
else if((i+j)%3==2){
if(a[i][j]!=-1) putchar('X');
else putchar('.');
}
else{
if(a[i][j]==-1) putchar('.');
else if(a[i][j]==0) putchar('O');
else putchar('X');
}
}
putchar('\n');
}
}
else{
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if((i+j)%3==2){
if(a[i][j]!=-1) putchar('O');
else putchar('.');
}
else if((i+j)%3==0){
if(a[i][j]!=-1) putchar('X');
else putchar('.');
}
else{
if(a[i][j]==-1) putchar('.');
else if(a[i][j]==0) putchar('O');
else putchar('X');
}
}
putchar('\n');
}
}
}
return 0;
}
:::
T5 汪了个汪
解法1:
可以将无序二元组的限制转化到图上,然后就是要找到
而对于
解法二:
开
:::success[code]
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int,int>
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define ivoid inline void
ivoid wsp(int x){
cout<<x<<" ";
}
ivoid wln(int x){
cout<<x<<"\n";
}
ivoid wstr(string s){
cout<<s<<"\n";
}
#define cout cerr
const int maxn=4e3+10;
int n,t;
int stac[maxn][maxn],tp[maxn],ans[maxn];
inline bool legal(int x){
return x>=1&&x<=n;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n>>t;
for(int i=1;i<=n;i++){
stac[i][++tp[i]]=i;
}
int now=-1;
for(int i=2;i<=n;i++){
now=-now;
for(int j=1;j<=n;j++){
int ne=stac[j][tp[j]]+((i-1)*now);
if(tp[j]<(i-1)||(!legal(ne))) continue;
stac[j][++tp[j]]=ne;
}
}
for(int i=1;i<=n;i++){
ans[tp[i]]=i;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=tp[ans[i]];j++) wsp(stac[ans[i]][j]);
wstr("");
}
return 0;
}
:::
T6 [APIO2022] 排列
记方案数为
考虑从小到大加数,容易想到一个操作数
考虑怎么优化,发现我们很讨厌二进制拆分中连续的
这样我们就将次数优化到了
但注意,要将数查到第二个数后面的前提是数列里有两个数且执行过
:::success[code]
#include<bits/stdc++.h>
using namespace std;
#define PII pair<int,int>
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define ivoid inline void
ivoid wsp(int x){
cout<<x<<" ";
}
ivoid wln(int x){
cout<<x<<"\n";
}
ivoid wstr(string s){
cout<<s<<"\n";
}
#define cout cerr
vector<int> ans;
int n=0;
void work(long long now){
if(now<=4){
while(--now) ans.insert(ans.begin(),n++);
return;
}
if(now%2==0) work(now>>1),ans.pb(n++);
else if(now%4==1) work(now-1),ans.insert(ans.begin(),n++);
else work(now-3),ans.insert(ans.begin()+2,n++);
}
vector<int> construct_permutation(long long k){
ans.clear();
n=0;
work(k);
return ans;
}
:::
T7 [BalticOI 2025] BOI acronym
显然,我们可以求出第一个
同理,我们也可以求出最后一个
接下来我们来考虑每一个位置
如果我们有
同理可以推出:如果
在代码中分讨一下即可
:::success[code]
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int,int>
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define ivoid inline void
ivoid wsp(int x){
cout<<x<<" ";
}
ivoid wln(int x){
cout<<x<<"\n";
}
ivoid wstr(string s){
cout<<s<<"\n";
}
#define cout cerr
const int maxn=2e3+10;
int n,ma[maxn][maxn],L,R;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n;
if(n==1){
wln(1);
return 0;
}
for(int i=1;i<=n;i++){
for(int j=i;j<=n;j++){
cin>>ma[i][j];
}
}
L=1,R=n;
for(int i=1;i<=n;i++){
if(ma[i][n]==ma[1][n]) L=i;
else break;
}
for(int i=n;i>=1;i--){
if(ma[1][i]==ma[1][n]) R=i;
else break;
}
wsp(L);
for(int i=L+1;i<R;i++){
if(ma[L][i]>ma[L+1][i]){
if(ma[L][i-1]<ma[L][i]) wsp(i);
continue;
}
if(ma[i][R]>ma[i][R-1]){
if(ma[i+1][R]<ma[i][R]) wsp(i);
continue;
}
if(ma[L+1][i]==ma[L+1][i-1]&&ma[i][R-1]==ma[i+1][R-1]) wsp(i);
}
wln(R);
return 0;
}
:::
T8 Double Knapsack
钦定
如果不存在,那么所有的
:::success[code]
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int,int>
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define ivoid inline void
ivoid wsp(int x){
cout<<x<<" ";
}
ivoid wln(int x){
cout<<x<<"\n";
}
ivoid wstr(string s){
cout<<s<<"\n";
}
#define cout cerr
const int maxn=1e6+10;
int n,a[maxn],b[maxn],sa[maxn],sb[maxn];
int lst[maxn],l[maxn],r[maxn];
bool flag=0;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i],sa[i]=sa[i-1]+a[i];
for(int i=1;i<=n;i++) cin>>b[i],sb[i]=sb[i-1]+b[i];
if(sa[n]>sb[n]){
flag=1;
for(int i=0;i<=n;i++) swap(a[i],b[i]),swap(sa[i],sb[i]);
}
for(int i=1,j=0;i<=n;i++){
while(sb[j]<sa[i]) j++;
l[i]=i,r[i]=j;
if(sa[i]==sb[j]){
if(flag){
wln(j);
for(int x=1;x<j;x++) wsp(x);
wln(j);
wln(i);
for(int x=1;x<i;x++) wsp(x);
wln(i);
}
else{
wln(i);
for(int x=1;x<i;x++) wsp(x);
wln(i);
wln(j);
for(int x=1;x<j;x++) wsp(x);
wln(j);
}
break;
}
if(lst[sb[j]-sa[i]]){
int l0=l[lst[sb[j]-sa[i]]],r0=r[lst[sb[j]-sa[i]]];
if(flag){
wln(j-r0);
for(int x=r0+1;x<j;x++) wsp(x);
wln(j);
wln(i-l0);
for(int x=l0+1;x<i;x++) wsp(x);
wln(i);
}
else{
wln(i-l0);
for(int x=l0+1;x<i;x++) wsp(x);
wln(i);
wln(j-r0);
for(int x=r0+1;x<j;x++) wsp(x);
wln(j);
}
break;
}
lst[sb[j]-sa[i]]=i;
}
return 0;
}
:::
T9 Square Table
注意到
因此取
:::success[code]
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int,int>
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define ivoid inline void
ivoid wsp(int x){
cout<<x<<" ";
}
ivoid wln(int x){
cout<<x<<"\n";
}
ivoid wstr(string s){
cout<<s<<"\n";
}
#define cout cerr
const int maxn=110;
int a[maxn],b[maxn],n,m;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n>>m;
if(n==1) a[1]=1;
else if(n==2) a[1]=3,a[2]=4;
else{
for(int i=1;i<n;i++) a[i]=2;
a[n]=n-2;
}
if(m==1) b[1]=1;
else if(m==2) b[1]=3,b[2]=4;
else{
for(int i=1;i<m;i++) b[i]=2;
b[m]=m-2;
}
for(int i=1;i<=n;i++){
for(int j=1;j<m;j++) wsp(a[i]*b[j]);
wln(a[i]*b[m]);
}
return 0;
}
:::
T10 [NOIP2022] 喵了个喵
sb题
k=2n-2
这种情况非常简单,容易发现每个栈放两个数最后会空出一个栈(这个栈永远不能放数,称他为特殊栈),对于新来的数,如果它不在栈中,找一个数字数量小于 2 的栈放入,如果它在栈中,若在栈顶,直接放入栈即可,如果在栈底,放到空栈中,然后消栈底即可
k=2n-1
考虑前
我们可以找到下一个在栈底或为
此时可以将
记
a.在
b.在
实现
这题最傻逼的地方在这里
如果只需要口胡,这题远远算不上历年最难
对每个栈,我们可以使用双端队列
对于还用空位的栈,用队列
对于普通的插入(不是第
:::info
bool simple(int x){//id为每个数字在的栈
if(!id[x]){
if(q.empty()) return 0;
int a=q.front();q.pop();
id[x]=a;stk[a].push_back(x);
ans.pb(mk(a,0));
}
else{
int s=id[x];
if(stk[s].back()==x){
ans.pb(mk(s,0));
stk[s].pop_back();
id[x]=0;
q.push(s);
}
else{
ans.pb(mk(spe,0));
ans.pb(mk(spe,s));
stk[s].pop_back();
id[x]=0;
q.push(s);
}
}
return 1;
}
:::
对于不平凡的插入,分讨即可,具体见代码
:::success[code]
#include<bits/stdc++.h>
using namespace std;
#define PII pair<int,int>
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define ivoid inline void
const int maxn=2e6+10;
int T,n,m,k,a[maxn],id[maxn];
int spe;
queue<int> q;
vector<PII> ans;
deque<int> stk[1010];
bool simple(int x){
if(!id[x]){
if(q.empty()) return 0;
int a=q.front();q.pop();
id[x]=a;stk[a].push_back(x);
ans.pb(mk(a,0));
}
else{
int s=id[x];
if(stk[s].back()==x){
ans.pb(mk(s,0));
stk[s].pop_back();
id[x]=0;
q.push(s);
}
else{
ans.pb(mk(spe,0));
ans.pb(mk(spe,s));
stk[s].pop_back();
id[x]=0;
q.push(s);
}
}
return 1;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>T;
ans.reserve(4e6+10);
while(T--){
cin>>n>>m>>k;ans.clear();
while(!q.empty()) q.pop();
for(int i=1;i<=n;i++) stk[i].clear();
for(int i=1;i<=m;i++) id[i]=0;
for(int i=1;i<=m;i++) cin>>a[i];
for(int i=1;i<=n-1;i++){
q.push(i);
q.push(i);
}spe=n;
for(int i=1;i<=m;i++) if(!simple(a[i])){
int r=i+1;
while(r<=m){
if(a[r]!=a[i]&&(id[a[r]]==0||stk[id[a[r]]].back()==a[r])){
r++;
}
else break;
}
if(r==m+1){
ans.pb(mk(spe,0));
for(int j=i+1;j<=m;j++) assert(simple(a[j]));
break;
}
if(a[i]==a[r]){
ans.pb(mk(spe,0));
for(int j=i+1;j<r;j++) assert(simple(a[j]));
ans.pb(mk(spe,0));
}
else{
int cnt=0;
int s=id[a[r]];
int u=a[r],v=stk[s].back();
assert(u!=v);
for(int j=i+1;j<r;j++){
if(a[j]==v) cnt^=1;
}
if(cnt==0){
ans.pb(mk(s,0));
for(int j=i+1;j<r;j++){
if(a[j]==v) ans.pb(mk(spe,0));
else assert(simple(a[j]));
}
ans.pb(mk(spe,0));
ans.pb(mk(spe,s));
id[u]=0;
id[a[i]]=s;
stk[s].pop_front();
stk[s].push_back(a[i]);
}
else{
ans.pb(mk(spe,0));
for(int j=i+1;j<r;j++){
if(a[j]==v) ans.pb(mk(s,0));
else assert(simple(a[j]));
}
ans.pb(mk(s,0));
q.push(spe);
id[a[i]]=spe;
stk[spe].push_back(a[i]);
spe=s;
stk[s].clear();
id[u]=id[v]=0;
}
}
i=r;
}
cout<<ans.size()<<"\n";
for(auto u:ans){
if(u.se==0) cout<<"1 "<<u.fi<<"\n";
else cout<<"2 "<<u.fi<<" "<<u.se<<"\n";
}
}
return 0;
}
:::
区区 122 行