版本比较
【题录】构造杂题
正文
增加 387 行删除 5 行
12345146 行未改动全部展开15215315415515615715815916016116216316416530 行未改动196197198199200201202203204205206## [ABC103B Robot Arms](https://www.luogu.com.cn/problem/AT_arc103_b)### 思路首先考虑无解的情况。观察题目,由于 $x$ 或 $y$ 第 $i$ 次操作会加上或减去 $d_i$,而加上或减去一个数对奇偶性的影响是相同的,又因为 $d$ 数组都是固定的,所以最终 $x+y$ 的奇偶性也是固定的。146 行未改动全部展开int n,m,x[1010],y[1010];long long b[45];int main(){freopen("code.in","r",stdin);//样例输入scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d%d",&x[i],&y[i]);freopen("code.out","r",stdin);//你的输出scanf("%d",&m);for(int i=1;i<=m;i++)scanf("%lld",&b[i]);bool tf=true;30 行未改动点有点权,可为 $0$ 或 $1$。求一个赋权方案,使其满足:1. 权值为 $0$ 的点数与权值为 $1$ 的点数不相等。2. 对于任意一个点,要求与其相邻且权值相同的点的个数不超过 $\left\lceil\sqrt{n}\right\rceil$。$n\le22$。---### 思路12345678910146 行未改动全部展开15715815916016116216316416516616716816917030 行未改动201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588## Robot Arms### 题面[ARC103B Robot Arms](https://www.luogu.com.cn/problem/AT_arc103_b)---### 思路首先考虑无解的情况。观察题目,由于 $x$ 或 $y$ 第 $i$ 次操作会加上或减去 $d_i$,而加上或减去一个数对奇偶性的影响是相同的,又因为 $d$ 数组都是固定的,所以最终 $x+y$ 的奇偶性也是固定的。146 行未改动全部展开int n,m,x[1010],y[1010];long long b[45];int main(){freopen("code.in","r",stdin);//程序输入scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d%d",&x[i],&y[i]);freopen("code.out","r",stdin);//程序输出scanf("%d",&m);for(int i=1;i<=m;i++)scanf("%lld",&b[i]);bool tf=true;30 行未改动点有点权,可为 $0$ 或 $1$。求一个赋权方案,使其满足:1. 权值为 $0$ 的点数与权值为 $1$ 的点数不相等。2. 对于任意一个点,要求与其相邻且权值相同的点数不超过 $\left\lceil\sqrt{n}\right\rceil$。>$1\le n\le22$。---### 思路如果没有权值为 $0$ 的点数与权值为 $1$ 的点数不相等的限制,那么显然直接令权值为点编号的 $popcount$ 的奇偶性即可。$\left\lceil\sqrt{n}\right\rceil$ 提示要分块,因此考虑将点编号的二进制位分块。经过拉马努金托梦,得出构造方案:* 一个点的权值是 $1$ 当且仅当至少有一个块全是 $1$。从一个点往相邻点走时:* 一个权值为 $0$ 的点每一块都可能变成全 $1$,且每块至多有一种方案,因此其相邻的权值为 $1$ 的点数不超过块的数量 $\left\lceil\sqrt{n}\right\rceil$。* 一个权值为 $1$ 的点仅在只有一块全是 $1$ 的情况下可能改变权值,且方案数为此块块长,因此其相邻的权值为 $0$ 的点数不超过块长 $\left\lfloor\sqrt{n}\right\rfloor$。发现上面构造出方案仅满足相邻的权值不同点数不超过 $\left\lceil\sqrt{n}\right\rceil$,与原题相反。又因为相邻点编号的 $popcount$ 的奇偶性都不相同,所以将所有点的权值都异或上该点编号的 $popcount$ 的奇偶性即可。时间复杂度为 $O(n2^n)$。---### 代码```cpp#include<bits/stdc++.h>#define lowbit(x) ((x)&(-(x)))using namespace std;int n,b,bl,st[6],ed[6];int popcnt(int s){int cnt=0;while(s>=1){cnt+=1;s-=lowbit(s);}return cnt;}//popcount 函数int main(){scanf("%d",&n);b=ceil(sqrt(n));bl=n/b;if(n%b!=0)bl+=1;for(int i=1;i<=bl;i++){st[i]=(i-1)*b+1;ed[i]=min(i*b,n);}for(int s=0;s<=(1<<n)-1;s++){int ans=0;for(int bi=1;bi<=bl;bi++){bool tf=true;for(int i=st[bi];i<=ed[bi];i++){if(!(s&(1<<(i-1)))){tf=false;break;}//块内有 0 即跳过}if(tf){ans=1;break;}//有至少一块为全 1 即记权值为 1}//计算原权值ans^=popcnt(s)&1;//将原权值异或上点编号的 popcountprintf("%d",ans);}return 0;}```---### checker```cpp#include<bits/stdc++.h>using namespace std;int n,sq,a[4200000];int main(){freopen("code.in","r",stdin);//程序输入scanf("%d",&n);sq=ceil(sqrt(n));freopen("code.out","r",stdin);//程序输出for(int i=0;i<=(1<<n)-1;i++){char c;cin>>c;a[i]=c-'0';}for(int x=0;x<=(1<<n)-1;x++){int s=0;for(int i=1;i<=n;i++){int y=x^(1<<(i-1));if(a[x]==a[y])s+=1;}if(s>sq){printf("WA on vertex %d\n",x);printf("limit: %d\n",sq);printf("yours: %d\n",s);return 0;}}printf("AC");return 0;}```---## Binary Cards### 题面[CF949E Binary Cards](https://www.luogu.com.cn/problem/CF949E)---## Ловушка со свечками### 题面[GYM102420C Ловушка со свечками](https://codeforces.com/gym/102420/problem/C)---## Weighted Mean### 题面[Luogu P10311 「Cfz Round 2」Weighted Mean](https://www.luogu.com.cn/problem/P10311)---## Increasing Income### 题面[QOJ #8837. Increasing Income](https://qoj.ac/problem/8837)---### 思路定义 $S_a$ 为排列 $a$ 的前缀最大值的位置集合,则题目要求最大化 $|S_q|+|S_{p_q}|$,易得:$$|S_q|+|S_{p_q}|=|S_q\cup S_{p_q}|+|S_q\cap S_{p_q}|$$观察右式:* 显然有 $|S_q\cup S_{p_q}|\le n$。* 发现对于集合 $S_q\cap S_{p_q}$,若将其按升序排序,则 $q$ 和 $p_q$ 的值皆按升序排序。说明 $S_q\cap S_{p_q}$ 对应着 $p$ 的一个上升子序列的下标集合,因此得 $|S_q\cap S_{p_q}|\le LIS(p)$。综上所述,得:$$|S_q|+|S_{p_q}|\le n+LIS(p)$$由此得到答案的上界,下面考虑构造一种方案使答案达到上界。求出 $p$ 的一个最长上升子序列的下标序列,令其与 $p$ 的末尾都加入 $n+1$ 作为边界。从小到大依此枚举该下标序列相邻的两个元素,设为 $l,r$。依此进行以下步骤:1. 将 $l$ 加入到 $q$ 的末尾。2. 从小到大遍历 $i\in(l,r)$,若 $p_i<p_r$,则将 $i$ 加入到 $q$ 的末尾。由于 $l<i<r,p_i<p_r$ 且 $i$ 不在最长上升子序列的下标序列中,所以有 $p_i<p_l$。3. 从小到大遍历 $i\in(p_l,p_r)$,若其在 $p$ 中的位置 $rnk_i<r$,则将 $rnk_i$ 加入到 $q$ 的末尾。由于 $p_l<i<p_r,rnk_i<r$ 且 $rnk_i$ 不在最长上升子序列的下标序列中,所以有 $rnk_i<l$。下面证明此构造方案可以使答案达到上界。定义 1 类数为某一轮中被步骤 1 加入 $q$ 的数,2、3 类数同理。首先,易得 1 类数必不与 2、3 类数重复。其次,2 类数与 3 类数也不会重复,证明如下:>考虑反证法,设存在 $i$ 同时为 2、3 类数,则存在 $l_2,r_2,l_3,r_3$ 满足 $l_2<i<r_2,p_i<p_{l_2}$ 且 $p_{l_3}<p_i<p_{r_3},i<l_3$。>>由 $p_i<p_{l_2}$ 和 $p_{l_3}<p_i<p_{r_3}$ 可得 $l_3<r_3\le l_2$,又由 $l_2<i<r_2$ 得 $l_3<l_2<i$,与 $i<l_3$ 矛盾,因此不存在这样的 $i$。于是我们证明了 1、2、3 类数不重,接下来证明 1、2、3 类数包含了 $1$ 到 $n$ 的所有数:>设一个不为 1 类数的数为 $i$,则有 $l_1<i<r_1,p_{l_2}<p_i<p_{r_2}$。由于其不在最长上升子序列的下标序列中,所以 $(l_1,r_1)\ne(l_2,r_2)$,那么 $i$ 必然被加入到其中靠右的那一段中。综上所述,每一个数都一定是 1、2、3 类数中的一个。接着由此证明答案可达上界:* 对于 $q$ 而言,1、2 类数恰是 $q$ 的前缀最大值的位置,而 3 类数由于 $rnk_i<l$ 而不会成为前缀最大值的位置。* 对于 $p_q$ 而言,1、3 类数恰是 $p_q$ 的前缀最大值的位置,而 2 类数由于 $p_i<p_l$ 而不会成为前缀最大值的位置。由上,得答案为两倍的 1 类数个数加上 2、3 类数个数,即为 $n+LIS(p)$。找出最长上升子序列后按上述方案构造即可,复杂度易证为 $O(n\log n)$。---### 代码```cpp#include<bits/stdc++.h>using namespace std;struct data{int v,id;};bool operator < (data x,data y){return x.v<y.v;}int n,a[200010],rnk[200010];data tree[200010],f[200010];vector<int> vc,ans;void clear(){for(int i=1;i<=n;i++)tree[i]=(data){0,0};vc.clear();ans.clear();}inline int lowbit(int x){return x&(-x);}void add(int x,data v){while(x<=n){tree[x]=max(tree[x],v);x+=lowbit(x);}}data ask(int x){data s=(data){0,0};while(x>=1){s=max(s,tree[x]);x-=lowbit(x);}return s;}int main(){int t;scanf("%d",&t);while(t--){clear();scanf("%d",&n);int mx=0;for(int i=1;i<=n;i++){scanf("%d",&a[i]);rnk[a[i]]=i;f[i]=ask(a[i]-1);f[i].v+=1;add(a[i],(data){f[i].v,i});if(f[i].v>f[mx].v)mx=i;}for(int i=mx;i>=1;i=f[i].id)vc.push_back(i);reverse(vc.begin(),vc.end());//找到最长上升子序列的下标序列vc.push_back(n+1);a[n+1]=n+1;//加入 n+1 作为边界for(int i=1;i<vc.size();i++){int l=vc[i-1],r=vc[i];ans.push_back(l);//步骤 1for(int j=l+1;j<=r-1;j++)if(a[j]<a[r])ans.push_back(j);//步骤 2for(int j=a[l]+1;j<=a[r]-1;j++)if(rnk[j]<r)ans.push_back(rnk[j]);//步骤 3}for(int v:ans)printf("%d ",v);printf("\n");}return 0;}```---### checker```cpp#include<bits/stdc++.h>using namespace std;int n,tree[200010],f[200010],ans[200010],b[200010];vector<int> a[200010];void clear(){for(int i=1;i<=n;i++)tree[i]=0;}inline int lowbit(int x){return x&(-x);}void add(int x,int v){while(x<=n){tree[x]=max(tree[x],v);x+=lowbit(x);}}int ask(int x){int s=0;while(x>=1){s=max(s,tree[x]);x-=lowbit(x);}return s;}int main(){int t;freopen("code.in","r",stdin);//程序输入scanf("%d",&t);for(int ti=1;ti<=t;ti++){clear();scanf("%d",&n);a[ti].push_back(0);for(int i=1;i<=n;i++){int v;scanf("%d",&v);a[ti].push_back(v);int s=ask(v-1)+1;add(v,s);ans[ti]=max(ans[ti],s);}ans[ti]+=n;}freopen("code.out","r",stdin);//程序输出bool tf=true;for(int ti=1;ti<=t;ti++){int n=a[ti].size()-1;int s=0;int mx=0;for(int i=1;i<=n;i++){scanf("%d",&b[i]);if(b[i]>mx){mx=b[i];s+=1;}}mx=0;for(int i=1;i<=n;i++){if(a[ti][b[i]]>mx){mx=a[ti][b[i]];s+=1;}}if(s!=ans[ti]){printf("WA on case %d\n",ti);printf("ans: %d\n",ans[ti]);printf("yours: %d\n\n",s);tf=false;}}if(tf)printf("AC");return 0;}```