답안 #135509

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
135509 2019-07-24T07:16:31 Z 이온조(#3249) MP3 Player (CEOI10_mp3player) C++14
30 / 100
105 ms 7788 KB
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
#define X first
#define Y second

const int INF = 1e9;

struct segtree {
	vector<int> mn, mx, L;
	void init(int idx, int s, int e, int v) {
		if(s == e) {
			mn[idx] = mx[idx] = v;
			return;
		}
		int m = s+e >> 1;
		init(idx*2, s, m, v);
		init(idx*2+1, m+1, e, v);
		mn[idx] = mx[idx] = v;
	}
	void upl(int idx, int s, int e) {
		if(L[idx]) {
			mn[idx] += L[idx];
			mx[idx] += L[idx];
			if(s != e) {
				L[idx*2] += L[idx];
				L[idx*2+1] += L[idx];
			}
			L[idx] = 0;
		}
	}
	void upd(int idx, int s, int e, int l, int r, int v) {
		upl(idx, s, e);
		if(r < s || e < l) return;
		if(l <= s && e <= r) {
			L[idx] += v;
			upl(idx, s, e);
			return;
		}
		int m = s+e >> 1;
		upd(idx*2, s, m, l, r, v);
		upd(idx*2+1, m+1, e, l, r, v);
		mn[idx] = min(mn[idx*2], mn[idx*2+1]);
		mx[idx] = max(mx[idx*2], mx[idx*2+1]);
	}
	int findU(int idx, int s, int e, int v) {
		upl(idx, s, e);
		if(mx[idx] < v) return 0;
		if(s == e) return s;
		int m = s+e >> 1;
		int tmp = findU(idx*2+1, m+1, e, v);
		if(tmp == 0) return findU(idx*2, s, m, v);
		return tmp;
	}
	int findD(int idx, int s, int e, int v) {
		upl(idx, s, e);
		if(mn[idx] > v) return 0;
		if(s == e) return s;
		int m = s+e >> 1;
		int tmp = findD(idx*2+1, m+1, e, v);
		if(tmp == 0) return findD(idx*2, s, m, v);
		return tmp;
	}
	int gmn(int idx, int s, int e, int l, int r) {
		upl(idx, s, e);
		if(r < s || e < l) return INF;
		if(l <= s && e <= r) return mn[idx];
		int m = s+e >> 1;
		return min(gmn(idx*2, s, m, l, r), gmn(idx*2+1, m+1, e, l, r));
	}
	int gmx(int idx, int s, int e, int l, int r) {
		upl(idx, s, e);
		if(r < s || e < l) return -INF;
		if(l <= s && e <= r) return mx[idx];
		int m = s+e >> 1;
		return max(gmx(idx*2, s, m, l, r), gmx(idx*2+1, m+1, e, l, r));
	}
	segtree(int N, int V2) {
		mn.resize(4*N);
		mx.resize(4*N);
		L.resize(4*N);
		init(1, 1, N, V2);
	}
};

int N, C[100009];
char T[100009];
vector<pii> S;
bool chk[100009];

void add(segtree &seg, int i) {
	chk[i] = 1;
	if(T[i] == '+') seg.upd(1, 1, N, 1, i, -1);
	if(T[i] == '-') seg.upd(1, 1, N, 1, i, +1);
}

void show(segtree &seg) {
	puts("SHOW");
	for(int i=1; i<=N; i++) {
		if(chk[i]) printf("%c %d, sum: %d\n", T[i], C[i], seg.gmx(1, 1, N, i, i));
	}
	puts("----------------");
}

int main() {
	int VM, V2; scanf("%d%d%d",&N,&VM,&V2);
	if(VM == 0) return !printf("infinity");
	int ans = 0, V1 = V2;
	for(int i=1; i<=N; i++) {
		scanf(" %c%d", &T[i], &C[i]);
		if(i >= 2) S.push_back({C[i] - C[i-1], i});
	}
	sort(S.begin(), S.end());
	S.push_back({-1, -1});

	segtree seg(N, V2);
	for(int k=0; k<N-1; k++) {
		int cost = S[k].X;
		add(seg, S[k].Y);
		while(cost == S[k+1].X) {
			++k;
			add(seg, S[k].Y);
		}
		bool f = 1; int R;
		int fu = seg.findU(1, 1, N, VM);
		int fd = seg.findD(1, 1, N, 0);

		if(fu == -1 && fd == -1) R = seg.gmx(1, 1, N, 1, 1);
		else if(fd > fu) {
			if(seg.gmn(1, 1, N, fu+1, fd) < 0) f = 0;
			else {
				if(fu == -1) R = seg.gmx(1, 1, N, 1, 1);
				else R = VM;
			}
		}
		else {
			if(seg.gmx(1, 1, N, fd+1, fu) > VM) f = 0;
			else R = VM;
		}

		if(f) {
			if(k+2 == N) return !printf("infinity");
			ans = S[k+1].X - 1; V1 = R;
		}
	}
	printf("%d %d\n", ans, V1);
	return 0;
}

Compilation message

mp3player.cpp: In member function 'void segtree::init(int, int, int, int)':
mp3player.cpp:16:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   int m = s+e >> 1;
           ~^~
mp3player.cpp: In member function 'void segtree::upd(int, int, int, int, int, int)':
mp3player.cpp:40:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   int m = s+e >> 1;
           ~^~
mp3player.cpp: In member function 'int segtree::findU(int, int, int, int)':
mp3player.cpp:50:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   int m = s+e >> 1;
           ~^~
mp3player.cpp: In member function 'int segtree::findD(int, int, int, int)':
mp3player.cpp:59:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   int m = s+e >> 1;
           ~^~
mp3player.cpp: In member function 'int segtree::gmn(int, int, int, int, int)':
mp3player.cpp:68:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   int m = s+e >> 1;
           ~^~
mp3player.cpp: In member function 'int segtree::gmx(int, int, int, int, int)':
mp3player.cpp:75:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   int m = s+e >> 1;
           ~^~
mp3player.cpp: In function 'int main()':
mp3player.cpp:106:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  int VM, V2; scanf("%d%d%d",&N,&VM,&V2);
              ~~~~~^~~~~~~~~~~~~~~~~~~~~
mp3player.cpp:110:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf(" %c%d", &T[i], &C[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 376 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 632 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 504 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 6 ms 572 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 2036 KB Output is correct
2 Correct 21 ms 2164 KB Output is correct
3 Correct 18 ms 2036 KB Output is correct
4 Correct 19 ms 1908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 2292 KB Output is correct
2 Correct 20 ms 2288 KB Output is correct
3 Correct 20 ms 2292 KB Output is correct
4 Correct 29 ms 2164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 2676 KB Output is correct
2 Incorrect 34 ms 2676 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 56 ms 3696 KB Output is correct
2 Correct 36 ms 3748 KB Output is correct
3 Correct 38 ms 3440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 99 ms 7768 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 105 ms 7788 KB Output isn't correct
2 Halted 0 ms 0 KB -