Blog-o de Ganso
Publicado en

10%

Kolya and Tanya

Source

A very interesting counting problem. There are 33n3^{3n} possible ways to distribute coins, and 7 ways for each group to have 6 coins in total.

n = int(input())
print((27 ** n - 7 ** n) % (10 ** 9 + 7))

Towers

Source

Greedily remove a block from the highest tower and add it to the shortest one until there are no more operations available.

n, k = map(int, input().split())
ops = 0
height = list(map(int, input().split()))
ans = []

while(ops < k):
    pos_lo, lo = min(enumerate(height), key=lambda x: x[1])
    pos_hi, hi = max(enumerate(height), key=lambda x: x[1])
    if(hi == lo):
        break
    height[pos_lo] += 1
    height[pos_hi] -= 1
    ans.append((pos_hi + 1, pos_lo + 1))
    ops += 1
print(max(height) - min(height), ops)
print('\n'.join(str(x) + ' ' + str(y) for (x, y) in ans))

Cdgame

Source

Test all possibilities in O(len(a) * len(b))

class Cdgame:
    def rescount(self, a, b):
        sa = sum(a)
        sb = sum(b)
        s = set()
        for i in a:
            for j in b:
                s.add((sa-i+j) * (sb-j+i))
        return len(s)

Drbalance

Source

The best way to add a new '+' is to add it on the left side of the string. Add new '+' signs to the string and calculate its negativity until it reaches a value less than or equal to k.

class Drbalance:
    def lesscng(self, s, k):
        s = list(s)

        def neg(s):
            n = 0
            for i in range(1, len(s)+1):
                n += s[:i].count('+') < s[:i].count('-')
            return n
        ans = 0
        while(neg(s) > k):
            s[s.index('-')] = '+'
            ans += 1
        return ans

LiveConcert

Source

For each idol get the song with the max happiness value and add them all.

#include <bits/stdc++.h>

using namespace std;

class LiveConcert {
public:
  int maxHappiness(vector <int> h, vector <string> s) {
    map<string,int> song;
    int n = s.size();
    for(int i = 0; i < n; i++){
      if(song.find(s[i]) != song.end())
        song[s[i]] = h[i] > song[s[i]] ? h[i] : song[s[i]];
      else song[s[i]] = h[i];
    }
    int ans = 0;
    for(map<string,int> :: iterator it = song.begin(); it != song.end(); it++)
      ans += it -> second;
    return ans;
  }
};

CombiningSlimes

Source

It's not hard to prove that the order in which the operations are performed does not change the final result.

#include <bits/stdc++.h>

using namespace std;

class CombiningSlimes {
public:
  int maxMascots(vector <int> a) {
    int ans = 0;
    while(a.size() > 1){
      int x = a.back(); a.pop_back();
      int y = a.back(); a.pop_back();
      a.push_back(x + y);
      ans += x*y;
    }
    return ans;
  }
};

Conclusion

I found out that using Atom plus a terminal visor (iTerm, TotalTerminal) is a good enough way of writing solutions in python and c++, we'll see how that goes during a competition.