1790B - Taisia and Dice - CodeForces Solution


greedy greedy math

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
 
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define sz(x) (int)(x).size()
#define vt vector
#define pi pair<int,int>
#define ll long long

int caseNum = 1;
string piNum = "314159265358979323846264338327950288419716939937510";
void printCase(ll val) {
	cout << "Case #" << caseNum << ": " << val << "\n";
}
void solve() {
	int n,s,r;
	cin >> n >> s >> r;
	int m = s - r;
	//n integers, sum up to s and has max value of m
	vt<int> dice;
	for (int i = 0; i < n; i++) dice.pb(0);
	int curr = 0;
	int idx = 0;
	while(curr < s) {
		dice[idx % n]++;
		curr++;
		idx++;
	}
	sort(dice.begin(), dice.end());
	if(dice[n - 1] != m) {
		int need = m - dice[n-1];
		for (int i = n - 2; i >= 0; i--) {
			int max_give = dice[i] - 1;
			int give = min(need, max_give);
			dice[n-1] += give;
			dice[i] -= give;
			need -= give;
		}
	}
	for (int i = 0; i < n; i++) {
		cout << dice[i] << " ";
	}
	cout << "\n";
}
int main() {
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		solve();
		caseNum++;
	}
}


Comments

Submit
0 Comments
More Questions

479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality
1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD
149A - Business trip
34A - Reconnaissance 2
59A - Word
462B - Appleman and Card Game