Submission #1037616

#TimeUsernameProblemLanguageResultExecution timeMemory
1037616KasymKPalembang Bridges (APIO15_bridge)C++17
17 / 100
2096 ms2140 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); 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; } if(k == 1){ ll answer = 1e18; for(int k = 1; k <= n; ++k){ ll sm = 0; int br = s[k]; for(int i = 1; i <= n; ++i){ if(p[i] == q[i]){ sm += abs(s[i]-t[i]); continue; } // p[i] != q[i] sm++; // this is for bridge crossing sm += abs(br-s[i]); sm += abs(br-t[i]); } umin(answer, sm); sm = 0; br = t[k]; for(int i = 1; i <= n; ++i){ if(p[i] == q[i]){ sm += abs(s[i]-t[i]); continue; } sm++; sm += abs(br-s[i]); sm += abs(br-t[i]); } umin(answer, sm); } printf("%lld\n", answer); return 0; } // k = 2 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; }

Compilation message (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:25:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 |   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...