1 solutions
-
0
那头与艺术
首先特判掉 中所有元素相同的情况。
容易发现答案有一个下界: 中众数出现的次数加一。
具体的,记众数为 ,任意一个其他的数为 。那么 形如 …A…A..B..A…A…A…。显然可以通过循环移位使得 中只包含 和 的子序列相等。
尝试构造取到这个下界。一种构造方案是 为 升序排列后的结果, 通过不断降序取 中剩余元素各一个得到。
例如:,则构造 ,。容易发现上述构造符合要求。
#include<bits/stdc++.h> using namespace std; const int N=505; int n,a[N]; void solve(int tid){ cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; sort(a+1,a+n+1); int maxn = 1, lst = 0; for (int i = 1; i <= n; ++ i) if (a[i] != a[i - 1]) lst = i - 1;else maxn = max(maxn, i - lst); cout << maxn + 1 << endl; for(int i=1;i<=n;i++) cout<<a[i]<<" "; cout<<"\n"; set<int> st; map<int,int> cnt; for(int i=1;i<=n;i++) st.insert(a[i]),cnt[a[i]]++; while(1){ vector<int> tmp; int flag=1; for(int x:st){ if(cnt[x]) tmp.push_back(x),cnt[x]--,flag=0; } if(flag) break; reverse(tmp.begin(),tmp.end()); for(int x:tmp) cout<<x<<" "; } } int main(){ freopen("art.in", "r", stdin); freopen("art.out", "w", stdout); int T = 1; for(int i=1;i<=T;i++) solve(i); }
- 1
Information
- ID
- 66
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 8
- Tags
- (None)
- # Submissions
- 23
- Accepted
- 6
- Uploaded By