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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Codeforces Round #585 (Div. 2) E. Marbles 状压dp + 逆序对

發(fā)布時(shí)間:2023/12/4 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Codeforces Round #585 (Div. 2) E. Marbles 状压dp + 逆序对 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

傳送門

文章目錄

  • 題意:
  • 思路:

題意:

思路:

考慮數(shù)列最終的狀態(tài)一定是相同顏色在一起,所以我們發(fā)現(xiàn)他的顏色是有順序的!顯然可以用狀壓dpdpdp來枚舉顏色的順序,但是又有問題了,你怎么確定當(dāng)前這個(gè)顏色加入這個(gè)集合的時(shí)候貢獻(xiàn)是多少呢?

更深刻的考慮一下,我們使用狀壓dpdpdp給顏色定序,實(shí)際上也就是將顏色大小重新定義了一下,最終排在最前面的是111,以此類推,而變換到答案序列的時(shí)候就是將其轉(zhuǎn)變?yōu)檫f增序列。這樣想有什么好處呢?我們知道將一個(gè)數(shù)列變?yōu)檫f增的,如果只能交換相鄰的元素的話,冒泡排序是最優(yōu)的,所需要的操作次數(shù)也就是逆序?qū)€(gè)數(shù)。

可以發(fā)現(xiàn),這樣的話就比較容易了。設(shè)f[S]f[S]f[S]為當(dāng)前集合為SSS的時(shí)候最少逆序?qū)€(gè)數(shù),假設(shè)當(dāng)前枚舉的集合為SSS,一個(gè)不在集合中的顏色為xxx,加上xxx之后的集合為GGG,那么我們?nèi)绻杜eSSS集合中某個(gè)顏色yyy,讓后只需要計(jì)算一下有多少對(duì)l<r,al=x,ar=yl<r,a_l=x,a_r=yl<r,al?=x,ar?=y,加入答案即可,也就是f[G]=f[S]+∑y∈Scost[y][x]f[G]=f[S]+\sum_{y\in S} cost[y][x]f[G]=f[S]+yS?cost[y][x]

現(xiàn)在問題就變成了我們?nèi)绾翁幚?span id="ozvdkddzhkzd" class="katex--inline">cost[y][x]cost[y][x]cost[y][x]數(shù)組,一種顯然的方式就是直接20?2020*2020?20枚舉y,xy,xy,x,讓后O(n)O(n)O(n)掃一遍統(tǒng)計(jì)逆序?qū)纯?#xff0c;這個(gè)復(fù)雜度為O(n?400)O(n*400)O(n?400)。還有一種比較快的方式就是從頭掃,記cnt[i]cnt[i]cnt[i]為到當(dāng)前點(diǎn)顏色iii出現(xiàn)的次數(shù),讓后再枚舉其他顏色,加上答案即可,復(fù)雜度O(n?20)O(n*20)O(n?20)
復(fù)雜度O(220?400+n?400)O(2^{20}*400+n*400)O(220?400+n?400)

// Problem: E. Marbles // Contest: Codeforces - Codeforces Round #585 (Div. 2) // URL: https://codeforces.com/contest/1215/problem/E // Memory Limit: 256 MB // Time Limit: 4000 ms // // Powered by CP Editor (https://cpeditor.org)//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native") //#pragma GCC optimize(2) #include<cstdio> #include<iostream> #include<string> #include<cstring> #include<map> #include<cmath> #include<cctype> #include<vector> #include<set> #include<queue> #include<algorithm> #include<sstream> #include<ctime> #include<cstdlib> #include<random> #include<cassert> #define X first #define Y second #define L (u<<1) #define R (u<<1|1) #define pb push_back #define mk make_pair #define Mid ((tr[u].l+tr[u].r)>>1) #define Len(u) (tr[u].r-tr[u].l+1) #define random(a,b) ((a)+rand()%((b)-(a)+1)) #define db puts("---") using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); } //void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); } //void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> PII;const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f; const double eps=1e-6;int n; int a[N]; LL f[4*N],cs[30][30];int main() { // ios::sync_with_stdio(false); // cin.tie(0);scanf("%d",&n);for(int i=1;i<=n;i++) scanf("%d",&a[i]),a[i]--; for(int i=0;i<20;i++) {for(int j=0;j<20;j++) {if(i==j) continue;LL ans=0,cnt=0;for(int k=1;k<=n;k++) {if(a[k]==i) cnt++;else if(a[k]==j) ans+=cnt;}cs[i][j]=ans;}}memset(f,0x3f3f3f3f,sizeof(f));f[0]=0;for(int i=0;i<1<<20;i++) {for(int j=0;j<20;j++) if(!(i>>j&1)) {LL sum=0;for(int k=0;k<20;k++) if(i>>k&1) {sum+=cs[k][j];}f[i|(1<<j)]=min(f[i|(1<<j)],f[i]+sum);}}cout<<f[(1<<20)-1]<<endl;return 0; } /**/

總結(jié)

以上是生活随笔為你收集整理的Codeforces Round #585 (Div. 2) E. Marbles 状压dp + 逆序对的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

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