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. Js中split()方法的正确使用

    通过 js 获取 QueryString (location.search部分) 参数很常见,网上代码也满天飞.不过现在的框架,基本上都通过路由伪静态了,把以前的 QueryString 变成了pat ...

  2. VUE路由去除#问题

    最近自己在写一个vue的小型管理系统,在浏览器中看到的路由都是带有#的,很是不好看.为了解决此问题,大家一般都会想到:mode: 'history'.可是在开发阶段没有问题,但是一旦build打包后, ...

  3. jquery控制css的display属性(显示与隐藏)

    jquery控制div的显示与隐藏,很方便的. 例如: $("#id").show()表示display:block, $("#id").hide()表示dis ...

  4. python爬虫对于gb2312

    对于刚刚接触python爬虫的人,常常会碰到一个比较烦的问题, 如果网页是GB2312编码格式,我们直接decode(’GB2312‘)一般python都会报错: GB2312不能编码该页面. 这就比 ...

  5. http://xx.xxx.xxx.xx:8080/把路径设置成http服务访问的形式

    1.官网下载python安装包(eg:python-3.6.3-embed-win32),并解压文件 2.配置环境变量 3.cmd里查看python版本并设置服务路径 4. 访问查看

  6. IO(字符流)

        1. 由于Java采用16位的Unicode字符,因此需要基于字符的输入/输出操作.从Java1.1版开始,加入了专门处理字符流的抽象类Reader和Writer,前者用于处理输入,后者用于处 ...

  7. mysql日志文件目录

    默认情况下mysql的二进制日志文件保存在默认的数据目录data下,如:/usr/local/mysql/data 修改日志保存目录(/backup/mysqlbinlog/mysql-bin)的话: ...

  8. PHP二维数组排序(感谢滔哥lvtao.net)

    滔哥原创 /* _ooOoo_ o8888888o 88" . "88 (| -_- |) O\ = /O ____/`---'\____ .' \\| |// `. / \\|| ...

  9. 使整个页面变灰的css代码

    * { filter:progid:DXImageTransform.Microsoft.BasicImage(grayscale=); -webkit-filter: grayscale(%); - ...

  10. C++设计模式 之 “组件协作”模式:Template Method、Strategy、Observer

    “组件协作”模式: #现代软件专业分工之后的第一个结果是“框架与应用程序的划分”,“组件协作”模式通过晚期绑定,来实现框架与应用程序之间的松耦合,是二者之间协作时常用的模式. #典型模式: Templ ...