Codeforces 260D - Black and White Tree

http://codeforces.com/problemset/problem/260/D

题意: 有n个点的一个树,同一条边的两个点涂成不同的颜色(black & white),每条边有一个权值,题目给出n , 和每个点的颜色 和 与每个点相连的边的权值和。

求出n-1条边 的端点和权值。

n的 大小  10e5.  这有是一个构造的 题目。

由于题目说明 肯定可以构造出来, 所以,白色点的权值和的 总和 == 黑色点的权值和的 总和 。

可以分别把白色点 和黑色点  按照 权值和大小 排序。 然后分别从 两堆点中,取出最小两个点,这两点连边,权值为 两者的min, 之后两点的权值和分别减去 这个min,把权值为0的点delete, 直到处理完 n-1个点。 (注意,如果取出两个点 的权值都为0 的情况)

code: 

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
#define INF 1000000000
#define N 100005
int ad1,ad2,n;
struct node
{
	int su,id;
};
struct node a[N],b[N];
bool cmp(struct node x,struct node y)
{
	return x.su<y.su;
}
int main()
{
	scanf("%d",&n);
	int col,su;
	ad1=ad2=0;
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d",&col,&su);
		if(col)
		{
			a[++ad1].su=su;
			a[ad1].id=i;
		}
		else
		{
			b[++ad2].su=su;
			b[ad2].id=i;
		}
	}
	sort(a+1,a+1+ad1,cmp);
	sort(b+1,b+1+ad2,cmp);
	for(int i=1,j=1;i<=ad1 && j<=ad2;)
	{
		int tmp=min(a[i].su,b[j].su);
		printf("%d %d %d\n",a[i].id,b[j].id,tmp);
		a[i].su-=tmp; b[j].su-=tmp;
		if(a[i].su) j++;
		else if(b[j].su) i++;
		else if(i<ad1) i++;
		else j++;
	}

	return 0;
}


### Codeforces Problem 976C Solution in Python For solving problem 976C on Codeforces using Python, efficiency becomes a critical factor due to strict time limits aimed at distinguishing between efficient and less efficient solutions[^1]. Given these constraints, it is advisable to focus on optimizing algorithms and choosing appropriate data structures. The provided code snippet offers insight into handling string manipulation problems efficiently by customizing comparison logic for sorting elements based on specific criteria[^2]. However, for addressing problem 976C specifically, which involves determining the winner ('A' or 'B') based on frequency counts within given inputs, one can adapt similar principles of optimization but tailored towards counting occurrences directly as shown below: ```python from collections import Counter def determine_winner(): for _ in range(int(input())): count_map = Counter(input().strip()) result = "A" if count_map['A'] > count_map['B'] else "B" print(result) determine_winner() ``` This approach leverages `Counter` from Python’s built-in `collections` module to quickly tally up instances of 'A' versus 'B'. By iterating over multiple test cases through a loop defined by user input, this method ensures that comparisons are made accurately while maintaining performance standards required under tight computational resources[^3]. To further enhance execution speed when working with Python, consider submitting codes via platforms like PyPy instead of traditional interpreters whenever possible since they offer better runtime efficiencies especially important during competitive programming contests where milliseconds matter significantly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值