1785A - Monsters (easy version) - CodeForces Solution


greedy sortings

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;

// clang-format off

#define           fi    first
#define           se    second
#define           pb    push_back
#define           ep    emplace
#define           eb    emplace_back
#define           lb    lower_bound
#define           ub    upper_bound
#define       all(x)    x.begin(), x.end()
#define      rall(x)    x.rbegin(), x.rend()
#define   uniquev(v)    sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define     mem(f,x)    memset(f , x , sizeof(f))
#define        sz(x)    (int32_t)(x).size()
#define  __lcm(a, b)    (1ll * ((a) / __gcd((a), (b))) * (b))
#define          mxx    *max_element
#define          mnn    *min_element
#define         left    Kurumi_Tokisaki
#define        right    Kei_Karuizawa
#define         next    Mai_Sakurajima
#define          div    Yume_Irido
#define         prev    Chizuru_Mizuhara
#define    cntbit(x)    __builtin_popcountll(x)
#define      MASK(x)    ( 1ll << (x) )
#define          Yes    cout << "Yes"
#define          YES    cout << "YES"
#define           No    cout << "No"
#define           NO    cout << "NO"
#define           AA    cout << "Alice"
#define           BB    cout << "Bob"

/// TASK
/// -----------------------------
#ifdef LMQZZZ
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << " ]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}

#define deb(x...) cerr << "[ in " <<__func__<< "() : line " <<__LINE__<< " ] : [ " << #x << " ] = [ "; _print(x); cerr << '\n';
#define TASK "C"
#else
#define deb(x...) 3326
#define TASK "C"
#endif
///------------------------------

void lmqzzz();
void init() {};
int32_t main() {
        ios_base::sync_with_stdio(0);
        cin.tie(0);      cout.tie(0);
        if (fopen(TASK ".inp", "r")) {
                freopen(TASK ".inp", "r", stdin);
                freopen(TASK ".out", "w", stdout);
        }
        /// =================================
                constexpr bool MULTITEST = 1;
        /// ================================
        init();
        int32_t TT = 1;
        if ( MULTITEST ) cin >> TT;
        for(int32_t TTT = 1; TTT <= TT; TTT ++) {
                lmqzzz();
                cout << '\n';
        }
}

template <class T> inline T min(const T &a, const T &b, const T &c) { return min(a, min(b, c)); }
template <class T> inline T max(const T &a, const T &b, const T &c) { return max(a, max(b, c)); }
template <class T, class U> inline bool mini(T &a, const U &b) { if (a > b) { a = b; return 1; } return 0; }
template <class T, class U> inline bool maxi(T &a, const U &b) { if (a < b) { a = b; return 1; } return 0; }

constexpr     int16_t dr[]  =  {0, 0, -1, 1};
constexpr     int16_t dc[]  =  {1, -1, 0, 0};
constexpr      int64_t MOD  =  1e9 + 7;
constexpr     int32_t MAXN  =  1e6 + 10;

// clang-format on

int a[MAXN];
int64_t n, m, k;

void lmqzzz() {
        cin >> n;
        for (int i = 1; i <= n; i++) cin >> a[i];
        int64_t res = 0;
        sort(a + 1, a + n + 1);
        int64_t sum = accumulate(a + 1, a + n + 1, 0ll);
        int last = 0;
        for (int i = 1; i <= n; i++) {
                int na = min(a[i], last + 1);
                res += a[i] - na;
                last = na;
        }
        cout << res;
        // for (int i = 1; i <= n; i++) {
        //         int64_t tmp = sum - a[i];
        //         int cnt = 1;
        //         while (i < n && abs(a[i + 1] - a[i]) <= 1) {
        //                 cnt += (a[i + 1] > a[i]);
        //                 i++;
        //                 tmp -= a[i];
        //         }
        //         deb(i, tmp, cnt);
        //         res = min(res, tmp - 1ll * cnt * (n - i));
        // }
        // cout << res;
}


Comments

Submit
0 Comments
More Questions

535B - Tavas and SaDDas
722C - Destroying Array
366A - Dima and Guards
716B - Complete the Word
1461C - Random Events
1627A - Not Shading
141B - Hopscotch
47B - Coins
1466C - Canine poetry
74A - Room Leader
1333D - Challenges in school №41
1475B - New Year's Number
461A - Appleman and Toastman
320B - Ping-Pong (Easy Version)
948A - Protect Sheep
387A - George and Sleep
53A - Autocomplete
1729G - Cut Substrings
805B - 3-palindrome
805C - Find Amir
676C - Vasya and String
1042B - Vitamins
1729F - Kirei and the Linear Function
25D - Roads not only in Berland
1694A - Creep
659F - Polycarp and Hay
1040A - Palindrome Dance
372A - Counting Kangaroos is Fun
1396B - Stoned Game
16A - Flag