이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pair<int,int>> vpi;
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)
#define pb push_back
#define rsz resize
#define sz(x) int(x.size())
#define all(x) begin(x), end(x)
#define f first
#define s second
vpi cross;
ll build(int bri)
{
ll val=0;
trav(e, cross)
{
val += (ll)(e.s-e.f+1);
if(bri>e.s)
{
val += (ll)(bri - e.s) * (ll)2;
}
else if(bri<e.f)
{
val += (ll)(e.f - bri) * (ll)2;
}
}
return val;
}
bool ok(int now)
{
ll g = build(now+1) - build(now);
if(g<0)return 1;
else return 0;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int K,N;
cin>>K>>N;
char P[N],Q[N];
int S[N],T[N];
ll base=0;
F0R(i,N)
{
cin>>P[i]>>S[i]>>Q[i]>>T[i];
if(P[i]==Q[i])
{
base += (ll)( max(S[i],T[i]) - min(S[i],T[i]) );
}
else
{
cross.pb({min(S[i],T[i]), max(S[i],T[i])});
}
}
int lef=0,rig=1e9;
int mid;
while(lef<=rig)
{
mid=(lef+rig)/2;
if(ok(mid))lef=mid+1;
else rig=mid-1;
}
ll ans = 2e14+5;
FOR(i,max(0,lef-10),min((int)1e9+1,lef+10))
{
ans=min(ans,build(i));
}
if(ans==2e14+5)ans=0;
ans+=base;
cout<<ans<<'\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |