260C - Balls and Boxes - CodeForces Solution


constructive algorithms greedy implementation *1700

Please click on ads to support us..

Python Code:

n,k = map(int, input().split())
arr = list(map(int, input().split()))
m = min(arr)
k -= 1
ov = m*n
for i in range(n):
    arr[i] -= m
while arr[k] > 0:
    ov += 1
    arr[k] -= 1
    k = (k-1)%n
arr[k] = ov
print(*arr) 

C++ Code:

#include <bits/stdc++.h>

using namespace std;

#define ll  long long
#define endl '\n'
#define cin_2d(vec, n, m) for(int i = 0; i < n; i++) for(int j = 0; j < m && cin >> vec[i][j]; j++);
#define fixed(n) fixed << setprecision(n)
#define cout_2d(vec, n, m) for(int i = 0; i < n; i++, cout << "\n") for(int j = 0; j < m && cout << vec[i][j] ; j++);
#define  loop(n)     for(int i=0; i<n; ++i)
#define  loopj(n)     for(int j=0; j<n; ++j)
#define  trav(a, b)   for(auto &a:b)
#define  YES         cout << "YES"<<endl;
#define  NO          cout << "NO" << endl;
#define yn cout<<(flag1?"YES":"NO")<<endl;
#define mem(a, b) memset(a , b , sizeof a);
#define FO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define sz(s) (int)s.size()
#define all(vec) vec.begin(), vec.end()
#define debug(x) cout << #x << ": " << (x) << "\n";
#define debugLine(x) cout << #x << ": " << (x) << " ";
const ll OO = 0x3f3f3f3f, mod = 1e9 + 7, N = 1e3 + 5;
ll n, m, k, l, r;
bool flag1;


void _go() {
    cin >> n >> k;
    k--;
    vector<ll> v(n);
    loop(n)cin >> v[i];

    int val = *min_element(all(v));
    ll sum = 0;
    loop(n) {
        v[i] -= val;
        sum += val;
    }
    while (v[k])
    {
        v[k]--;
        k=(k-1+n)%n;
        sum++;
    }

    v[(k+n)%n]+=sum;
    loop(n)cout<<v[i]<<" ";
    cout<<endl;


}

int main() {
    //FO
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    int t = 1;
    //cin >> t;
    while (t--) {
        _go();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST