日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【2018ACM山东省赛 - E】Sequence(树状数组,思维,优化)

發布時間:2023/12/10 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【2018ACM山东省赛 - E】Sequence(树状数组,思维,优化) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

We define an element aia_iai? in a sequence "good", if and only if there exists a j(1≤j<i)j(1\le j < i)j(1≤j<i) such that aj<aia_j < a_iaj?<ai?.
Given a permutation ppp of integers from 111 to nnn. Remove an element from the permutation such that the number of "good" elements is maximized.

Input

The input consists of several test cases. The first line of the input gives the number of test cases, T(1≤T≤103)T(1\le T\le 10^3)T(1≤T≤103).
For each test case, the first line contains an integer n(1≤n≤106)n(1\le n\le 10^6)n(1≤n≤106), representing the length of the given permutation.
The second line contains nnn integers p1,p2,?,pn(1≤pi≤n)p_1,p_2,\cdots,p_n(1\le p_i\le n)p1?,p2?,?,pn?(1≤pi?≤n), representing ?the given permutation ppp.
It’s guaranteed that ∑n≤2×107\sum n\le 2\times 10^7∑n≤2×107.

Output

For each test case, output one integer in a single line, representing the element that should be deleted. If there are several answers, output the minimal one.
?

Sample Input

2 1 1 5 5 1 2 3 4

Sample Output

1 5

題目大意:

定義a[i]是good,當且僅當數組中他前面的數中 存在比他小的數。給定一個n個數的序列a,保證是1~n的一個排列,現在讓你刪掉其中一個數(且必須刪掉一個),使得good數最多。問你刪掉哪個數?如果有多種可能性就輸出最小的解。

解題報告:

? ?這題在重現賽nlogn是可以過的,但是現場賽應該是嚴格保證線性才能過(因為nlogn那就是個水題了啊怎么可能過的隊伍這么少)。保證線性也不難,因為你會發現你只需要記錄前綴最小值和次小值然后維護一下就行了。

AC代碼:

#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<vector> #include<cctype> using namespace std; typedef long long ll; const int maxn=2e7+5; int n,m; int val[maxn],suf[maxn]; //suf[val[i]]記錄依附于val[i]的數的個數 //cnt[i]求每個數前小于它的數的個數, minn[]同時記錄前綴1~i的最小值 int minn[maxn],cnt[maxn]; bool is[maxn];//is[i]記錄val[i]是否不是前綴1~i的最小值 inline int lowbit(int t){return t&-t;} void add(int p,int val,int op) {if(op==0){for(;p<=n;p+=lowbit(p))cnt[p]+=val;}else {for(;p<=n;p+=lowbit(p))minn[p]=min(minn[p],val);} } int query(int p,int op) {int res;if(op){for(res=1e9;p;p-=lowbit(p))res=min(minn[p],res);}else {for(res=0;p;p-=lowbit(p))res+=cnt[p];}return res; } int main() {int t,q,i,j,k;int ans,maxx,tmp,num,sum,Min;cin>>t;for(;t;t--){sum=0; maxx=-1; ans=1e9;scanf("%d",&n);for(i=1;i<=n;i++){scanf("%d",val+i);cnt[i]=suf[i]=is[i]=0; minn[i]=1e9;}for(i=1;i<=n;i++){num=query(val[i]-1,0);if(num){sum++; is[i]=1;if(num==1){tmp=query(val[i]-1,1);suf[tmp]++;}}add(val[i],1,0); add(val[i],val[i],1);}for(i=1;i<=n;i++){tmp=sum-(int)is[i]-suf[val[i]];if(tmp>maxx||tmp==maxx&&val[i]<ans) {maxx=tmp; ans=val[i];}}printf("%d\n",ans);}return 0; } /* 7 1 1 5 5 1 2 3 4 5 4 3 5 2 1 5 1 3 2 4 5 1 5 1 2 */

線性AC代碼:

#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<vector> #include<cctype> using namespace std; typedef long long ll; const int maxn=1e6+5; int n,m1,m2; int val[maxn],suf[maxn]; //suf[i]記錄依附于val[i]的數的個數 //cnt[i]求每個數前小于它的數的個數, minn[]同時記錄前綴1~i的最小值 bool is[maxn];//is[i]記錄val[i]是否不是前綴1~i的最小值 int main() {int t,q,i,j,k;int ans,maxx,tmp,num,sum;cin>>t;for(;t;t--){sum=0; maxx=-1; ans=m1=m2=1e9;scanf("%d",&n);for(i=1;i<=n;i++){scanf("%d",val+i);suf[i]=is[i]=0; }for(i=1;i<=n;i++){if(m1<val[i]){sum++; is[i]=1;if(m2>val[i]) {suf[m1]++; m2=val[i];}}else {m2=m1; m1=val[i];}}for(i=1;i<=n;i++){tmp=sum-(int)is[i]-suf[val[i]];if(tmp>maxx||tmp==maxx&&val[i]<ans) {maxx=tmp; ans=val[i];}}printf("%d\n",ans);}return 0; }

?

總結

以上是生活随笔為你收集整理的【2018ACM山东省赛 - E】Sequence(树状数组,思维,优化)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。