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)\) ...
随机推荐
- winfrom 下 listbox 选项添加value
public struct MyItem { public string Name; public string Tag; public override string ToString() { re ...
- Understanding Convolutional Neural Networks for NLP
When we hear about Convolutional Neural Network (CNNs), we typically think of Computer Vision. CNNs ...
- Python 迭代器切片
函数itertools.islice() 正好适用于在迭代器和生成器上做切片操作 >>> def count(n): ... while True: ... yield n ... ...
- svn导出文件夹到另外目录export
svn导出文件夹到另外目录export 2.选择目录,下面两个选项不用勾选 3.有存在的文件选择overwrite覆盖,勾选下面的同样操作
- C++提供了四个转换运算符
const_cast <new_type> (expression) static_cast <new_type> (expression) reinterpret_cast ...
- C++算法原理与实践(面试中的算法和准备过程)
第0部分 简介 1. 举个例子:面试的时候,可能会出一道算法考试题,比如写一个 strstr 函数——字符串匹配. 可能会想到用KMP算法来解题,但是该算法很复杂,不适宜在面试中使用. 1.1 C++ ...
- "/var/lib/mysql/mysql.sock"不存在解决办法
今天再次遇到mysql.sock问题, 下面是我的三种解决方案. 解决办法: 1./etc/my.cnf,至少增加/修改一行(前提是您find到了这个mysql.sock是在tmp下) [mysql] ...
- Python3基础 __len__,__getitem__ 记录列表中元素访问的次数 定制不可变序列,下标字典
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- DD-WRT自定义脚本更新花生壳DDNS
N年以前买了一个tp-link 841n v7,一直用的还算可以吧,除了不定期重启路由器,不然网速慢的龟爬啊!这也是TP原厂固件的通病,于是刷了DD-WRT,话说DD确实很爽,除了功能强大之外,而且很 ...
- 小朋友排队|2014年蓝桥杯B组题解析第十题-fishers
小朋友排队 n 个小朋友站成一排.现在要把他们按身高从低到高的顺序排列,但是每次只能交换位置相邻的两个小朋友. 每个小朋友都有一个不高兴的程度.开始的时候,所有小朋友的不高兴程度都是0. 如果某个小朋 ...