2020牛客暑期多校訓練營(第六場)E.Easy Construction(思維構造,附圖解)
阿新 • • 發佈:2020-07-29
地址:https://ac.nowcoder.com/acm/contest/5671/E
題意:
構造一個只含1~n的序列,使得對於每一個長度在[1,n]的i,序列都存在一段長度為i的連續子序列,它的和%n==k
解析:
正著看不好看,來倒著分析
首先是i==n的情況,也就是序列總和,如果它%n!=k,那肯定是不行的。
接下來就是一個猜測的過程了:
i固定時,儘量把sum弄大,防止小的sum湊不出來,這個也是根據畫了好幾種得出的,所以一定要勤動手啊。
所以就盲猜了一下構造方式,1,n-1,2,n-2.....直到把所有數填上,但是n奇偶不同,構造的過程在末尾有些不同。所以分開討論即可。
而且根據圖,n這個數字是一定要在兩端的。
#include<bits/stdc++.h> #include<iostream> #include<cstring> #include<string.h> #include<cmath> #include<map> using namespace std; typedef long long ll; const int maxn=5e5+10; int a[maxn]; int vis[5005]; string s; int main() { // memset(vis,0,sizeof(vis)); int n,k; cin>>n>>k; if(n==1) { cout<<"1"<<endl;return 0; } ll sum=(ll)(n*n+n)/2; if(sum%n!=k) { cout<<"-1"<<endl;return 0; } if(n%2==0) { for(int i=1;i<n/2;i++) cout<<i<<" "<<n-i<<" "; cout<<n/2<<" "<<n<<endl; } else { for(int i=1;i<=n/2;i++) cout<<i<<" "<<n-i<<" "; cout<<n<<endl; } }