codeforces 755F F. PolandBall and Gifts(贪心+多重背包)
题目链接:
1.5 seconds
256 megabytes
standard input
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 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 2
3 4 1 5 2
2 4
10 1
2 3 4 5 6 7 8 9 10 1
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(贪心+多重背包)的更多相关文章
- Codeforces 755 F. PolandBall and Gifts 多重背包+贪心
F. PolandBall and Gifts It's Christmas time! PolandBall and his friends will be giving themselves ...
- 【codeforces 755F】PolandBall and Gifts
[题目链接]:http://codeforces.com/contest/755/problem/F [题意] n个人; 计划是每个人都拿一个礼物来送给一个除了自己之外的人; 且如果一个人没有送出礼物 ...
- POJ 2392 Space Elevator(贪心+多重背包)
POJ 2392 Space Elevator(贪心+多重背包) http://poj.org/problem?id=2392 题意: 题意:给定n种积木.每种积木都有一个高度h[i],一个数量num ...
- Atcoder Regular Contest 096 D - Sweet Alchemy(贪心+多重背包)
洛谷题面传送门 & Atcoder 题面传送门 由于再过 1h 就是 NOI 笔试了所以题解写得会略有点简略. 考虑差分,记 \(b_i=c_i-c_{fa_i}\),那么根据题意有 \(b_ ...
- CodeForces755F 贪心 + 多重背包二进制优化
https://cn.vjudge.net/problem/615831/origin 题意 n个人; 计划是每个人都拿一个礼物来送给一个除了自己之外的人; 如果一个人没有送出礼物,那么它和它送礼 ...
- HDU 2844 二进制优化的多重背包
Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- Codeforces 755F PolandBall and Gifts bitset + 二进制优化多重背包
PolandBall and Gifts 转换成置换群后, 对于最大值我们很好处理. 对于最小值, 只跟若干个圈能否刚好组能 k 有关. 最直观的想法就是bitset优化背包, 直接搞肯定T掉. 我们 ...
- Codeforces 106 C 多重背包
题目链接:http://codeforces.com/problemset/problem/106/C 根据题意列出式子,设每种蛋糕做了xi个,则对于每种材料bi*xi<=ai. 对于dough ...
- Educational Codeforces Round 61 (Rated for Div. 2) E 多重背包优化
https://codeforces.com/contest/1132/problem/E 题意 有8种物品,重量是1~8,每种数量是\(cnt[i]\)(1e16),问容量为W(1e18)的背包最多 ...
随机推荐
- oracle导入数据库报错:IMP-00019: 由于 ORACLE 错误 12899 而拒绝行 IMP-00003: 遇到 ORACLE 错误 12899
主要是字符集 成 导入的Oracle服务器的字符集 对应不上. 以下方案为是修改服务器的字符集. 这样会影响之前的Oracle其他数据库的数据显示(正式服务器慎用) 个人认为应该修改导出文件的字 ...
- iOS git 托管代码 常用几个操作
学习 git 切换分支 1 从远程下载一个分支develop(本地没有的) (1) git fetch origin develop (2) git checkout develop (默认 分支切 ...
- VMWare中安装windowsXP遇到的问题
XP系统安装 1.安装Windows和安装linux不一样,创建虚拟机完成后Linux自动根据硬盘进行系统安装,不需要提前分区.而windows必须进行提前分区,这个分区是在虚拟磁盘上完成的,就是你创 ...
- 用js来实现那些数据结构 第一章
在开始正式的内容之前,不得不说说js中的数据类型和数据结构,以及一些比较容易让人混淆的概念.那么为什么要从数组说起?数组在js中是最常见的内存数据结构,数组数据结构在js中拥有很多的方法,很多初学者记 ...
- HAproxy 介绍
HAproxy 介绍 (1)HAProxy 是一款提供高可用性.负载均衡以及基于TCP(第四层)和HTTP(第七层)应用的代理软件,支持虚拟主机,它是免费.快速并且可靠的一种解决方案. HAProxy ...
- CSS3透明背景表单
在线演示 本地下载
- 写时拷贝(Copy On Write)方案详解
本文旨在通过对 写时拷贝 的四个方案(Copy On Write)分析,让大家明白写时拷贝的实现及原理. 关于浅拷贝与深拷贝,我在之前的博客中已经阐述过了 浅拷贝容易出现指针悬挂的问题,深拷贝效率低 ...
- UI基础_transform
#import "ViewController.h" typedef enum { ButtonTypeLeft = 1, ButtonTypeRight, ButtonTypeU ...
- 让FireFox支持 window.event 全局事件对象
这里比原文稍加改进,让FF也支持 event.srcElement了, 省得每次写兼容代码挺麻烦的: //For firefox window.event if(typeof(window.event ...
- spring boot 默认配置bug
问题场景:请求很耗时,当一次请求完成后,之后的20秒内的请求很快速,在之后的第一个请求很慢! 每隔一段时间,请求就会出发解压jar的操作,不确定是操作系统的问题还是sping-boot的bug &qu ...