Codeforces 755 F. PolandBall and Gifts 多重背包+贪心
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
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个人,每个人固定给一个人送一个gift,不会给自己送gift,不会有一个人收到两个gift
也就是说,每个人会收到一个gift和送出去一个gift,这样的人ans+1
现在你可以指定k个人不会送出去gift
ans最小和最大是多少
题解:
划分为多个置换群
ans最大去贪心就好了
最小的话: 如果选取置换群大小相加存在等于k的最小就是k,否则是k+1
利用以上:跑背包,是否能构成K,范围太大,需要二进制优化多重背包优化到n*∑log(b[i]),另外bitset优化
#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double Pi = acos(-1.0);
const int N = 1e6+, maxn = 1e3+, mod = 1e9+, inf = 2e9; int a[N],b[N],n,k,vis[N],H[N],cnt;
int main() {
scanf("%d%d",&n,&k);
for(int i = ; i <= n; ++i) scanf("%d",&a[i]);
for(int i = ; i <= n; ++i) {
if(vis[i]) continue;
int j = a[i],cnts = ;
while(!vis[j]) {
cnts++;
vis[j] = ;
j = a[j];
}
b[++cnt] = cnts;
}
sort(b+,b+cnt+);
int ans2 = ,kk = k;
for(int i = ; i <= cnt; ++i) {
if(kk >= b[i]/) kk-=b[i]/,ans2 += b[i]/*;
else {
ans2 += *kk;
kk = ;
break;
}
}
ans2 += kk;
ans2 = min(n,ans2);
for(int i = ; i <= cnt; ++i) H[b[i]]++;
bitset<N> dp;
dp.set();
for(int i = ; i <= n; ++i) {
if(H[i] == ) continue;
int t = H[i],l = ;
while(t) {
int w = min(t,l);
t -= w;
dp |= dp<<(w*i);
l <<= ;
}
if(dp[k] == ) {
printf("%d %d\n",k,ans2);
return ;
}
}
printf("%d %d\n",+k,ans2);
return ;
}
Codeforces 755 F. PolandBall and Gifts 多重背包+贪心的更多相关文章
- codeforces 755F F. PolandBall and Gifts(贪心+多重背包)
题目链接: F. PolandBall and Gifts time limit per test 1.5 seconds memory limit per test 256 megabytes in ...
- 【codeforces 755F】PolandBall and Gifts
[题目链接]:http://codeforces.com/contest/755/problem/F [题意] n个人; 计划是每个人都拿一个礼物来送给一个除了自己之外的人; 且如果一个人没有送出礼物 ...
- $POJ1742\ Coins$ 多重背包+贪心
Vjudge传送门 $Sol$ 首先发现这是一个多重背包,所以可以用多重背包的一般解法(直接拆分法,二进制拆分法...) 但事实是会TLE,只能另寻出路 本题仅关注“可行性”(面值能否拼成)而不是“最 ...
- 01背包模板、全然背包 and 多重背包(模板)
转载请注明出处:http://blog.csdn.net/u012860063 贴一个自觉得解说不错的链接:http://www.cppblog.com/tanky-woo/archive/2010/ ...
- 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 ...
- CodeCraft-19 and Codeforces Round #537 (Div. 2) D 多重排列 + 反向01背包 + 离线处理
https://codeforces.com/contest/1111/problem/D 多重排列 + 反向01背包 题意: 给你一个字符串(n<=1e5,n为偶数),有q个询问,每次询问两个 ...
- Educational Codeforces Round 61 (Rated for Div. 2) E 多重背包优化
https://codeforces.com/contest/1132/problem/E 题意 有8种物品,重量是1~8,每种数量是\(cnt[i]\)(1e16),问容量为W(1e18)的背包最多 ...
- 完全背包和多重背包的混合 F - The Fewest Coins
http://poj.org/problem?id=3260 这个题目有点小难,我开始没什么头绪,感觉很乱. 后来看了题解,感觉豁然开朗. 题目大意:就是这个人去买东西,东西的价格是T,这个人拥有的纸 ...
随机推荐
- CSS3---圆角设置
1.border-radius是向元素添加圆角边框.border-radius:10px; /* 所有角都使用半径为10px的圆角 */ border-radius: 5px 4px 3px ...
- Python爬虫-爬取京东商品信息-按给定关键词
目的:按给定关键词爬取京东商品信息,并保存至mongodb. 字段:title.url.store.store_url.item_id.price.comments_count.comments 工具 ...
- Spring Boot Web开发中Thymeleaf模板引擎的使用
这里使用的是idea 1.新建Spring Boot项目 File-->New-->Project...,然后选择左边的Spring Initializr-->Next,可根据自己的 ...
- bzoj2973 入门oj4798 石头游戏
我们人为地搞出来一个全能神,每次调用他他可以给一个节点 \(1\) 个石头. 这样,当前的状态就可以由上一秒的状态搞过来,这就像是一个递推.用矩阵加速. #include <iostream&g ...
- BNUOJ 1575 Supermarket
Supermarket Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID ...
- 【Java 理论篇 1】Java2平台的三个版本介绍
导读:关于java的三种分类J2SE.J2EE.J2ME,在网上有很多资料,然后自己写的,也大多是从各个网站上搜罗里的.算是自己的一种笔记,或者明白的说,就是把别人的东西抄一遍.但是,这对于我来说,也 ...
- hdu 1879
#include<stdio.h> #include<stdlib.h> #define N 100 struct node { int x,y,dis; }road[N*N] ...
- HDU 6311 最少路径覆盖边集 欧拉路径
Cover Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- struts2中的session使用
1.1. 如何获取Session 1.1.1. 获取Session的方式 Struts2中获取Session的方式有3种,大家掌握其中任何一种都可以. 通过ActionContext.getConte ...
- 洛谷——P1265 公路修建
P1265 公路修建 题目描述 某国有n个城市,它们互相之间没有公路相通,因此交通十分不便.为解决这一“行路难”的问题,政府决定修建公路.修建公路的任务由各城市共同完成. 修建工程分若干轮完成.在每一 ...