题目链接:

F. PolandBall and Gifts

time limit per test

1.5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 ≤ 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 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.

Examples
input
5 2
3 4 1 5 2
output
2 4
input
10 1
2 3 4 5 6 7 8 9 10 1
output
2 2

题意:

这是一个带礼物的置换,只有当你给带了礼物且给你带礼物的那个人带了礼物你才能获得礼物,现在有k个人没带礼物,问最少和最多有多少人无法获得礼物;

思路:

先把这个置换写成循环的形式,对于一个长度为x的循环,现在假设其中y个没带礼物,那么最少会有y+1个人无法获得礼物,如果x==y,那么就有y个人无法获得礼物,最多有2*y个人没法获得礼物;
所以现在答案的贪心条件就明晰了;
假设这个置换可以写成num个循环,分别长为bi,那么其中一些bi的和等于k那么minans=k,or minans=k+1;这时发现是一个背包问题,可以发现bi的种类大约在sqrt(n)附近,
所以像多重背包那样把bi的数目转化成二进制的和,这样复杂度就降下来了,还有多重背包可以用单调队列优化
maxans直接贪心就好; AC代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+5;
int n,k,vis[maxn],a[maxn],b[maxn],c[maxn],d[maxn];
bool dp[maxn];
int main()
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
int ans1=k,ans2=0,cnt=0;
for(int i=1;i<=n;i++)
{
if(vis[i])continue;
int number=0,p=i;
while(!vis[p])
{
vis[p]=1;
number++;
p=a[p];
}
b[++cnt]=number;
}
sort(b+1,b+cnt+1);
int kk=k,num=0;
for(int i=1;i<=cnt;++i)
{
if(b[i]/2<=kk)ans2+=b[i]/2*2,kk-=b[i]/2;
else ans2+=2*kk,kk=0;
if(b[i]&1)num++;
}
ans2+=min(kk,num);
num=0;
int number=0;
for(int i=1;i<=cnt;++i)
{
if(b[i]==b[i-1])d[num]++;
else d[++num]=1,a[num]=b[i];
}
cnt=0;
for(int i=1;i<=num;++i)
{
for(int j=1;d[i];j<<=1)
{
int tep=min(d[i],j);
c[++cnt]=a[i]*tep;
d[i]-=tep;
}
}
dp[0]=true;
for(int i=1;i<=cnt;++i)
for(int j=k-c[i];j>=0;--j)
if(dp[j])dp[j+c[i]]=true;
if(!dp[k])ans1++;
printf("%d %d\n",ans1,ans2);
return 0;
}

  

codeforces 755F F. PolandBall and Gifts(贪心+多重背包)的更多相关文章

  1. Codeforces 755 F. PolandBall and Gifts 多重背包+贪心

    F. PolandBall and Gifts   It's Christmas time! PolandBall and his friends will be giving themselves ...

  2. 【codeforces 755F】PolandBall and Gifts

    [题目链接]:http://codeforces.com/contest/755/problem/F [题意] n个人; 计划是每个人都拿一个礼物来送给一个除了自己之外的人; 且如果一个人没有送出礼物 ...

  3. POJ 2392 Space Elevator(贪心+多重背包)

    POJ 2392 Space Elevator(贪心+多重背包) http://poj.org/problem?id=2392 题意: 题意:给定n种积木.每种积木都有一个高度h[i],一个数量num ...

  4. Atcoder Regular Contest 096 D - Sweet Alchemy(贪心+多重背包)

    洛谷题面传送门 & Atcoder 题面传送门 由于再过 1h 就是 NOI 笔试了所以题解写得会略有点简略. 考虑差分,记 \(b_i=c_i-c_{fa_i}\),那么根据题意有 \(b_ ...

  5. CodeForces755F 贪心 + 多重背包二进制优化

    https://cn.vjudge.net/problem/615831/origin 题意 n个人;  计划是每个人都拿一个礼物来送给一个除了自己之外的人;  如果一个人没有送出礼物,那么它和它送礼 ...

  6. HDU 2844 二进制优化的多重背包

    Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  7. Codeforces 755F PolandBall and Gifts bitset + 二进制优化多重背包

    PolandBall and Gifts 转换成置换群后, 对于最大值我们很好处理. 对于最小值, 只跟若干个圈能否刚好组能 k 有关. 最直观的想法就是bitset优化背包, 直接搞肯定T掉. 我们 ...

  8. Codeforces 106 C 多重背包

    题目链接:http://codeforces.com/problemset/problem/106/C 根据题意列出式子,设每种蛋糕做了xi个,则对于每种材料bi*xi<=ai. 对于dough ...

  9. Educational Codeforces Round 61 (Rated for Div. 2) E 多重背包优化

    https://codeforces.com/contest/1132/problem/E 题意 有8种物品,重量是1~8,每种数量是\(cnt[i]\)(1e16),问容量为W(1e18)的背包最多 ...

随机推荐

  1. [String ] StringBuffer VS StringBuilder

    StringBuilder的性能高于StringBuffer,因为StringBuffer是线程安全的. 首先说明一下,一般情况下,字符串相加默认是StringBuilder,但是当数量大于100,或 ...

  2. C语言转义字符的使用方法

    cppreference.com -> 转义字符 常量转义字符 以下的转义字符使普通字符表示不同的意义. 转义字符 描述 \' 单引号 \" 双引号 \\ 反斜杠 \0 空字符 \a ...

  3. Python之迭代器和生成器(Day17)

    一.可迭代对象(iterable) 刚才说过,很多容器都是可迭代对象,此外还有更多的对象同样也是可迭代对象,比如处于打开状态的files,sockets等等.但凡是可以返回一个迭代器的对象都可称之为可 ...

  4. 【转】通过fio工具,测试SATA,SAS,SSD 读写性能

      转自:http://blog.csdn.net/killmice/article/details/42745937

  5. OC源文件扩展名

    常见的文件扩展名 扩展名 含义 扩展名 含义 .c C语言源文件 .mm Objective-C++源文件 .cc..cpp C++源文件 .pl Perl源文件 .h 头文件 .o Object(编 ...

  6. UI控件之UITableView的基本属性

    UITableView:特殊的滚动视图,横向固定,可以在纵向上滚动,自动计算contentSize 创建tableView,初始化时指定样式,默认是plain UITableView *_tableV ...

  7. first application

    <!DOCTYPE html> <html> <head> <title>Create a Map</title> <meta htt ...

  8. Cocos2d-x项目移植到WP8系列之六:C#工程使用C++的DLL

    原文链接: http://www.cnblogs.com/zouzf/p/3984510.html 此时,一些大问题都被解决后,整个工程基本能跑起来了,最后一个大问题是:业务层是用Lua开发的,底层的 ...

  9. iOS_KVC与KVO

    目 录: 一.KVC   二.KVO                                                                                   ...

  10. 关于C++ 中的this 的理解

    关键字this 通常被用在一个class内部,指正在被执行的该class的对象(object)在内存中的地址.它是一个指针,其值永远是自身object的地址.