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. 3:3 OGNL 表达式一

    一: 用例 (直接链式访问属性名,其实内部还是的调用set,get方法实现数据的流动); 二: 注意:表达式里面是没有方法的,只能点属性, 访问列表: (访问的时候加上#,表示访问非值栈的内容.) 访 ...

  2. 排序的hashmap(guava)

    1.mvnrepository上搜索 guava.并引用其jar包 类似compile "com.google.guava:guava:18.0" 测试代码 Builder< ...

  3. 神经网络 java包

    java神经网络组件Joone.Encog和Neuroph https://github.com/deeplearning4j/deeplearning4j http://muchong.com/ht ...

  4. Linux服务器---ssh登录

    Ssh登录     Ssh是建立在应用层和传输层的安全协议,专门为远程登录回话和其他网络服务提供安全性.利用ssh可以有效的防止远程管理中的信息泄露问题,同时ssh传输的数据是经过压缩的,可以加快传输 ...

  5. MySQL数据库读写分离、读负载均衡方案选择

    MySQL数据库读写分离.读负载均衡方案选择 一.MySQL Cluster外键所关联的记录在别的分片节点中性能很差对需要进行分片的表需要修改引擎Innodb为NDB因此MySQL Cluster不适 ...

  6. JProfiler8 远程监控tomcat配置过程

    1. 阅读人群 1.熟悉liunx服务器,起码知道liunx常见的命令 2.熟悉tomcat容器,起码知道怎么tomcat的启动以及停止 3.熟悉java编程语言,JProfiler8是专门监控jav ...

  7. Android 实践项目开发一

    这次做的项目是—手机地图系统 本次实验的功能是,为用户提供需要的目标定位系统定位处理,即用户设置一个目标后, 可以在后台启动一个Service,能够定时读取GPS数据已获得用户当前所在的位置信息, 并 ...

  8. cogs 444. [HAOI2010]软件安装

    ★★☆   输入文件:install.in   输出文件:install.out   简单对比 时间限制:1 s   内存限制:128 MB [问题描述]现在我们的手头有N个软件,对于一个软件i,它要 ...

  9. Django组件(三) Django之中间件

    中间件概述 中间件顾名思义,是介于request与response处理之间的一道处理过程,相对比较轻量级,并且在全局上改变django的输入与输出.因为改变的是全局,所以需要谨慎实用,用不好会影响到性 ...

  10. JavaScript 题目

    1. ],b=a; b[]=; console.log(a+b); a=[], b=a, b=[]; console.log(a+b); 2.快速排序法 var quickSort = functio ...