[λ°±μ€ 1011] Fly me to the Alpha Centauri (C++)
https://www.acmicpc.net/problem/1011
1011λ²: Fly me to the Alpha Centauri
μ°νμ΄λ μ΄λ¦° μμ , μ§κ΅¬ μΈμ λ€λ₯Έ νμ±μμλ μΈλ₯λ€μ΄ μ΄μκ° μ μλ λ―Έλκ° μ€λ¦¬λΌ λ―Ώμλ€. κ·Έλ¦¬κ³ κ·Έκ° μ§κ΅¬λΌλ μΈμμ λ°μ λ΄λ € λμ μ§ 23λ μ΄ μ§λ μ§κΈ, μΈκ³ μ΅μ°μ ASNA μ°μ£Ό λΉν
www.acmicpc.net
μμ΄λμ΄
λ§μ§λ§μ΄ 1λ‘ λλλλ‘ μΌλ¨ λμ΄μ ν΄λ΄€λλ° μλμ κ°μλ€.
μ«μ | μ | νμ |
1 | 1 | 1 |
2 | 1+1 | 2 |
3 | 1+1+1 | 3 |
4 | 1+2+1 | 3 |
5 | 1+2+1+1 | 4 |
6 | 1+2+2+1 | 4 |
7 | 1+2+2+1+1 | 5 |
8 | 1+2+2+2+1 | 5 |
9 | 1+2+3+2+1 | 5 |
10 | 1+2+3+2+1+1 | 6 |
11 | 1+2+3+2+2+1 | 6 |
12 | 1+2+3+3+2+1 | 6 |
13 | 1+2+3+3+2+2+1 | 7 |
14 | 1+2+3+3+2+2+1 | 7 |
15 | 1+2+3+3+3+2+2+1 | 8 |
16 | 1+2+3+4+3+2+1 | 8 |
μ κ³±μλ§λ€ μλ‘μ΄ μ«μκ° λνλλ κ²μ λ³Ό μ μλ€. (4μ κ²½μ° 2 λ±μ₯, 9μ κ²½μ° 3λ±μ₯) κ·Έλ¦¬κ³ μ κ³±μμ κ²½μ° 1+2+3+..n+n-1+n-2+...1 μ¦ νμλ νμ μμ μ κ³±κ·Ό +1μ΄λ€. (4μ κ²½μ° λ£¨νΈ4+1. μ¦ 3μ΄λ€.)
κ·Έλ¦¬κ³ 5 6 7 8μ 보면 56μκ²½μ° 4λ², 78μ 5λ²μΈλ° 6μ 2+4λ‘ λνλΌ μ μλ€λ μ¬μ€μ μ£Όλͺ©νμ.
λΉμ·ν μμλ‘ 9μ 16μ¬μ΄μμ νμκ° λ°λλ λΆλΆμΈ 12μμ 3+9μ΄λ€. μ½λλ‘ λνλ΄λ©΄ μλμ κ°λ€.
μ½λ
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T;
long long a, b;
cin >> T;
while (T != 0) {
cin >> a >> b;
int go =(int)sqrt(b - a);
if (b - a < 4) {
cout << b - a<<"\n";
}
else {
//μ κ³±μμ κ²½μ° ex)4
if (b - a == go * go) {
cout << 2 * go - 1 << "\n";
}
//거리+거리μ μ κ³±κ·Όλ³΄λ€ ν΄ κ²½μ° ex)7,8
else if (go * go + go < b - a) {
cout << go * 2 + 1 << "\n";
}
//거리+거리μ μ κ³±κ·Όλ³΄λ€ μμ κ²½μ°ex)5,6
else {
cout << go * 2 << "\n";
}
}
T--;
}
}