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. vue相关安装命令

    安装cnpm npm install cnpm -g --registry=https://registry.npm.taobao.org

  2. ThreadLocal的实现

    0.简介:创建线程局部变量的类 使用ThreadLocal创建的变量只能被当前线程访问,其他线程则无法访问和修改. 内部类ThreadLocalMap实现,key是变量,value是所在的线程. 用法 ...

  3. 3.Filter和interceptor的区别

    https://blog.csdn.net/qq_36411874/article/details/53996873

  4. CF&&CC百套计划3 Codeforces Round #204 (Div. 1) D. Jeff and Removing Periods

    http://codeforces.com/problemset/problem/351/D 题意: n个数的一个序列,m个操作 给出操作区间[l,r], 首先可以删除下标为等差数列且数值相等的一些数 ...

  5. Spyder简述

    导言 想打造轮子, 就必须要有一套完善的造轮子的工具. 我在jupyter+sciTE的组合里转来转去, 最后还是打算放弃这个组合, 因为离开了自动完成/调用提示/随时随地的访问文档帮助, 前行之路太 ...

  6. [译]Quartz.NET 框架 教程(中文版)2.2.x 之第四课 更多关于Triggers

    第四课 更多关于Triggers 跟作业任务类似,触发器也非常容易使用,但是在你能够充分掌握Quartz之前,你需要知道并理解许多触发器的客户化的参数.前面已经提到过,有许多不同类型的触发器供你选择, ...

  7. bzoj 3790 神奇项链(Manacher,DP+BIT | 贪心)

    [题意] 你可以产生一个回文串,也可以将两个串合并成一个串,问产生目标串需要的最少合并次数. [思路] 显然我们要先产生目标串中包含的极大回文字符串. Manacher求出每个位置可以向两边延伸的最长 ...

  8. 20155301 2016-2017-2 《Java程序设计》第5周学习总结

    20155301 2016-2017-2 <Java程序设计>第5周学习总结 教材学习内容总结 1.1try.catch关键词,在用户不小心输入错误的时候,程序会出现错误信息,将代表错误的 ...

  9. 树形dp(A - Anniversary party HDU - 1520 )

    题目链接:https://cn.vjudge.net/contest/277955#problem/A 题目大意:略 具体思路:刚开始接触树形dp,说一下我对这个题的初步理解吧,首先,我们从根节点开始 ...

  10. LINUX vim 修改文件 退出

    vim 保存退出, 先按ESC ,然后:wq(保存退出)W:write,写入 Q:quit,退出, 也可以直接输入X,代表WQ,也是保存退出 或者 先按ESC,再按shift+ZZ 也是保存退出 正常 ...