欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 前端技术 > javascript >内容正文

javascript

Deltix Round, Spring 2021 D. Love-Hate 随机化 + sos dp(高维前缀和)

发布时间:2023/12/4 javascript 47 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Deltix Round, Spring 2021 D. Love-Hate 随机化 + sos dp(高维前缀和) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

传送门

文章目录

  • 题意:
  • 思路:

题意:

给你nnn个朋友,一共有mmm种货币,一个朋友最多喜欢ppp种,用二进制给出111代表喜欢,让你选出最多的一个货币集合使得至少有⌈n2⌉\left \lceil \frac{n}{2} \right \rceil2n个朋友都喜欢货币集合中的每一个货币。
n≤2e5,1≤m≤60,1≤p≤15n\le2e5,1\le m\le 60,1 \le p \le 15n2e5,1m60,1p15

思路:

很明显有一个随机化算法,如果我们从中随机选一个人,那么这个人在最终的喜欢货币集合的⌈n2⌉\left \lceil \frac{n}{2} \right \rceil2n人中的概率是12\frac{1}{2}21,所以我们只需要选100100100次就可以基本保证能选到最终的某一个人,错误率为12100\frac{1}{2^{100}}21001
假设我们已经知道某个人一定选了,那么我们最终的货币集合一定是这个人喜欢的货币的子集,因为如果最终集合中存在他不喜欢的,那么这个人最终就不会被选到。
我们现在就可以用一个高维前缀和推算出每个子集有多少个人喜欢,让后取一个满足条件的最大值就好啦。
由于这个不是由子集向上推,所以我们求高维前缀和只需要将原来的0,10,10,1互换一下,就可以实现由"我们认知的高维(也就是111多的状态)"来向下推低维(111少的状态)了。

// Problem: D. Love-Hate // Contest: Codeforces - Deltix Round, Spring 2021 (open for everyone, rated, Div. 1 + Div. 2) // URL: https://codeforces.com/contest/1523/problem/D // Memory Limit: 256 MB // Time Limit: 3000 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<type_traits> #include<chrono> #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,p,m; LL state[N]; char s[N]; int a[N]; mt19937 rnd(time(0));int get(LL x) {int cnt=0;while(x) cnt+=x%2,x/=2;return cnt; }int main() { // ios::sync_with_stdio(false); // cin.tie(0);scanf("%d%d%d",&n,&m,&p);for(int i=1;i<=n;i++) {scanf("%s",s+1);for(int j=1;j<=m;j++) if(s[j]-'0'==1) state[i]+=1ll<<(j-1);}LL ans=0;for(int i=1;i<=100;i++) {int pos=rnd()%n+1;vector<int>v;for(int i=0;i<m;i++) if(state[pos]>>i&1) v.pb(i);int sz=v.size();vector<int>sum(1ll<<(v.size()));for(int i=1;i<=n;i++) {LL now=0;for(int j=0;j<sz;j++) if(state[i]>>v[j]&1) now+=1ll<<j; sum[now]++;}for(int i=0;i<v.size();i++) {for(int now=0;now<(1<<sz);now++) {if(!(now&(1<<i))) {sum[now]+=sum[now^(1<<i)];}}}for(int i=0;i<(1<<sz);i++) {if(sum[i]*2>=n&&get(i)>get(ans)) {LL now=0;for(int j=0;j<sz;j++) if(i>>j&1) now+=1ll<<v[j];ans=now;}}}for(int i=0;i<m;i++) printf("%d",ans>>i&1);return 0; } /**/ 创作挑战赛新人创作奖励来咯,坚持创作打卡瓜分现金大奖

总结

以上是生活随笔为你收集整理的Deltix Round, Spring 2021 D. Love-Hate 随机化 + sos dp(高维前缀和)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。