T183637-变异距离(2021 CoE III C)【单调栈】
正題
題目鏈接:https://www.luogu.com.cn/problem/T183637
題目大意
給出nnn個二元組(xi,yi)(x_i,y_i)(xi?,yi?),求最大的
∣xi?xj∣×min{∣yi∣,∣yj∣}|x_i-x_j|\times min\{|y_i|,|y_j|\}∣xi??xj?∣×min{∣yi?∣,∣yj?∣}
1≤n≤2×106,?106≤xi≤106,?109≤yi≤109,1≤T≤101\leq n\leq 2\times 10^6,-10^6\leq x_i\leq 10^6,-10^9\leq y_i\leq 10^9,1\leq T\leq 101≤n≤2×106,?106≤xi?≤106,?109≤yi?≤109,1≤T≤10
解題思路
昨天出去了所以沒打比賽,這個算法是那個時候口胡的。
首先時間復雜度顯然不能帶logloglog,但是注意到xxx的范圍,這是在告訴我們可以拿xxx去排序。
xxx排好序之后,我們發現對于一個位置jjj,我們尋找一個i<ji<ji<j使得答案最大那么顯然iii要在從前往后的單調隊列里。
這個其實啟示了我們,我們可以前后各維護一個單調棧然后在兩個棧里面搞。
至于搞法不難發現決定因素是最小的那個,所以我們每次把小的那個彈出頂部就好了。
時間復雜度O(n)O(n)O(n)。
然后交上去TTT了好多發,以為是常數的問題,結果換成題解的做法還是TTT了。
最后發現快讀還不夠,要用那個文件的黑科技讀入,出題人真有你的
code
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<cctype> #define ll long long using namespace std; const int N=2e6+10; int T,n,a[N],sp[N],ss[N],tp,ts; long long ans; inline char Getchar() {static char buf[100000],*p1=buf+100000,*pend=buf+100000;if(p1==pend){p1=buf; pend=buf+fread(buf,1,100000,stdin);if (pend==p1) return -1;}return *p1++; } inline int read() {char c;int d=1;int f=0;while(c=Getchar(),!isdigit(c))if(c==45)d=-1;f=(f<<3)+(f<<1)+c-48;while(c=Getchar(),isdigit(c)) f=(f<<3)+(f<<1)+c-48;return d*f; } void GetAns(int i,int j) {ans=max(ans,1ll*(i-j)*min(a[i],a[j]));} signed main() {T=read();while(T--){n=read();ans=0;memset(a,0,sizeof(a));for(int i=1;i<=n;i++){int x=read()+1e6,y=read();a[x]=max(a[x],abs(y));}ts=tp=0;for(int i=0;i<=2e6;i++){if(!a[i])continue;while(ts>0&&a[i]>=a[ss[ts]])ts--;ss[++ts]=i;}for(int i=2e6;i>=0;i--){if(!a[i])continue;while(tp>0&&a[i]>=a[sp[tp]])tp--;sp[++tp]=i;}int hp=1,hs=1;while(ts&&tp){GetAns(ss[ts],sp[tp]);if(ts>0&&a[ss[ts]]<=a[sp[tp]])ts--;else tp--;}printf("%lld\n",ans);}return 0; }總結
以上是生活随笔為你收集整理的T183637-变异距离(2021 CoE III C)【单调栈】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Excel中hlookup函数的使用方法
- 下一篇: P5405-[CTS2019]氪金手游【