codeforces 755F PolandBall and Gifts(多重背包)
来源:互联网 发布:海尔品牌价值知乎 编辑:程序博客网 时间:2024/06/06 18:26
It's Christmas time! PolandBall and his friends will be giving themselves gifts. There are n Balls overall. Each Ball has someone for whom he should bring a present according to some permutation p, pi ≠ i for all i.
Unfortunately, Balls are quite clumsy. We know earlier that exactly k of them will forget to bring their gift. A Ball number i will get his present if the following two constraints will hold:
- Ball number i will bring the present he should give.
- Ball x such that px = i will bring his present.
What is minimum and maximum possible number of kids who will not get their present if exactly k Balls will forget theirs?
The first line of input contains two integers n and k (2 ≤ n ≤ 106, 0 ≤ k ≤ n), representing the number of Balls and the number of Balls who will forget to bring their presents.
The second line contains the permutation p of integers from 1 to n, where pi is the index of Ball who should get a gift from the i-th Ball. For all i, pi ≠ i holds.
You should output two values — minimum and maximum possible number of Balls who will not get their presents, in that order.
5 23 4 1 5 2
2 4
10 12 3 4 5 6 7 8 9 10 1
2 2
In the first sample, if the third and the first balls will forget to bring their presents, they will be th only balls not getting a present. Thus the minimum answer is 2. However, if the first ans the second balls will forget to bring their presents, then only the fifth ball will get a present. So, the maximum answer is 4.
题意:就是说有的人忘带礼物了 忘带礼物的人不能收到礼物,给你 n个人 n个数 代表每个人该给哪个id送礼物,给定k有k个人忘带礼物了。问最少和最多多少个人没有礼物。这种情况就是一个环,然后环的话
,每隔一个人忘一个人的话可以填满整个环 对于环的长度是偶数,只需要一半的人就可以填满,对于环的长度是奇数,会还剩下一个人 ,最多多少个人没有礼物就可以贪心了。从小到大读环 并且记录奇数环的个数,因为最后如果k还有剩余的话,就填在这些奇数的位置。那么如何求最小的,很明显,求最小的 只有 k个人没有礼物或者 k+1个人没有礼物。如何判断是哪一个,就是已知物品的体积和个数,问你是否能填满能填满就是k,填不满就是k+1,由于这个数据量很大 所以是一个多重背包模型,另外再用一点位运算就好了
#include <bits/stdc++.h>using namespace std;const int N = 1e6+100;int p[N],a[N];vector<int> v;int wu[N*5];int vis[N];int dp[N];int du[N];int main(){int n,kk;scanf("%d%d",&n,&kk);int kk1=kk;for(int i=1;i<=n;i++)scanf("%d",&a[i]);for(int i=1;i<=n;i++){if(vis[i]) continue;int num=0;int k=i;while(!vis[k]){vis[k]=1;num++;k=a[k];}v.push_back(num);du[num]++;}sort(v.begin(),v.end());int num=0;int ans=0;for(int i=0;i<v.size();i++){int tt=v[i]/2;if(kk<=tt) ans+=2*kk,kk=0;else kk-=tt,ans+=2*tt;if(v[i]&1) num++;}ans+=min(kk,num);int cnt=0;v.erase(unique(v.begin(),v.end()),v.end());for(int i=0;i<v.size();i++){for(int j=1;du[v[i]];j<<=1){int hhh=min(du[v[i]],j);wu[cnt++]=hhh*v[i];du[v[i]]-=hhh;}}int ans2=kk1;dp[0]=1;for(int i=0;i<cnt;i++){for(int j=kk1;j>=wu[i];j--)if(dp[j-wu[i]]) dp[j]=dp[j-wu[i]];}if(!dp[kk1])ans2++;printf("%d %d\n",ans2,ans );}
- codeforces 755F PolandBall and Gifts(多重背包)
- Codeforces 755 F. PolandBall and Gifts (简单贪心+二进制优化多重背包)
- codeforces755F. PolandBall and Gifts(贪心+多重背包)
- codeforces 755 d PolandBall and Polygon(找规律)
- codeforces-755D-PolandBall and Polygon(数学题)
- 【codeforces 755A】PolandBall and Hypothesis
- 【codeforces 755B】PolandBall and Game
- 【codeforces 755C】PolandBall and Forest
- 【codeforces 755D】PolandBall and Polygon
- codeforces-755-B PolandBall and Game
- Codeforces 755-B. PolandBall and Game(贪心)
- 【Codeforces 755 B PolandBall and Game】
- Codeforces 755B-PolandBall and Game
- Codeforces 755C-PolandBall and Forest
- Codeforces 755D-PolandBall and Polygon
- CodeForces 755B PolandBall and Game
- CodeForces 755C PolandBall and Forest
- codeforces 755E PolandBall and White-Red graph
- Spring cache + redis 项目偶发死锁异常浅析
- 2017-8-9
- 课时21 Linux系统启动详解
- Unity使用暴风魔镜VR游戏开发
- 俗称最全---正则表达式---
- codeforces 755F PolandBall and Gifts(多重背包)
- 仿 Nice 首页图片列表 9 图样式 (iOS)
- Java中继承thread类与实现Runnable接口的区别
- React Native中组件的封装使用
- JDK配置
- 按钮的四种点击事件
- 汉诺塔问题
- JavaScript函数专题
- 网络通信——初学者java编程