D. Binary String To Subsequences #贪心 #构造

题目链接

题意

给定一个\(01\)串\(s\),完全分割成若干子序列(注意,不要混淆子串与子序列的概念),其中的子序列不包含两个相邻的\(0\)或\(1\)\((eg:"0101", "1010")\)。对\(s\)按这样的分割方式,最少能分出多少串子序列?同时,还要求输出\(s\)串中每一字符所在的分割出的子序列编号

分析

参考了官方题解的思路,我们可以定义两个数组为\(now0\)和\(now1\),\(now0\)存下了当前以0为结尾的所有10子序列,\(now1\)反之。

假定我们遇到\(str[i]\)为'\(0\)',为了使分割的子序列数量越少越好,当然是希望能够将这个字符'0'接入之前已创建好的子序列中,因此先检查\(now1\)是否存在以\(1\)结尾的10子序列。

如果存在,我们就接入(注意,接入后,该子序列的末尾是'0'了,因而需要在\(now0\)数组留下标记);如果不存在,我们只能从\(now0\)中创建一个新编号,注意这个编号是基于所有子序列的数量的。

  1. #include <string>
  2. #include <cstdio>
  3. #include <iostream>
  4. #include <vector>
  5. #include <string>
  6. using namespace std;
  7. typedef long long ll;
  8. int main(){
  9. int q; scanf("%d", &q);
  10. while(q--){
  11. vector<int> ans;
  12. vector<int> now1, now0;
  13. int n; scanf("%d", &n);
  14. string str; cin >> str;
  15. //利用vector模拟过程,便于查看当前集合的元素个数,及增加/修改元素
  16. for (int i = 0; i < n; i++){
  17. int newkey = now0.size() + now1.size();//newkey用于统计最新子序列编号(从0开始)
  18. if(str[i] == '0'){
  19. if(now1.empty()){ //暂无最新的01串,只能构建以0为开头的新子序列
  20. now0.push_back(newkey);
  21. } //新子串+1
  22. else{ //当前存在可接入的01串,为该串的尾部放置合适位置
  23. newkey = now1.back(); //注意获取最新01串的编号
  24. now0.push_back(newkey); //第newkey串结尾改为0
  25. now1.pop_back(); //第newkey串结尾不再是1
  26. }
  27. }
  28. else {
  29. if(now0.empty()){ //暂无最新的10串
  30. now1.push_back(newkey);
  31. }
  32. else{
  33. newkey = now0.back();
  34. now1.push_back(newkey);
  35. now0.pop_back();
  36. }
  37. }
  38. ans.push_back(newkey);
  39. }
  40. printf("%d\n", (int)(now1.size() + now0.size()));
  41. for (int i = 0; i < n; i++){
  42. printf("%d%c", ans[i] + 1, (i == n - 1) ? '\n' : ' ');
  43. }
  44. }
  45. }

其实\(for\)循环内部不需要分出两个大\(if\)判断,可以定义\(now[2][...]\)然后通过\(now[!newkey]\)进行一系列操作即可

E1. Weights Division #贪心 #大顶堆

题目链接

题意

给定每条边带一定权值的有根树。你可以将任意一条边的权值减少一半,即\(w_i:=wi/2\)(向下取整),每一次对某一边权值减少一半记为一次操作。定义树的总重量:根节点到所有叶子路径权值之和。现给定\(S\),需要你用最少的操作次数使得树的总重量不超过\(S\)。

分析

根节点到达某一叶子的路径,与 根节点到达另一叶子的路径,很有可能发生部分重合。而题干中不遗漏路径中所有经过边的权值,所以我们需要提前将任何边的经过次数\(num\)统计下来。

我们很容易往贪心的方向考虑。贪心什么?我最初贪心,是将当前所有边中,出现次数\(\times\)边权得到的加权最大的边挑出来并进行修改。然而,这样不准确,比如{\(w_1=5, num_1=4\)}与{\(w_2=2, num_2=10\)},他们总加权相等,但由于边权值缩小是向下取整,5缩小一半对总权重的影响比2缩小一半更大。从这里我们可以发现,贪心的目标应该是选择缩小对树重量的影响最大的边即缩小前与缩小后之差最大。

  1. #include <string>
  2. #include <cstdio>
  3. #include <iostream>
  4. #include <queue>
  5. using namespace std;
  6. typedef long long ll;
  7. const int MAXN = 2e5 + 5;
  8. int H[MAXN], tot = 0;
  9. ll s = 0, n, cursum = 0;
  10. typedef struct Edge {
  11. int to, nextNbr, num; ll w; //num记录该边的经过次数
  12. bool operator < (const struct Edge& x) const {
  13. return num * (w - w / 2) < x.num * (x.w - x.w / 2);
  14. } //对总权重影响最大的,优先取出
  15. }Edge;
  16. Edge E[MAXN * 2];
  17. priority_queue<Edge> myque;
  18. void addEdge(int u, int v, ll w) {
  19. tot++;
  20. E[tot].to = v; E[tot].w = w; E[tot].nextNbr = H[u]; E[tot].num = 1;
  21. H[u] = tot;
  22. }
  23. void Init() {
  24. for (int i = 1; i <= n; i++) H[i] = -1;
  25. tot = 0; cursum = 0;
  26. while (!myque.empty()) myque.pop();
  27. }
  28. int dfs(int now, int pre) {
  29. int cnt = 0; //cnt记录从now(除了叶节点)出发,它的出边的经过次数
  30. bool f = false;
  31. for (int i = H[now]; i >= 0; i = E[i].nextNbr) {
  32. if (E[i].to != pre) { //保证不回退
  33. E[i].num = dfs(E[i].to, now); //回溯返回该边的经过次数
  34. cnt += E[i].num;
  35. myque.push(E[i]);
  36. cursum += (ll)E[i].num * E[i].w; //累加当前树的总权重
  37. f = true;
  38. }
  39. }
  40. return f ? cnt : 1; //如果是叶子节点即返回1,否则返回给上一条边的经过次数
  41. }
  42. int main() {
  43. int q; scanf("%d", &q);
  44. while (q--) {
  45. scanf("%lld%lld", &n, &s); Init();
  46. for (int i = 1; i <= n-1; i++) {
  47. int u, v; ll w;
  48. scanf("%d%d%lld", &u, &v, &w);
  49. addEdge(u, v, w); addEdge(v, u, w);
  50. }
  51. dfs(1, 1); //对每一条边的出现次数及所有边的总权重作统计
  52. ll ans = 0;
  53. while (cursum > s) {
  54. Edge tmp = myque.top(); myque.pop();
  55. cursum -= tmp.w * tmp.num;
  56. tmp.w = (ll)(tmp.w / 2); //该边权值
  57. cursum += tmp.w * tmp.num;
  58. myque.push(tmp);
  59. ans++;
  60. }
  61. printf("%lld\n", ans);
  62. }
  63. return 0;
  64. }

Codeforces Round #661 (Div. 3) D、E1 题解的更多相关文章

  1. Codeforces Round #612 (Div. 2) 前四题题解

    这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...

  2. Codeforces Round #198 (Div. 2)A,B题解

    Codeforces Round #198 (Div. 2) 昨天看到奋斗群的群赛,好奇的去做了一下, 大概花了3个小时Ak,我大概可以退役了吧 那下面来稍微总结一下 A. The Wall Iahu ...

  3. Codeforces Round #672 (Div. 2) A - C1题解

    [Codeforces Round #672 (Div. 2) A - C1 ] 题目链接# A. Cubes Sorting 思路: " If Wheatley needs more th ...

  4. Codeforces Round #614 (Div. 2) A-E简要题解

    链接:https://codeforces.com/contest/1293 A. ConneR and the A.R.C. Markland-N 题意:略 思路:上下枚举1000次扫一遍,比较一下 ...

  5. Codeforces Round #610 (Div. 2) A-E简要题解

    contest链接: https://codeforces.com/contest/1282 A. Temporarily unavailable 题意: 给一个区间L,R通有网络,有个点x,在x+r ...

  6. Codeforces Round #611 (Div. 3) A-F简要题解

    contest链接:https://codeforces.com/contest/1283 A. Minutes Before the New Year 题意:给一个当前时间,输出离第二天差多少分钟 ...

  7. Codeforces Round #499 (Div. 2) D. Rocket题解

    题目: http://codeforces.com/contest/1011/problem/D This is an interactive problem. Natasha is going to ...

  8. Codeforces Round #499 (Div. 2) C Fly题解

    题目 http://codeforces.com/contest/1011/problem/C Natasha is going to fly on a rocket to Mars and retu ...

  9. Codeforces Round #198 (Div. 2)C,D题解

    接着是C,D的题解 C. Tourist Problem Iahub is a big fan of tourists. He wants to become a tourist himself, s ...

随机推荐

  1. 《JavaScript高级程序设计》——第三章 基本概念

    这章讲了JavaScript的语法.数据类型.流控制语句和函数.理解还是挺好理解的,但有很多和C.C++.Java不同的地方需要记忆.比如, JavaScript标识符可以由Unicode字符字符组成 ...

  2. 本地文件r如何上传到github上

    来源:http://www.cnblogs.com/shenchanghui/p/7184101.html 来源:http://blog.csdn.net/zamamiro/article/detai ...

  3. 获取url中查询字符串参数

    // 获取url中查询字符串参数 例如http://www.test.com?a=1&b=2 function RequestParamete() { var url = window.loc ...

  4. python数据分析使用matplotlib绘图

    matplotlib绘图 关注公众号"轻松学编程"了解更多. Series和DataFrame都有一个用于生成各类图表的plot方法.默认情况下,它们所生成的是线形图 %matpl ...

  5. Parameter 'name' implicitly has an 'any' type.

    出现在vue3版本 找到tsconfig.json文件 增加"noImplicitAny":flase,或把"strict":true改成"stric ...

  6. 【老孟Flutter】6种极大提升Flutter开发效率的工具包

    老孟导读:本文介绍6种极大提升Flutter开发效率的工具包. [1] 强大的日志软件包 在开发 Flutter 的过程中打印日志是常用的调试方式之一,但 Flutter 内置的日志打印非常简单,下面 ...

  7. 解决 cannot resolve 依赖包的问题

    在maven import的时候 报这样的错误 之前也经常碰到这样的错误,通过reimport.清缓存等方法都可以解决.但这次试了好多次都还是这样,查看maven后发现我pom文件里也没写错. 最后是 ...

  8. 第05组 Alpha冲刺 (2/6)

    .th1 { font-family: 黑体; font-size: 25px; color: rgba(0, 0, 255, 1) } #ka { margin-top: 50px } .aaa11 ...

  9. currentColor在CSS的含义

    currentColor属于CSS中的一个变量,当然也有人称之为CSS的自定义属性,其代表"当前颜色". 在CSS中设置了color的值后,相应CSS代码块(由两个花括号包裹的内容 ...

  10. LinkedHashMap实现 LRU

    一.leetcode 题目 运用你所掌握的数据结构,设计和实现一个  LRU (最近最少使用) 缓存机制 . 实现 LRUCache 类: LRUCache(int capacity) 以正整数作为容 ...