D - Bags and Coins

思路:我们可以这样构造,最大的那个肯定是作为以一个树根,所以我们只要找到一个序列a1 + a2 + a3 .... + ak 并且ak为

所有点中最大的那个,那么我们a1, a2, a3..., ak-1 作为单独的点,其他没有涉及到的点套在ak的里面。

现在问题变成了找出a1, a2, a3, a4, ... , ak。 可以用bitset优化普通dp,因为要找路径,空间开不下,所以需要分段。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long
using namespace std; const int N = + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-; int n, s;
PII a[N];
bitset<> dp[N/];
bitset<> dp2;
bitset<> tmp;
vector<int> edge[N];
vector<PII> dol;
int c[N];
bool in[N]; bool ok(int x, int y) {
if(y < ) return false;
dp2 = dp[x/];
for(int i = x/*+; i <= x; i++)
dp2 |= (dp2 << a[i].fi);
return dp2[y];
} int main() {
scanf("%d%d", &n, &s);
for(int i = ; i <= n; i++) {
scanf("%d", &a[i].fi);
a[i].se = i;
}
sort(a+, a++n);
int tar = s - a[n].fi;
tmp[] = ; dp[] = tmp;
for(int i = ; i < n; i++) {
tmp |= (tmp<<a[i].fi);
if(i % == ) dp[i/] = tmp;
}
if(ok(n-, tar)) {
for(int i = n-, now = tar; i >= ; i--) {
if(now >= a[i].fi && ok(i-, now-a[i].fi)) {
now = now - a[i].fi;
in[a[i].se] = true;
}
}
for(int i = ; i < n; i++) {
if(in[a[i].se]) {
c[a[i].se] = a[i].fi;
} else {
dol.push_back(a[i]);
}
}
dol.push_back(a[n]);
for(int i = ; i < dol.size(); i++) {
if(i) {
c[dol[i].se] = dol[i].fi - dol[i-].fi;
edge[dol[i].se].push_back(dol[i-].se);
} else {
c[dol[i].se] = dol[i].fi;
}
}
for(int i = ; i <= n; i++) {
printf("%d %d ", c[i], edge[i].size());
for(int j : edge[i]) printf("%d ", j);
puts("");
}
} else {
puts("-1");
}
return ;
} /*
*/

Codeforces Round #207 (Div. 1) D - Bags and Coins 构造 + bitset优化dp + 分段查找优化空间的更多相关文章

  1. Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)

    题目链接:Codeforces Round #275 (Div. 2) C - Diverse Permutation 题意:一串排列1~n.求一个序列当中相邻两项差的绝对值的个数(指绝对值不同的个数 ...

  2. Codeforces Round #207 (Div. 1) A. Knight Tournament (线段树离线)

    题目:http://codeforces.com/problemset/problem/356/A 题意:首先给你n,m,代表有n个人还有m次描述,下面m行,每行l,r,x,代表l到r这个区间都被x所 ...

  3. Codeforces Round #207 (Div. 1) A. Knight Tournament(STL)

    脑子又卡了...来一发set的,STL真心不熟. #include <stdio.h> #include <string.h> #include <iostream> ...

  4. Codeforces Round #207 (Div. 2) A. Group of Students

    #include <iostream> #include <vector> using namespace std; int main(){ ; cin >> m ...

  5. Codeforces Round #207 (Div. 1)B(数学)

    数学so奇妙.. 这题肯定会有一个循环节 就是最小公倍数 对于公倍数内的相同的数的判断 就要借助最大公约数了 想想可以想明白 #include <iostream> #include< ...

  6. Codeforces Round #207 (Div. 2)C

    读错题意了..线段树延迟标记 白刷这么多线段树 #include <iostream> #include<cstdio> #include<cstring> #in ...

  7. Codeforces Round #207 (Div. 2)

    A:超级大水题: 代码: #include<cstdio> #define maxn 105 using namespace std; int n,a[maxn],x,y,ans; int ...

  8. Codeforces Round #207 (Div. 1) B. Xenia and Hamming(gcd的运用)

    题目链接: B. Xenia and Hamming 题意: 要求找到复制后的两个字符串中不同样的字符 思路: 子问题: 在两串长度是最大公倍数的情况下, 求出一个串在还有一个串中反复字符的个数 CO ...

  9. Codeforces Round #207 (Div. 2)A B C E 水 思路 set 恶心分类

    A. Group of Students time limit per test 1 second memory limit per test 256 megabytes input standard ...

随机推荐

  1. Camera ISO、快门、光圈、曝光这几个概念

    转载自知乎:https://www.zhihu.com/question/21427664 种田要知节气,开车要懂离合,任何一样手艺都有行话.虽然我觉得尽量从实际问题说起,尽量不要说的很专业,但有几个 ...

  2. N制和PAL制区别

  3. CF&&CC百套计划2 CodeChef December Challenge 2017 Total Diamonds

    https://www.codechef.com/DEC17/problems/VK18 #include<cstdio> #include<iostream> #includ ...

  4. [洛谷P3643] [APIO2016]划艇

    洛谷题目链接:[APIO2016]划艇 题目描述 在首尔城中,汉江横贯东西.在汉江的北岸,从西向东星星点点地分布着 \(N\) 个划艇学校,编号依次为 \(1\) 到 \(N\).每个学校都拥有若干艘 ...

  5. Python核心编程——Chapter15

    正则表达式在脚本语言里是最重要的一部分,这部分的题目真的不容怠慢. 开始这部分的题目的解答! 15.1识别下列字符串:bat,bit,but,hat,hit和hut. >>> imp ...

  6. PartyPlay发布版

    发布版本在beta版本的基础之上修改了pose王中的显示函数部分,使其可以正常的多次显示不同图片,不会出现卡在一张图片的问题上. 因此总的发布版本内容: 1.谁是卧底: 点击进入谁是卧底,进入游戏前的 ...

  7. 让老版本IE支持HTML5

    一直想入手C3和H5,但因为所开发的项目一直要求兼容IE7,IE8.而这两个浏览器并不支持html5,所以一直都在观望而未真正的投入太多精力去学习.尽管我知道h5和c3是主流. 在最近的项目开发中,偶 ...

  8. HDU 6205 (模拟) card card card

    题目链接 Problem Description As a fan of Doudizhu, WYJ likes collecting playing cards very much. One day ...

  9. 表单验证插件-validator.js 使用教程

    做网站的时候,常常会涉及到各种表单验证.选择一款好用的表单验证插件,会降低表单验证开发的难度.在开发中,我目前使用的表单验证插件是:validator.js. validator.js 是一款轻量的表 ...

  10. ubuntu 18.04 安装 flash

    下载源码包, 解压 sudo cp Downloads/flash_player_npapi_linux.x86_64/libflashplayer.so /usr/lib/mozilla/plugi ...