#include<iostream>
#include<stdio.h>
#include<vector>
#include<cmath>
#include<queue>
#include<utility>
#include<string.h>
#include<map>
#include<set>
#include<algorithm>
#define ll long long
#define pi pair < ll,ll >
#define mp(a,b) make_pair(a,b)
#define rep(i,a,b) for(int i = a;i < b;i++)
#define N 100004
#define INF 1e18
using namespace std;
ll n,k;
pi ar[N];
ll b,d;
char a,c;
ll ans,cnt;
vector <ll>possible;
vector < ll > ola;
set < ll > p;
ll abso(ll x)
{
if(x<0)
return -x;
return x;
}
ll checkbridge(ll x,ll y)
{
ll res = ans;
rep(i,0,cnt)
{
if(ar[i].first <= x && x <= ar[i].second)
{
res += ar[i].second-ar[i].first;
}
else if(ar[i].first <= y && y <= ar[i].second)
{
res+=ar[i].second-ar[i].first;
}
else
{
res += 2*min(min(abso(ar[i].first-x),abso(ar[i].second-x)),min(abso(ar[i].first-y),abso(ar[i].second-y)))+(ar[i].second-ar[i].first);
}
}
// cout << x << " " << y << " " << res << endl;
return res;
}
ll findans(ll x)
{
ll low = x;
ll high = possible.size()-1;
ll res = x;
while(low <= high)
{
ll mid = (low+high)/2;
if(checkbridge(possible[x],possible[mid-1]) >= checkbridge(possible[x],possible[mid]))
{
low = mid+1;
res = mid;
}
else
{
high = mid-1;
}
}
ll fin = checkbridge(possible[x],possible[res]);
low = 0;
high = x;
res = 0;
while(low <= high)
{
ll mid = (low+high)/2;
if(checkbridge(possible[x],possible[mid-1]) >= checkbridge(possible[x],possible[mid]))
{
low = mid+1;
res = mid;
}
else
{
high = mid-1;
}
}
return min(fin,checkbridge(possible[x],possible[res]));
}
void solve2()
{
ll fin = checkbridge(possible[0],possible[0]);
rep(i,0,possible.size())
{
fin = min(fin,findans(i));
}
cout << fin << endl;
return;
}
int main()
{
ios_base::sync_with_stdio(false);
// freopen("bridge_3_3.in","r",stdin);
cin >> k >> n;
rep(i,0,n)
{
cin >> a >> b >> c >> d;
if(d < b)
swap(b,d);
if(a == c)
ans += d-b;
else
{
ar[cnt].first = b;
ar[cnt].second = d;
ola.push_back(b);
ola.push_back(d);
ans++;
cnt++;
}
}
if(cnt == 0)
{
cout << ans;
return 0;
}
rep(i,0,cnt)
{
p.insert(ar[i].first);
p.insert(ar[i].second);
}
for(set < ll >::iterator it = p.begin();it != p.end();it++)
{
possible.push_back(*it);
}
if(k == 2)
{
solve2();
return 0;
}
ll low = 0;
ll high = possible.size()-1;
ll res = 0;
while(low <= high)
{
ll mid = (low+high)/2;
if(checkbridge(possible[mid-1],possible[mid-1]) >= checkbridge(possible[mid],possible[mid]))
{
low = mid+1;
res = mid;
}
else
{
high = mid-1;
}
}
cout << checkbridge(possible[res],possible[res]);
return 0;
}
/*
2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
2 3
A 1 B 1
A 4 B 7
A 10 B 12
*/
Compilation message
bridge.cpp: In function 'void solve2()':
bridge.cpp:14:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define rep(i,a,b) for(int i = a;i < b;i++)
bridge.cpp:110:9:
rep(i,0,possible.size())
~~~~~~~~~~~~~~~~~~~
bridge.cpp:110:5: note: in expansion of macro 'rep'
rep(i,0,possible.size())
^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
512 KB |
Output is correct |
8 |
Correct |
3 ms |
512 KB |
Output is correct |
9 |
Correct |
3 ms |
512 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
512 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
3 ms |
512 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
512 KB |
Output is correct |
12 |
Correct |
26 ms |
3584 KB |
Output is correct |
13 |
Correct |
219 ms |
15988 KB |
Output is correct |
14 |
Correct |
68 ms |
3812 KB |
Output is correct |
15 |
Correct |
97 ms |
9200 KB |
Output is correct |
16 |
Correct |
29 ms |
3576 KB |
Output is correct |
17 |
Correct |
104 ms |
15956 KB |
Output is correct |
18 |
Correct |
101 ms |
15976 KB |
Output is correct |
19 |
Correct |
181 ms |
15592 KB |
Output is correct |
20 |
Correct |
35 ms |
3568 KB |
Output is correct |
21 |
Correct |
128 ms |
15976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
15 ms |
384 KB |
Output is correct |
15 |
Correct |
351 ms |
540 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
8 ms |
384 KB |
Output is correct |
18 |
Correct |
35 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
347 ms |
632 KB |
Output is correct |
21 |
Correct |
153 ms |
512 KB |
Output is correct |
22 |
Correct |
547 ms |
632 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
489 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
15 ms |
476 KB |
Output is correct |
15 |
Correct |
346 ms |
632 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
9 ms |
512 KB |
Output is correct |
18 |
Correct |
35 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
357 ms |
512 KB |
Output is correct |
21 |
Correct |
160 ms |
512 KB |
Output is correct |
22 |
Correct |
550 ms |
512 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
494 ms |
632 KB |
Output is correct |
25 |
Correct |
33 ms |
4476 KB |
Output is correct |
26 |
Correct |
1997 ms |
4712 KB |
Output is correct |
27 |
Execution timed out |
2051 ms |
16740 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |