CodeForces - 1561E Bottom-Tier Reversals(构造)
生活随笔
收集整理的這篇文章主要介紹了
CodeForces - 1561E Bottom-Tier Reversals(构造)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:給出一個長度為 nnn 的排列,每次操作可以選擇一個奇數長度的前綴然后反轉,需要構造一種方案,使得在不超過 5n2\frac{5n}{2}25n? 次操作后使得序列有序
題目分析:挺有意思的一道構造題,手玩幾組樣例后不難發現每個位置在反轉后,下標的奇偶性是不發生改變的,所以可以大膽猜測如果奇偶性滿足條件的情況下,是一定可以構造出答案的。
因為每次反轉的是前綴,反過來想后綴是不會受到影響的,所以不妨倒著構造,先將 nnn 的位置放好,再放 n?1n-1n?1 如此。
又因為每次操作只能反轉長度為奇數的前綴,又因為操作數是 5n2\frac{5n}{2}25n?,不難猜想,對于一對數 {i,i?1}\{i,i-1\}{i,i?1},我們應該在 555 次操作將其回到相應的位置。
有了上面的鋪墊亂搞搞應該就能搞出正解啦,設 a[x]=ia[x]=ia[x]=i,a[y]=i?1a[y]=i-1a[y]=i?1,則五次操作分別對應著:{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; }總結
以上是生活随笔為你收集整理的CodeForces - 1561E Bottom-Tier Reversals(构造)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 洛谷 - P3899 [湖南集训]谈笑风
- 下一篇: HDU - 5381 The sum o