Skip to content

Instantly share code, notes, and snippets.

@fpdjsns
Last active January 21, 2019 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fpdjsns/29a8c22f51a04d7331d326cd68701aaa to your computer and use it in GitHub Desktop.
Save fpdjsns/29a8c22f51a04d7331d326cd68701aaa to your computer and use it in GitHub Desktop.
[Qualification Round 2018] Saving The Universe Again : https://codejam.withgoogle.com/2018/challenges/00000000000000cb/dashboard
//
// Created by fpdjsns on 2019. 1. 21
// Copyright © 2019 fpdjsns. All rights reserved.
//
/*
* 시간복잡도 : O(T * |P|^2)
* 공간복잡도 : O(|P|)
*/
#include<iostream>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
int main() {
int T;
cin >> T;
for (int t = 1; t <= T; t++) {
int D;
cin >> D;
string P;
cin >> P;
int sCnt = 0;
long long int power = 1;
long long int damage = 0;
map<long long int, int> m; // (damage, cnt)
long long int maxShootDamage = 1;
for (int i = 0; i < P.size(); i++) {
if (P[i] == 'S') { // S
sCnt++;
damage += power;
maxShootDamage = max(maxShootDamage, power);
m[power]++;
}
else { // C
power *= 2;
}
}
printf("Case #%d: ", t);
if (sCnt > D) {
printf("IMPOSSIBLE\n");
continue;
}
int hackCnt = 0;
while (D < damage) {
hackCnt++;
damage -= maxShootDamage / 2;
m[maxShootDamage]--;
m[maxShootDamage / 2]++;
if (m[maxShootDamage] == 0) {
maxShootDamage /= 2;
}
}
printf("%d\n", hackCnt);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment