관리 메뉴

너와 나의 스토리

Codeforces Round #565 (Div. 3) 본문

Algorithm/Codeforces

Codeforces Round #565 (Div. 3)

노는게제일좋아! 2019. 7. 16. 00:07
반응형

A - Divide it!

int tc;
long long n;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL), cout.tie(NULL);

	cin >> tc;
	while (tc--) {
		cin >> n;
		long long cnt = 0;
		bool frag = true;
		while (n > 1) {
			if (n % 2 == 0) n /= 2;
			else if (n % 3 == 0) n=n/3*2;
			else if (n % 5 == 0) n=n/5*4;
			else {
				frag = false;
				break;
			}
			cnt++;
		}
		if (!frag) cout << "-1\n";
		else cout << cnt << '\n';
	}
	return 0;
}

 

B - Merge it!

입력을 3의 나머지로만 연산

(1,2) (1,1,1) (2,2,2)일 때 3의 배수로 만들 수 있음

int tc,n;
 
int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL), cout.tie(NULL);
 
	cin >> tc;
	while (tc--) {
		cin >> n;
		int arr[3] = { 0,0,0 };
		int cnt = 0;
		for (int i = 0; i < n; i++){
			int q;
			cin >> q;
			arr[q % 3]++;
		}
		cnt += arr[0];
		if (arr[1] < arr[2]) {
			cnt += arr[1];
			cnt += (arr[2] - arr[1]) / 3;
		}
		else {
			cnt += arr[2];
			cnt += (arr[1] - arr[2]) / 3;
		}
		cout << cnt << '\n';
	}
	return 0;
}

 

C - Lose it!

int arr[7] = {0,4,8,15,16,23,42}, n, pos;
map<int,int> s;
 
int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL), cout.tie(NULL);
 
	cin >> n;
	int cnt = 0;
	map<int,int> m;
	for (int i = 0; i < 7; i++) {
		m[arr[i]] = i;
	}
	s[0] = 500000;
	for (int i = 0; i < n; i++) {
		int w;
		cin >> w;
		if (w == 42) {
			if (s[m[w] - 1] == 0) cnt++;
			else s[m[w] - 1]--;
			continue;
		}
		if (s[m[w] - 1] == 0) cnt++;
		else{
			s[m[w]-1]--;
			s[m[w]]++;
		}
		
	}
	int t = 0;
	for (auto i : s) {
		cnt += i.first*(i.second);
	}
	cout << cnt;
	return 0;
}

 

반응형
Comments