제출 #466666

#제출 시각아이디문제언어결과실행 시간메모리
466666cgiosyPalembang Bridges (APIO15_bridge)C++17
100 / 100
90 ms3132 KiB
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using PQ=priority_queue<int>;

struct pii {
	int l, r;
	bool operator<(pii b) const { return l+r<b.l+b.r; }
};
int main() {
	int K, N, M=0;
	scanf("%d%d", &K, &N);
	pii A[N];
	ll B[N], s=0, w=0;
	for(int i=0; i<N; i++) {
		char a, b;
		int l, r;
		scanf(" %c%d %c%d", &a, &l, &b, &r);
		if(l>r) swap(l, r);
		if(a!=b) A[M++]={l, r};
		else s+=r-l;
	}
	s+=N=M;
	sort(A, A+N);

	PQ L, R;
	auto add=[&](pii v) {
		L.push(v.l), R.push(-v.r);
		w+=v.r-v.l;
		if(L.top()>-R.top()) {
			int l=L.top(), r=-R.top();
			L.pop(), R.pop();
			L.push(r), R.push(-l);
			w+=(l-r)*2;
		}
		return w;
	};

	for(int i=0; i<N; i++) B[i]=add(A[i]);
	w=0;
	PQ().swap(L);
	PQ().swap(R);
	ll t=N ? B[N-1] : 0;
	if(N && K==2) for(int i=N; --i;) t=min(t, add(A[i])+B[i-1]);
	printf("%lld\n", s+t);
}

컴파일 시 표준 에러 (stderr) 메시지

bridge.cpp: In function 'int main()':
bridge.cpp:12:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 |  scanf("%d%d", &K, &N);
      |  ~~~~~^~~~~~~~~~~~~~~~
bridge.cpp:18:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |   scanf(" %c%d %c%d", &a, &l, &b, &r);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...