제출 #1037806

#제출 시각아이디문제언어결과실행 시간메모리
1037806KasymKPalembang Bridges (APIO15_bridge)C++17
31 / 100
2074 ms9988 KiB

#include "bits/stdc++.h"
using namespace std;
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define wr puts("----------------")
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
const int MOD = 1e9+7;
const int INF = 1e9;
const int N = 1e5+5;
int s[N], t[N];
char p[N], q[N];

int main(){
	int k, n;
	scanf("%d%d", &k, &n);
	if(k == 1){
		ll sm = 0, sum = 0;
		int cnt = 0;
		multiset<int> A;
	    for(int i = 1; i <= n; ++i){
	    	char p, q;
	    	int s, t;
	    	scanf(" %c %d %c %d", &p, &s, &q, &t);
	        if (p == q)
	            sm += abs(s-t);
	        else{
	        	cnt++;
	        	A.insert(s);
	        	A.insert(t);
	        	sum -= (s+t);
	        }
	    }
	    ll answer = cnt+sm;
	    while(cnt--)
	    	sum += (*--A.end()*2), A.erase(--A.end());
	    answer += sum;
	    printf("%lld\n", answer);
	    return 0;
	}
	// k=2
	for(int i = 1; i <= n; ++i){
		char a, b;
		int x, y;
		scanf(" %c %d %c %d", &a, &x, &b, &y);
		p[i] = a, s[i] = x, q[i] = b, t[i] = y;
	}
	ll answer = 1e18;
	for(int x = 1; x <= n; ++x)
		for(int y = 1; y <= n; ++y){
			if(s[x] == t[y])
				continue; // two bridge cannot be in same place
			ll sm = 0;
			int br = s[x], br_ = t[y];
			for(int i = 1; i <= n; ++i){
				if(p[i] == q[i]){
					sm += abs(s[i]-t[i]);
					continue;
				}
				sm++;
				ll A = abs(br-s[i])+abs(br-t[i]);
				ll B = abs(br_-s[i])+abs(br_-t[i]);
				sm += min(A, B);
			}
			umin(answer, sm);
			sm = 0;
			if(s[y] == t[x])
				continue;
			br = s[y], br_ = t[x];
			for(int i = 1; i <= n; ++i){
				if(p[i] == q[i]){
					sm += abs(s[i]-t[i]);
					continue;
				}
				sm++;
				ll A = abs(br-s[i])+abs(br-t[i]);
				ll B = abs(br_-s[i])+abs(br_-t[i]);
				sm += min(A, B);
			}
			umin(answer, sm);
		}
	printf("%lld\n", answer);
    return 0;
}

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

bridge.cpp: In function 'int main()':
bridge.cpp:21:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |  scanf("%d%d", &k, &n);
      |  ~~~~~^~~~~~~~~~~~~~~~
bridge.cpp:29:12: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   29 |       scanf(" %c %d %c %d", &p, &s, &q, &t);
      |       ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:50:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |   scanf(" %c %d %c %d", &a, &x, &b, &y);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...