260D - Black and White Tree

思路:把两种颜色先按值sort一下,最小值肯定是叶子,然后把这个叶子和另外一中颜色的一个最小值的节点连接起来,再把这个节点变成叶子,把值减掉就可以了。

如下图:

代码1

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+;
struct node
{
int val,id;
bool operator <(node &a)
{
return val<a.val;
}
}a[N],b[N];
int main()
{
int n;
int c1=,c2=;
int col,val;
cin>>n;
for(int i=;i<=n;i++)
{
cin>>col>>val;
if(col)
{
b[c2].id=i;
b[c2++].val=val;
}
else
{
a[c1].id=i;
a[c1++].val=val;
}
}
sort(a,a+c1);
sort(b,b+c2);
int l1=,l2=;
int id0,id1;
while(l1<c1&&l2<c2)
{
id0=a[l1].id;
id1=b[l2].id;
if(a[l1].val<b[l2].val)
{
cout<<a[l1].id<<' '<<b[l2].id<<' '<<a[l1].val<<endl;
b[l2].val-=a[l1].val;
l1++;
if(l1>=c1)l2++;//如果其中一种颜色没了,那么最后一个连的另外一种颜色的节点就没有节点连了(也就是叶子结点)
}
else
{
cout<<a[l1].id<<' '<<b[l2].id<<' '<<b[l2].val<<endl;
a[l1].val-=b[l2].val;
l2++;
if(l2>=c2)l1++;
}
}
while(l1<c1)//所有剩下的节点和最后一个另外一种颜色连
{
cout<<id1<<' '<<a[l1].id<<' '<<a[l1].val<<endl;
l1++;
}
while(l2<c2)
{
cout<<id0<<' '<<b[l2].id<<' '<<b[l2].val<<endl;
l2++;
}
return ;
}

代码2(写残版):

我居然用了优先队列,患上STL综合症的我脑残了。

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+;
struct node
{
int col;
int val;
int id;
friend bool operator>(node a,node b)
{
return a.val>b.val;
}
}a[N];
int main()
{
int n;
priority_queue<node,vector<node>,greater<node> >q0,q1;
cin>>n;
for(int i=;i<=n;i++)
{
cin>>a[i].col>>a[i].val;
a[i].id=i;
if(a[i].col)q1.push(a[i]);
else q0.push(a[i]);
}
int id0,id1;
while(q0.size()&&q1.size())
{
node temp0=q0.top();
node temp1=q1.top();
id0=temp0.id;
id1=temp1.id;
if(temp0.val<temp1.val)
{
q0.pop();
q1.pop();
cout<<temp0.id<<' '<<temp1.id<<' '<<temp0.val<<endl;
temp1.val-=temp0.val;
if(q0.size())q1.push(temp1);
}
else
{
q0.pop();
q1.pop();
cout<<temp0.id<<' '<<temp1.id<<' '<<temp1.val<<endl;
temp0.val-=temp1.val;
if(q1.size())q0.push(temp0);
}
}
while(q0.size())
{
node temp=q0.top();
q0.pop();
cout<<temp.id<<' '<<id1<<' '<<temp.val<<endl;
}
while(q1.size())
{
node temp=q1.top();
q1.pop();
cout<<temp.id<<' '<<id0<<' '<<temp.val<<endl;
}
return ;
}

Codeforces 260D - Black and White Tree的更多相关文章

  1. codeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(启发式合并)

    codeforces 741D Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...

  2. codeforces 812E Sagheer and Apple Tree(思维、nim博弈)

    codeforces 812E Sagheer and Apple Tree 题意 一棵带点权有根树,保证所有叶子节点到根的距离同奇偶. 每次可以选择一个点,把它的点权删除x,它的某个儿子的点权增加x ...

  3. codeforces 220 C. Game on Tree

    题目链接 codeforces 220 C. Game on Tree 题解 对于 1节点一定要选的 发现对于每个节点,被覆盖切选中其节点的概率为祖先个数分之一,也就是深度分之一 代码 #includ ...

  4. AtCoder 2376 Black and White Tree

    D - Black and White Tree Time limit : 2sec / Memory limit : 256MB Score : 900 points Problem Stateme ...

  5. HDU 5905 Black White Tree(树型DP)

    题目链接  Black White Tree 树型DP,设$f[i][j]$为以$i$为根的子树中大小为$j$的连通块中可以包含的最小黑点数目. $g[i][j]$为以$i$为根的子树中大小为$j$的 ...

  6. 2017国家集训队作业[agc014d]Black and White Tree

    2017国家集训队作业[agc014d]Black and White Tree 题意: ​ 有一颗n个点的树,刚开始每个点都没有颜色.Alice和Bob会轮流对这棵树的一个点涂色,Alice涂白,B ...

  7. Codeforces E. Alyona and a tree(二分树上差分)

    题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  8. 【27.91%】【codeforces 734E】Anton and Tree

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. Codeforces 379 F. New Year Tree

    \(>Codeforces \space 379 F. New Year Tree<\) 题目大意 : 有一棵有 \(4\) 个节点个树,有连边 \((1,2) (1,3) (1,4)\) ...

随机推荐

  1. ShuffleElements(随机打乱数组中的元素)

    给定一个数组,随机打乱数组中的元素,题意很简单直接上代码: package Array; import java.util.Arrays; import java.util.Collections; ...

  2. 019-centos的yum用法

    1.检测系统是否已经安装过mysql或其依赖:# yum list installed | grep mysql(当然也可以用 rpm -qa | grep mysql) 2.卸载已经存在的mysql ...

  3. VS2010/MFC编程入门之三十九(文档、视图和框架:概述)

    前面几节讲了菜单.工具栏和状态栏的使用,鸡啄米本节开始将为大家讲解文档.视图和框架的知识. 文档.视图和框架简介 在VS2010/MFC编程入门之三十四(菜单:VS2010菜单资源详解)创建的单文档工 ...

  4. Codeforces Round #247 (Div. 2) C D

    这题是一个背包问题 这样的 在一个k子树上 每个节点都有自己的k个孩子 然后 从原点走 走到 某个点的 和为 N 且每条的 长度不小于D 就暂停问这样的 路有多少条,  呵呵 想到了 这样做没有把他敲 ...

  5. Lombok让pojo变得更优雅

    Lombok 采取注解的形式,标记在pojo上面,在编译后,自动生成相应的方法,像get.set.构造方法等都可以注解一键生成. 引入jar包: <dependency> <grou ...

  6. Linux 系统版本查询

    显示Linux版本信息 输入"cat /proc/version",说明正在运行的内核版本. 输入"cat /etc/issue", 显示的是发行版本信息. 输 ...

  7. mac OSX 实用快捷键

    Command + shift + G. 前往文件夹 按键 效果 Shift + option + 音量+/- 以四分之一的刻度加 / 减音量 Shift. + option + 9 ······

  8. UVa 1471 Defense Lines - 线段树 - 离散化

    题意是说给一个序列,删掉其中一段连续的子序列(貌似可以为空),使得新的序列中最长的连续递增子序列最长. 网上似乎最多的做法是二分查找优化,然而不会,只会值域线段树和离散化... 先预处理出所有的点所能 ...

  9. React 回忆录(三)使用 React 渲染界面

    Hi 各位,欢迎来到 React 回忆录!

  10. C语言宏定义中的#和##的作用【转】

    本文转载自:http://my.oschina.net/shelllife/blog/123202 在宏定义中#和##的作用是:前者将宏定义的变量转化为字符串:后者将其前后的两个宏定义中的两个变量无缝 ...