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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

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

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

傳送門

文章目錄

  • 題意:
  • 思路:

題意:

思路:

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

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

可以發現,這樣的話就比較容易了。設f[S]f[S]f[S]為當前集合為SSS的時候最少逆序對個數,假設當前枚舉的集合為SSS,一個不在集合中的顏色為xxx,加上xxx之后的集合為GGG,那么我們如果枚舉SSS集合中某個顏色yyy,讓后只需要計算一下有多少對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]

現在問題就變成了我們如何處理cost[y][x]cost[y][x]cost[y][x]數組,一種顯然的方式就是直接20?2020*2020?20枚舉y,xy,xy,x,讓后O(n)O(n)O(n)掃一遍統計逆序對即可,這個復雜度為O(n?400)O(n*400)O(n?400)。還有一種比較快的方式就是從頭掃,記cnt[i]cnt[i]cnt[i]為到當前點顏色iii出現的次數,讓后再枚舉其他顏色,加上答案即可,復雜度O(n?20)O(n*20)O(n?20)
復雜度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; } /**/

總結

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

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