CodeForces - 1561E Bottom-Tier Reversals(构造)
題目鏈接:點(diǎn)擊查看
題目大意:給出一個(gè)長(zhǎng)度為 nnn 的排列,每次操作可以選擇一個(gè)奇數(shù)長(zhǎng)度的前綴然后反轉(zhuǎn),需要構(gòu)造一種方案,使得在不超過(guò) 5n2\frac{5n}{2}25n? 次操作后使得序列有序
題目分析:挺有意思的一道構(gòu)造題,手玩幾組樣例后不難發(fā)現(xiàn)每個(gè)位置在反轉(zhuǎn)后,下標(biāo)的奇偶性是不發(fā)生改變的,所以可以大膽猜測(cè)如果奇偶性滿足條件的情況下,是一定可以構(gòu)造出答案的。
因?yàn)槊看畏崔D(zhuǎn)的是前綴,反過(guò)來(lái)想后綴是不會(huì)受到影響的,所以不妨倒著構(gòu)造,先將 nnn 的位置放好,再放 n?1n-1n?1 如此。
又因?yàn)槊看尾僮髦荒芊崔D(zhuǎn)長(zhǎng)度為奇數(shù)的前綴,又因?yàn)椴僮鲾?shù)是 5n2\frac{5n}{2}25n?,不難猜想,對(duì)于一對(duì)數(shù) {i,i?1}\{i,i-1\}{i,i?1},我們應(yīng)該在 555 次操作將其回到相應(yīng)的位置。
有了上面的鋪墊亂搞搞應(yīng)該就能搞出正解啦,設(shè) a[x]=ia[x]=ia[x]=i,a[y]=i?1a[y]=i-1a[y]=i?1,則五次操作分別對(duì)應(yīng)著:{x,y?1,y+1,3,i}\{x,y-1,y+1,3,i\}{x,y?1,y+1,3,i}
代碼:
// Problem: E. Bottom-Tier Reversals // Contest: Codeforces - Codeforces Round #740 (Div. 2, based on VK Cup 2021 - Final (Engine)) // URL: https://codeforces.com/contest/1561/problem/E // Memory Limit: 512 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org)// #pragma GCC optimize(2) // #pragma GCC optimize("Ofast","inline","-ffast-math") // #pragma GCC target("avx,sse2,sse3,sse4,mmx") #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> #include<bitset> #include<list> #include<unordered_map> #define lowbit(x) (x&-x) using namespace std; typedef long long LL; typedef unsigned long long ull; template<typename T> inline void read(T &x) {T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f; } template<typename T> inline void write(T x) {if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0'); } const int inf=0x3f3f3f3f; const int N=1e6+100; int a[N],n; vector<int>ans; int find(int x) {for(int i=1;i<=n;i++) {if(a[i]==x) {return i;}}return -1; } void add(int x) {ans.push_back(x);reverse(a+1,a+1+x); } int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);int w;cin>>w;while(w--) {ans.clear();read(n);bool flag=true;for(int i=1;i<=n;i++) {read(a[i]);if((a[i]-i)&1) {flag=false;}}if(!flag) {puts("-1");continue;}for(int i=n;i>1;i-=2) {int x=find(i);add(x);int y=find(i-1);add(y-1);add(y+1);add(3);add(i);}cout<<ans.size()<<endl;for(auto it:ans) {printf("%d ",it);}cout<<endl;}return 0; }總結(jié)
以上是生活随笔為你收集整理的CodeForces - 1561E Bottom-Tier Reversals(构造)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 洛谷 - P3899 [湖南集训]谈笑风
- 下一篇: HDU - 5381 The sum o