D. Decreasing Debts

tags: cf brush questions

Topic 1

Title:

     gives the debt relationship of n individuals, and asks to simplify the debt relationship. For example, a owes b10 yuan and b owes c10 yuan, so you can directly let a owe c10 yuan.

analysis:

    We think about the debt relationship after the final simplification, for everyone, either owes money to others or others owe him money. Because if it exists at the same time, you can offset the borrowed money with the money you owe. So we only need to pay attention to everyone's final money and match.

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

typedef long long ll; 

ll debt[100005];
vector<ll> ans1,ans2,ans3;

struct node{
	ll id,val;
	node(ll a,ll b)
	{
		id = a;
		val = b;
	}
};

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	queue<node> q1,q2;
	int n,m;
	cin >> n >> m;
	for (int i = 1; i <= m; i++)
	{
		ll x,y,v;
		cin >> x >> y >> v;
		debt[x] += v;
		debt[y] -= v;
	}
	for (int i = 1; i <= n; i++)
	{
		if( debt[i] > 0 ) q1.push(node(i,debt[i]));
		else if( debt[i] < 0 ) q2.push(node(i,-debt[i]));
	}
	ll t1 = 0,t2 = 0;
	int id1,id2;
	while( !q1.empty() || t1 != 0 )
	{
		if( t1 == 0 )
		{
 			t1 = q1.front().val;
			id1 = q1.front().id;
			q1.pop(); 
		}
		if( t2 == 0 )
		{
			t2 = q2.front().val;
			id2 = q2.front().id;
			q2.pop();
		}
		ans1.push_back(id1);
		ans2.push_back(id2);
		ll z = min(t1,t2);
		ans3.push_back(z);
		t1 -= z;
		t2 -= z;
	}
	cout << ans1.size() << '\n';
	for (int i = 0; i < ans1.size(); i++)
	{
		cout << ans1[i] << ' ' << ans2[i] << ' ' << ans3[i] << '\n';
	}
	return 0;
}

Intelligent Recommendation

Decreasing String

Problem F: Decreasing String Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 81  Solved: 28 [Submit][Status][Web Board] Description You need to find a string which has exactly K po...

Increasing and Decreasing

Increasing and Decreasing Time Limit: 3000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 508    Accepted Submission(...

Incremental and decreasing

Increment ++ A and A ++ are incremented by A the difference ++ A first returns the value of A after increment A ++ X first returns the original value of A, then return the value after increment 2. Dec...

UE4 screen a dark debts canceled automatic exposure

First, cancel the automatic exposure of automatic exposure in project settings Edit -> Project Setting -> Engine (Engine) -> Rendering (Rendering) -> Default Settings -> Cancel Check Au...

2018 National Multi-School Algorithm Winter Holiday Training Camp Practice Competition (Second Game) D YB wants to play Hearthstone [The longest non-decreasing subsequence]

Title description Wozuinb likes to play Hearthstone very much, but the food is not good, so he decided to play to practice hands in the arena. The system gives n cards in order, and each card has its ...

More Recommendation

Leetcode665.Non-decreasing Array non-decreasing array

Given an array of integers of length n, your task is to determine whether the array can become a non-decreasing sequence if at most 1 element is changed. We define a non-decreasing sequence like this:...

665. Non-decreasing Array an array of non-decreasing

Given an array with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element. We define an array is non-decreasing if array[i] &...

Leetcode665.Non-decreasing Array an array of non-decreasing

Given a length of the array of integers n, your task is determined in the case of changing up to an element of the array can become a non-decreasing number of columns. We thus defined is a non-decreas...

Non-decreasing Array【Non-decreasing array】

PROBLEM: Given an array with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element. We define an array is non-decreasing if a...

Application of the decreasing function

In the case of normal traversal of the array, the length caused by the deletion of its own elements is insufficient, affecting the number of loops solution: uses its own length to decrement to 0, does...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top