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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

*【2019牛客暑期多校训练营(第三场)- G】Removing Stones(分治)

發(fā)布時(shí)間:2023/12/10 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 *【2019牛客暑期多校训练营(第三场)- G】Removing Stones(分治) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題干:

鏈接:https://ac.nowcoder.com/acm/contest/883/G
來(lái)源:牛客網(wǎng)
?

Summer vacation is coming and Mark has returned home from his university having successfully survived the exam week. Today, he is very bored. So his friend Alice challenges him to play a game with stones which is invented by her. Alice gives Mark?N\ N?N piles of stones numbered from?1\ 1?1 to?N\ N?N, and there are aia_iai? stones in the?i\ i?i-th pile. The rules of the game are simple: Mark will try to remove all stones. In each move, Mark chooses two different non-empty piles and removes one stone from each of those two piles. Mark can perform any number of moves. If all the piles are empty after some number of moves, Mark wins the game. If he can't make a valid move but not all piles are empty, he loses the game.?Obviously, if the total number of stones is odd, then Mark is not able to win the game. So there is an additional rule: if initially, the total number of stones is odd, then Mark removes a single stone from the pile with the fewest stones before starting the game. If there are multiple piles with the smallest number of stones, Mark chooses one among them to remove a stone.

?

Mark found the optimal strategy for Alice's game very quickly and gets bored again. Also, he noticed that for some configuration of stones there is no way to win. So he challenges you to solve this problem: count the number of integer pairs (l,r)?(1≤l<r≤N)(l,r) \ (1 \le l < r \le N)(l,r)?(1≤l<r≤N) such that it is possible for Mark to win the game if the game is played using only the piles numbered from?l\ l?l to?r\ r?r.

輸入描述:

The input contains multiple cases. The first line of the input contains a single positive integer?T\ T?T, the number of cases. The first line of each case contains a single integer N?(2≤N≤300000)N \ (2 \le N \le 300000)N?(2≤N≤300000), the number of piles. The following line contains?N\ N?N space-separated integers, where the?i\ i?i-th integer denotes ai?(1≤ai≤109)a_i \ (1 \le a_i \le 10^9)ai??(1≤ai?≤109), the number of stones in the?i\ i?i-th pile. It is guaranteed that the sum of?N\ N?N over all cases does not exceed?1000000\ 1000000?1000000.

輸出描述:

For each case, print a single integer on a single line, the number of pairs satisfying the required property.

示例1

輸入

復(fù)制

2 3 1 1 1 4 1 2 3 4

輸出

復(fù)制

3 3

題目大意:

給你N堆石子,每堆石子中有a[i]個(gè)石子,問(wèn)多少個(gè)石子區(qū)間,可以滿足:

①石子總和若為偶數(shù),那么可以兩兩取 來(lái)自不同堆的石子各一個(gè),重復(fù)操作,直到取完。

②如果為奇數(shù),那么在最多石子的一堆中去掉其中一個(gè),使得石子總和變?yōu)榕紨?shù),然后進(jìn)入操作①。

解題報(bào)告:

對(duì)于一個(gè)區(qū)間來(lái)說(shuō),如果最大值大于除它之外其他數(shù)的和,就肯定不能清空了,反之,通過(guò)歸納證明,一定可以清空。(這一個(gè)證明好像是前幾天codeforce的一個(gè)div2B原題?)

因?yàn)檫@題轉(zhuǎn)化后的題意是這樣:求有多少個(gè)長(zhǎng)度不小于2的連續(xù)子序列,使得其中最大元素不大于序列和的1/2。

再轉(zhuǎn)化一下題意:求:區(qū)間最大值的兩倍 小于等于 區(qū)間和的區(qū)間個(gè)數(shù)。

子序列的統(tǒng)計(jì)的題,可以考慮分治解決。

首先找到最大值的位置pos,這樣的話[L,R]= [L,pos-1] + [pos+1,R] + 跨過(guò)pos這個(gè)位置的區(qū)間個(gè)數(shù)。不難發(fā)現(xiàn)這樣分治是正確的。(和第一場(chǎng)那個(gè)分治做法差不多啊)

但是注意下一步統(tǒng)計(jì)跨過(guò)pos這個(gè)位置的答案的時(shí)候,要枚舉那個(gè)小區(qū)間,好像可以證明均攤復(fù)雜度是nlogn的。(我也不會(huì)證)

然后比較正確的做法就是枚舉這個(gè)小區(qū)間的每一個(gè)位置,然后對(duì)前綴和二分(因?yàn)榍熬Y和是具有單調(diào)性的,而值沒(méi)有),二分右區(qū)間的位置。如果尺取的話復(fù)雜度是不一定能保證的,詳見(jiàn)下面的代碼。

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #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 = 3e5 + 5; int lg[MAX],f[MAX][33]; int a[MAX],n,Log[MAX]; ll sum[MAX],ans; inline void ST() {for(int i = 1; i<=n; i++) f[i][0] = i;for(int x,y,j = 1; (1<<j)<=n; j++) {for(int i = 1; i+(1<<j)-1<=n; i++) {x = f[i][j-1],y = f[i+(1<<j-1)][j-1];if(a[x] > a[y]) f[i][j] = x;else f[i][j] = y;}} } inline int gMax(int l,int r) {int k = Log[r-l+1];int x = f[l][k],y = f[r-(1<<k)+1][k];if(a[x] < a[y]) return y;else return x; } void solve(int L,int R) {if(L >= R) return ;int pos = gMax(L,R);solve(L,pos-1);solve(pos+1,R);if(pos - L + 1< R - pos + 1) {//如果左邊的數(shù)字少的話 int r = pos;for(int l = L; l<=pos; l++) {while(r <= R && sum[r] - sum[l-1] < 2*a[pos]) r++;ans += R-r+1;}// for(int l = pos; l>=L; l--) { // while(r >= pos && sum[r] - sum[l-1] >= 2*a[pos]) r--; // ans += R-r; // }}else {int l = pos;for(int r = R; r>=pos; r--) {while(l>=L && sum[r] - sum[l-1] < 2*a[pos]) l--;ans += l-L+1;}// for(int r = pos; r<=R; r++) { // while(l <= pos && sum[r] - sum[l-1] >= 2*a[pos]) l++; // ans += l-L; // }} } int main() {for(int i = 2; i<MAX; i++) Log[i] = Log[i>>1]+1;int t;cin>>t;while(t--) {scanf("%d",&n);for(int i = 1; i<=n; i++) scanf("%d",a+i),sum[i] = sum[i-1] + a[i];ST();ans = 0;solve(1,n);printf("%lld\n",ans);}return 0 ; }

TLE的代碼:

分析一下原因,其實(shí)思路都是一樣的,只是實(shí)現(xiàn)的時(shí)候他相當(dāng)于是 先移動(dòng)左指針,然后看右指針最多移動(dòng)到那里。我是按照一般的尺取,先移動(dòng)右指針。這樣就有個(gè)問(wèn)題當(dāng)他是遞減區(qū)間的時(shí)候,我這樣相當(dāng)于還是n^2的復(fù)雜度,但是他那樣的話雖然看起來(lái)也是n^2的復(fù)雜度,但是至少對(duì)于單減的序列或者單增序列復(fù)雜度都沒(méi)問(wèn)題。(不過(guò)保險(xiǎn)起見(jiàn)這題還是寫(xiě)二分比較好吧..最差復(fù)雜度保證了O(nlog^2 n))

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #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 = 3e5 + 5; int lg[MAX],f[MAX][33]; int a[MAX],n,Log[MAX]; ll sum[MAX],ans; inline void ST() {for(int i = 1; i<=n; i++) f[i][0] = i;for(int x,y,j = 1; (1<<j)<=n; j++) {for(int i = 1; i+(1<<j)-1<=n; i++) {x = f[i][j-1],y = f[i+(1<<j-1)][j-1];if(a[x] > a[y]) f[i][j] = x;else f[i][j] = y;}} } inline int gMax(int l,int r) {int k = Log[r-l+1];int x = f[l][k],y = f[r-(1<<k)+1][k];if(a[x] < a[y]) return y;else return x; } void solve(int L,int R) {if(L >= R) return ;int pos = gMax(L,R);solve(L,pos-1);solve(pos+1,R);if(pos - L + 1< R - pos + 1) {//如果左邊的數(shù)字少的話 int r = R;for(int l = pos; l>=L; l--) {while(r >= pos && sum[r] - sum[l-1] >= 2*a[pos]) r--;ans += R-r;}}else {int l = L;for(int r = pos; r<=R; r++) {while(l <= pos && sum[r] - sum[l-1] >= 2*a[pos]) l++;ans += l-L;}} } int main() {for(int i = 2; i<MAX; i++) Log[i] = Log[i>>1]+1;int t;cin>>t;while(t--) {scanf("%d",&n);for(int i = 1; i<=n; i++) scanf("%d",a+i),sum[i] = sum[i-1] + a[i];ST();ans = 0;solve(1,n);printf("%lld\n",ans);}return 0 ; }

咖啡雞的On代碼:(但是并不能看懂 先放著吧)

對(duì)于每個(gè)數(shù)都看把這個(gè)數(shù)當(dāng)成最大值得左邊界和右邊界的和,然后求是否這個(gè)區(qū)間成立。
但是這里不好統(tǒng)計(jì)有多少成立的區(qū)間,于是便反向取,即求有多少不滿足的區(qū)間。

#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=300005; ll a[maxn],l[maxn],r[maxn]; int main() {int t;int n,m;cin>>t;while(t--){cin>>n;ll ans=1LL*n*(n+1)/2;for(int i=1;i<=n;i++) cin>>a[i];for(int i=1;i<=n;i++){int x=0,y=0;while(i-x-1>=1&&a[i-x-1]+l[x]<a[i]){l[x+1]=l[x]+a[i-x-1];x++;}while(i+y+1<=n&&a[i+y+1]+r[y]<a[i]){r[y+1]=r[y]+a[i+y+1];y++;}for(int j=0;j<=x;j++){while(l[j]+r[y]>=a[i]) y--;ans-=y+1;}}cout<<ans<<endl;}return 0; }

?

總結(jié)

以上是生活随笔為你收集整理的*【2019牛客暑期多校训练营(第三场)- G】Removing Stones(分治)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 香蕉视频在线播放 | 久久黄色片视频 | 999视频在线播放 | 欧美,日韩,国产精品免费观看 | 日本精品视频一区 | 91天堂在线视频 | 天堂视频中文在线 | 国产伦精品视频一区二区三区 | 午夜成人亚洲理伦片在线观看 | 一级片在线视频 | 国产精品99无码一区二区 | 99riav1国产精品视频 | 色站av| 日日操天天操 | 激情网久久 | 国模无码视频一区 | 人人插人人干 | 久热精品视频在线播放 | 激情在线观看视频 | 欧美一二三区在线观看 | 国产ts人妖系列高潮 | 成人av网站免费 | 日韩精彩视频在线观看 | 精品一区二区久久久久蜜桃 | av手机天堂 | 日韩一级在线视频 | 国产精品自产拍高潮在线观看 | 二区三区 | 精品一区二区三区精华液 | 欧美韩国日本一区 | 欧美日韩视频免费 | 美国美女群体交乱 | 亚洲经典久久 | 一级免费在线观看 | 日韩欧美福利视频 | 久久精品aaaaaa毛片 | 日韩精品电影在线观看 | 天堂av一区二区 | 天天操夜夜摸 | 全部免费毛片在线播放高潮 | 992在线观看 | 神马午夜激情 | 黄色一级生活片 | 亚洲国产成人va在线观看天堂 | 激情午夜av | 日本韩国中文字幕 | 亚洲最新在线视频 | 国产精品入口66mio男同 | 三上悠亚影音先锋 | 欧美一级黄色大片 | 日本一本在线观看 | 日本www视频在线观看 | 亚洲天堂无吗 | 免费簧片在线观看 | 亚洲最大福利网站 | 国产成人精品国内自产拍免费看 | 就去色av| 污网站在线观看免费 | av小说免费在线观看 | 中文字幕在线观看网站 | 久久久久一区二区精码av少妇 | 久久久久久国产精品无码 | 久久福利网站 | 天天视频入口 | 男人的天堂一区 | 久久精品国产一区二区三区 | 黄网在线观看视频 | 99r热| 久91| 丰满岳乱妇一区二区 | 国产网站黄色 | 亚洲AV无码成人精品区先锋 | 拔擦8x成人一区二区三区 | 国产色91| 18岁禁黄网站 | 国产ts人妖调教重口男 | 欧美日韩视频无码一区二区三 | 91插插影库 | av免费观看大全 | 日韩网站视频 | av福利片 | 欧美日韩综合在线观看 | 男人的天堂avav | 色婷婷综合久久久久中文 | 国产青草视频 | 国产一级片一区二区 | 国产精品色网 | 久国产精品 | 成片免费观看视频大全 | 性做爰裸体按摩视频 | 曰韩一级片 | 中文字幕第7页 | 天天在线免费视频 | 日本久色 | 久久久久久久久久久福利 | 九七久久| 长腿校花无力呻吟娇喘的视频 | 91美女福利视频 | 日本激情影院 |