【codeforces 755F】PolandBall and Gifts
【题目链接】:http://codeforces.com/contest/755/problem/F
【题意】
n个人;
计划是每个人都拿一个礼物来送给一个除了自己之外的人;
且如果一个人没有送出礼物,那么它和它送礼物的对象都得不到礼物;
但是已经知道有k个人会忘记带礼物来;
问最少有几个人收不到礼物,最多有多少个人收不到礼物
【题解】
①
对于最多的情况;
最后会形成多个环;
每个环隔一个人选一个人忘记带礼物;这样一个长度为len的环只要len/2个人没带礼物就能够整个环的人都收不到礼物了;
然后如果len为奇数就放在最后再处理;因为那些多余的一个人必须得用1个名额来补充;所以先放在最后;因为你当前一个名额能够抵消两个人,肯定优先抵消两个人;
②
对于最少的情况;
就是k个名额刚好分配每个环的人数;
如果不是刚好;
则会多出一个人即k+1
那么问题就转换成一个背包问题了;
把每个环的长度看成一个物品,同种长度的环可能有多个
->多重背包;
问能不能刚好体积为k;
这里用到了多重背包的二进制优化;
然后还用到了bitset的技巧;
即把增加容量和
二进制的左移操作对应;
很厉害。。
【Number Of WA】
3
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) cin >> x
#define pri(x) cout << x
#define ms(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e6+1000;
int n,k,a[N],b[N],num,cnt,ans1,ans2,w[N];
bool vis[N];
bitset<N> f;
int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false);
rei(n),rei(k);
rep1(i,1,n)
rei(a[i]);
rep1(i,1,n)
if (!vis[i])
{
num++,cnt = 0;
int j = i;
while (!vis[j])
{
vis[j] = true;
cnt++;
j = a[j];
}
b[num] = cnt;
}
n = num;
sort(b+1,b+1+n);
int rest = k,t = 0;
rep1(i,1,n)
{
if (b[i]/2<=rest)
{
rest-=b[i]/2;
ans2+=b[i]/2*2;
}
else
{
ans2+=rest*2;rest = 0;
break;
}
if (b[i]&1) t++;
}
ans2+=min(rest,t);
num = 0;
rep1(i,1,n)
{
int j = i;
while (j+1<=n && b[j+1]==b[i]) j++;
int t = 1,len = j-i+1;
while (len>=t)
{
w[++num] = t*b[i];
len-=t;
t<<=1;
}
if (len)
w[++num] = len*b[i];
i = j;
}
n = num;
f[0] = 1;
rep1(i,1,n)
f|=f<<(w[i]);
ans1 = k+1;
if (f[k])
ans1 = k;
pri(ans1<<' '<<ans2<<endl);
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
【codeforces 755F】PolandBall and Gifts的更多相关文章
- 【codeforces 755B】PolandBall and Game
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 755A】PolandBall and Hypothesis
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 755D】PolandBall and Polygon
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 755C】PolandBall and Forest
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 755E】PolandBall and White-Red graph
[题目链接]:http://codeforces.com/contest/755/problem/E [题意] 给你n个节点; 让你在这些点之间接若干条边;构成原图(要求n个节点都联通) 然后分别求出 ...
- 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 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【27.85%】【codeforces 743D】Chloe and pleasant prizes
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
随机推荐
- [HNOI2006]潘多拉的宝盒
https://www.zybuluo.com/ysner/note/1250303 题面 给定\(s\)个自动机,如果某个自动机\(A\)能产生的所有串都能在自动机\(B\)中产生(即走相同\(0/ ...
- distpicker三级联动,动态改变省市信息
一.引入3个js文件 <script type="text/javascript" src="js/distpicker.data.js">< ...
- mac 安装 swoole 可能会出现的错误
请先看完之后再操作 一.用pecl安装swoole(没有安装起来) 2018年4月,由于homebrew的变动,导致无法使用brew install的方式安装php的扩展,现在改为用pecl安装,pe ...
- 写出更好的 JavaScript 条件语句
1. 使用 Array.includes 来处理多重条件 // 条件语句 function test(fruit) { if (fruit == 'apple' || fruit == 'strawb ...
- Nginx一个实现负载均衡的强大web server
<转>nginx 这个轻量级.高性能的 web server 主要可以干两件事情: 〉直接作为http server(代替apache,对PHP需要FastCGI处理器支持): 〉另外一个 ...
- scala控制流程语句
直接上代码了哈. package com.test.scala.test object Kongzi { def main(args: Array[String]): Unit = { //if 语句 ...
- Spring Cloud (6) 分布式配置中心-高可用
高可用 现在已经可以从配置中心读取配置文件了,当微服务很多时都从配置中心读取配置文件,这时可以将配置中心做成一个微服务,将其集群化,从而达到高可用. 改造config-server 加入eureka ...
- vue杂记
VUE杂记 声明式渲染 <div id="app"> {{ message }} </div> var app = new Vue({ el: '#app' ...
- SQL基本操作——row_number() over()
row_number() 与over()是在一起使用的,作用就是对表进行排序并记数. 语法: ROW_NUMBER ( ) OVER ( [ PARTITION BY value_expression ...
- C# 学习——静态(第四天)
一.命名空间 类似于文件夹,而类就是文件夹中的文件: 作用:明确的指向我们所需要的类的 所在的位置: 统一命名空间下,类名不能重复. 二.类 概念:具有相同属性和功能的对象的抽象的集合. 三.静态与实 ...