【2019icpc南京站网络赛 - F】Greedy Sequence(思维,贪心构造,STLset)
題干:
You're given a permutation?aa?of length?nn?(1 \le n \le 10^51≤n≤105).
For each?i \in [1,n]i∈[1,n], construct a sequence?s_isi??by the following rules:
Consider two sequences?C = [c_1, c_2, ... c_n]C=[c1?,c2?,...cn?]?and?D=[d_1, d_2, ..., d_n]D=[d1?,d2?,...,dn?], we say the weight of?CC?is?higher thanthat of?DD?if and only if there exists an integer?kk?such that?1 \le k \le n1≤k≤n,?c_i=d_ici?=di??for all?1 \le i < k1≤i<k, and?c_k > d_kck?>dk?.
If for each?i \in [1,n]i∈[1,n],?c_i=d_ici?=di?, the weight of?CC?is equal to the weight of?DD.
For each?i \in [1,n]i∈[1,n], print the number of non-zero elements of?s_isi??separated by a space.
It's guaranteed that there is only one possible answer.
Input
There are multiple test cases.
The first line contains one integer?T(1 \le T \le 20)T(1≤T≤20), denoting the number of test cases.
Each test case contains two lines, the first line contains two integers?nn?and?kk?(1 \le n,k \le 10^51≤n,k≤105), the second line contains?nn?distinct integers?a_1, a_2, ..., a_na1?,a2?,...,an??(1 \le a_i \le n1≤ai?≤n) separated by a space, which is the permutation?aa.
Output
For each test case, print one line consists of?nn?integers?|s_1|, |s_2|, ..., |s_n|∣s1?∣,∣s2?∣,...,∣sn?∣?separated by a space.
|s_i|∣si?∣?is the number of non-zero elements of sequence?s_isi?.
There is no space at the end of the line.
樣例輸入復制
2 3 1 3 2 1 7 2 3 1 4 6 2 5 7樣例輸出復制
1 2 3 1 1 2 3 2 3 3題目大意:
給你一個1~n的排列
解題報告:
首先貪心的思想,每個元素的下一個元素一定是與他距離不超過 k 的所有元素中,權值最大的元素,所以每個元素的下一個元素是固定的,我們可以通過滑動窗口 + set 上二分的預處理計算出每個元素的下一個元素,之后通過一個記憶化搜索即可 O(n) 求出每一個貪心序列的長度。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; int a[MAX]; int n,q; set<int> ss; int db[MAX];//cun index int ans[MAX]; int k; int main() {int T;cin>>T;while(T--) {scanf("%d%d",&n,&k);ss.clear();for(int i = 1; i<=n; i++) scanf("%d",a+i);int l=1-k,r=1+k;for(int i = 1; i<=r; i++) ss.insert(a[i]);for(int i = 1; i<=n; i++) { l = i-k,r = i+k;auto it = ss.lower_bound(a[i]);if(it != ss.begin()) {it--;db[a[i]] = (*it);}else db[a[i]] = 0; if(l >= 1) {auto it = ss.lower_bound(a[l]);ss.erase(it);}if(r+1<=n) {ss.insert(a[r+1]);}}for(int i = 1; i<=n; i++) ans[i] = ans[db[i]]+1;for(int i = 1; i<=n; i++) {printf("%d%c",ans[i],i == n ? '\n' :' ');}}return 0 ; }?
總結
以上是生活随笔為你收集整理的【2019icpc南京站网络赛 - F】Greedy Sequence(思维,贪心构造,STLset)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 消息称华为nova 10 Pro标配10
- 下一篇: 挑战比亚迪DM-i!长安UNI-V iD