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 ppi ≠ 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:

  1. Ball number i will bring the present he should give.
  2. 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?

Input

The first line of input contains two integers n and k (2 ≤ n ≤ 1060 ≤ 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 ipi ≠ i holds.

Output

You should output two values — minimum and maximum possible number of Balls who will not get their presents, in that order.

Example
Input
5 23 4 1 5 2
Output
2 4
Input
10 12 3 4 5 6 7 8 9 10 1
Output
2 2
Note

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 );}


原创粉丝点击