Codeforces 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的更多相关文章
- 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 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...
- codeforces 812E Sagheer and Apple Tree(思维、nim博弈)
codeforces 812E Sagheer and Apple Tree 题意 一棵带点权有根树,保证所有叶子节点到根的距离同奇偶. 每次可以选择一个点,把它的点权删除x,它的某个儿子的点权增加x ...
- codeforces 220 C. Game on Tree
题目链接 codeforces 220 C. Game on Tree 题解 对于 1节点一定要选的 发现对于每个节点,被覆盖切选中其节点的概率为祖先个数分之一,也就是深度分之一 代码 #includ ...
- AtCoder 2376 Black and White Tree
D - Black and White Tree Time limit : 2sec / Memory limit : 256MB Score : 900 points Problem Stateme ...
- HDU 5905 Black White Tree(树型DP)
题目链接 Black White Tree 树型DP,设$f[i][j]$为以$i$为根的子树中大小为$j$的连通块中可以包含的最小黑点数目. $g[i][j]$为以$i$为根的子树中大小为$j$的 ...
- 2017国家集训队作业[agc014d]Black and White Tree
2017国家集训队作业[agc014d]Black and White Tree 题意: 有一颗n个点的树,刚开始每个点都没有颜色.Alice和Bob会轮流对这棵树的一个点涂色,Alice涂白,B ...
- Codeforces E. Alyona and a tree(二分树上差分)
题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- 【27.91%】【codeforces 734E】Anton and Tree
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces 379 F. New Year Tree
\(>Codeforces \space 379 F. New Year Tree<\) 题目大意 : 有一棵有 \(4\) 个节点个树,有连边 \((1,2) (1,3) (1,4)\) ...
随机推荐
- Problem B. Full Binary Tree
题目 链接:http://code.google.com/codejam/contest/2984486/dashboard#s=p1 googlde code jam 2014 Round1A 解题 ...
- 圆的k次面积并
搬运别人的 https://vjudge.net/problem/SPOJ-CIRUT //china no.1 #pragma comment(linker, "/STACK:102400 ...
- linux服务器---安装swat
安装swat swat是一个图形化的samba管理软件,可以帮助不熟悉的人去灵活的配置samba服务, 1.安装swat [root@localhost wj]#yum install -y samb ...
- Python3 Selenium自动化-select下拉框
Python3 Selenium自动化-select下拉框 selenium介绍select下拉框相关的操作方法:
- java service wrapper日志参数设置及优化
一般在容器比如tomcat/weblogic中运行时,我们都是通过log4j控制日志输出的,因为我们现在很多服务端使用java service wrapper(至于为什么使用jsw,原先是比较排斥使用 ...
- dom4j解析xml报"文档中根元素后面的标记格式必须正确"
今天,在写个批量启动报盘机的自动化应用,为了简化起见,将配置信息存储在xml中,格式如下: <?xml version="1.0" encoding="UTF-8& ...
- troubleshooting-执行导数shell脚本抛异常error=2, No such file or directory
Cannot run program "order_log.sh" (in directory "/data/yarn/nm/usercache/chenweidong/ ...
- xdebug安装方法
打开网址:https://xdebug.org/wizard.php 把phpinfo页面中输出的所有内容复制过来,粘贴在此处点下面那个按钮,系统会分析出你需要下载哪个版本的x-debug,还会告诉你 ...
- 20145225唐振遠《网络对抗》Exp5 MSF基础应用
基础问题回答 用自己的话解释什么是exploit,payload,encode? exploit就相当于是载具,将真正要负责攻击的代码传送到靶机中,我觉得老师上课举的火箭和卫星的例子非常形象,火箭只是 ...
- 微信小程序——2、配置json文件
配置文件详解 主配置文件app.json 主配置文件位于主目录中,用于进行全局配置.包括页面文件的路径.窗口表现.设置网络超时时间.设置多tab等 下面通过微信最初自带小程序来学习 { "p ...