- 데이터가 많으므로 stdio와 동기화 해제.
- dx[i] = i-1번째 칸에서 올라왔을 때, 부딪치는 방해물의 증감.
- 높이 1에서 시작하여 dx[i] 만큼 계속 더하면서 최솟값과 개수를 갱신.
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int N, H, half;
cin >> N >> H;
half = N/2;
int up, down, dx[H];
memset(dx, 0, sizeof dx);
for(int i=0; i<half; i++){
cin >> up >> down;
--dx[down];
++dx[H-up];
}
int cur_val = N/2;
int ret_val = N+1, ret_cnt = 1;
for(int i=0; i<H; i++){
cur_val += dx[i];
if(cur_val < ret_val) {
ret_val = cur_val;
ret_cnt = 1;
}
else if(cur_val == ret_val) {
ret_cnt++;
}
}
cout << ret_val << ' ' << ret_cnt;
}
#include <iostream>
#include <cstring>
using namespace std;
const int BUF_SIZE = 1 << 20; // 메모리 초과 가능, 적당히 조절할 것.
char input_buf[BUF_SIZE], output_buf[BUF_SIZE];
int input_buf_pos, output_buf_pos;
enum io_mode {READ, WRITE};
inline void char_io(io_mode mode, char &target)
{
if(mode == READ) {
if (input_buf_pos == BUF_SIZE)
{
fread(input_buf, sizeof(char), BUF_SIZE, stdin);
input_buf_pos = 0;
}
target = input_buf[input_buf_pos++];
}
else {
if(output_buf_pos == BUF_SIZE || target == EOF)
{
fwrite(output_buf, sizeof(char), output_buf_pos, stdout);
output_buf_pos = 0;
}
output_buf[output_buf_pos++] = target;
}
}
inline void char_io(io_mode mode, char &&target){
char_io(mode, target);
}
inline void int_io(io_mode mode, int &target)
{
char c;
if(mode == READ){
bool negative = false;
int ret = 0;
while(true)
{
char_io(READ, c);
if(c == '\n' || c == ' ') break;
if(c == '-') {
negative = true;
continue;
}
ret = ret * 10 + (c & 0b1111);
}
target = negative ? -ret : ret;
}
else {
int abs = target;
int div = 1'000'000'000;
char quotient = 0;
if(abs < 0) {
char_io(WRITE, '-');
abs *= -1;
}
while(abs < div && div != 1) { div /= 10; }
while(div != 0){
quotient = '0' + (char)((abs / div)%10);
char_io(WRITE, quotient);
div /= 10;
}
}
}
inline void int_io(io_mode mode, int &&target){
int_io(mode, target);
}
int main() {
int N, H, half;
int_io(READ, N);
int_io(READ, H);
half = N/2;
int up, down, dx[H];
memset(dx, 0, sizeof dx);
for(int i=0; i<half; i++){
int_io(READ, up);
int_io(READ, down);
--dx[down];
++dx[H-up];
}
int cur_val = N/2;
int ret_val = N+1, ret_cnt = 1;
for(int i=0; i<H; i++){
cur_val += dx[i];
if(cur_val < ret_val) {
ret_val = cur_val;
ret_cnt = 1;
}
else if(cur_val == ret_val) {
ret_cnt++;
}
}
int_io(WRITE, ret_val);
char_io(WRITE, ' ');
int_io(WRITE, ret_cnt);
char_io(WRITE, EOF);
}