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)的背包最多 ...
随机推荐
- 系统日志服务rsyslog
一.系统日志服务rsyslog:多线程,可以基于UDP.TCP.TLS协议进行远程通信,还可以将数据存储到MySQL.PGSQL.Oracle,强大的过滤器,可实现过滤日志信息中任何部分,可以自定义输 ...
- iOS开发之计算器
本项目基于swift3.0的语法. // // ViewController.swift // 加法计算器 // // Created by 葛杨杨 on 2017/7/25. // Copyrigh ...
- python之路 模块,序列化,迭代器,生成器
一.模块 1.模块简介 模块是一个包含所有你定义的函数和变量的文件,其后缀名是.py.模块可以被别的程序引入,以使用该模块中的函数等功能.这也是使用python标准库的方法. 类似于函数式编程和面向过 ...
- qt的udp的初步使用(转)
该程序实现的功能是:局域网内,每个用户登录到聊天软件,则软件界面的右端可以显示在线用户列表,分别显示的是用户名,主机名,ip地址.软件左边那大块是聊天内容显示界面,这里局域网相当于qq中的qq群,即群 ...
- imx6qsbd lvds dtc
lvds显示屏调试参考 1.基于飞思卡尔imxsolosabresd开发板Linux-3.10.53 lvds屏幕调试: http://blog.csdn.net/qq_37375427/articl ...
- 【Head First Servlets and JSP】笔记14:session再探 & Listener示例
对于session的“CRUD” 会话迁移 别忘了HttpSessionBindingListener Listener示例 1.session的“增”与“删”——session的创建和撤销的调用主体 ...
- github使用——如何恢复被删去文件。
首先git删除文件包括以下几种情况 删除本地文件,但是未添加到暂存区: 删除本地文件,并且把删除操作添加到了暂存区: 把暂存区的操作提交到了本地git库: 把本地git库的删除记录推送到了远程服务器g ...
- 使用ASP.Net MVC5 Web API OData和Sencha Touch 开发WebAPP
使用ASP.Net MVC5 Web API OData和SenCha Touch 开发WebAPP Demo 效果 第一步 创建数据库 创建表 第二步 搭建MVC,并导入OData 第三步,写入We ...
- winter 2018 02 01 关于模运算的一道题
题目:给出一个正整数n,问是否存在x,满足条件2^x mod n=1,如果存在,求出x的最小值. 分析:1.若给出的n是1,则肯定不存在这样的x; 2.若给出的是偶数,2的次幂取余一个偶数得到 ...
- MongoDB快速入门(二)- 数据库
创建数据库 MongoDB use DATABASE_NAME 用于创建数据库.该命令如果数据库不存在,将创建一个新的数据库, 否则将返回现有的数据库. 语法 use DATABASE语句的基本语法如 ...