#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2")
#include<bits/stdc++.h>
#define int long long
#define down cout<<'\n';
#define NHP ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
#define modwwe int t;cin>>t; while(t--)
#define bit(i,j) (i>>j&1)
#define sobit(a) __builtin_popcountll(a)
#define task "vrtic"
#define fin(x) freopen(x".inp","r",stdin)
#define fou(x) freopen(x".out","w",stdout)
#define pb push_back
#define checktime cerr << (double)clock() / CLOCKS_PER_SEC * 1000 << " ms";
using namespace std;
void phongbeo();
const int mod2=1e9+7;
const int mod1=998244353;
struct ib
{
int a;
int b;
};
struct icd
{
int a,b;
};
struct ic
{
int a,b,c;
};
struct id
{
int a,b,c,d;
};
int n,m,s2,s4,s3,sf,k,r,mid,s5,s6,mx,s7,s8,s9,mx2,res,dem2=0,dem=0,l;
int i,s10,s12;
int el=29;
main()
{
//#ifndef ONLINE_JUDGE
// fin(task),fou(task);
//#endif
NHP
//modwwe
// cin>>res;
phongbeo(),down
checktime
}
int dp[100001];
ib a[100001];
struct cmp{
bool operator()(int a,int b)
{
return a>b;
}
};
struct cmp2{
bool operator()(int a,int b)
{
return a<b;
}
};
priority_queue<int,vector<int>,cmp2>p;
priority_queue<int,vector<int>,cmp>p2;
void get()
{
if(p.top()>p2.top())
{ s5=p.top();
s6=p2.top();
s2=s2+s5*2;
s2=s2-s6*2;
s3+=s5;
s3-=s6;
s4+=s6;
s4-=s5;
p2.push(s5);
p.push(s6);
p2.pop();
p.pop();
}
}
bool cmp3(ib a,ib b)
{
return a.a+a.b<b.a+b.b;
}
void phongbeo()
{
cin>>k>>n;
for(int i=1;i<=n;i++)
{
string ss,sx;
cin>>ss>>l>>sx>>r;
if(ss==sx)
{
s8+=abs(r-l);
continue;
}
a[++dem]={l,r};
}
sort(a+1,a+1+dem,cmp3);
for(int i=1;i<=dem;i++)
{
s2+=abs(a[i].a-a[i].b);
p.push(min(a[i].b,a[i].a));
s3+=max(a[i].a,a[i].b);
s4+=min(a[i].a,a[i].b);
p2.push(max(a[i].b,a[i].a));
get();
dp[i]=s2;
}
if(k==1){
cout<<s2+dem+s8;
exit(0);}
else
{
while(!p.empty())
{
p.pop();
}
while(!p2.empty())
{
p2.pop();
}
s2=0;
s7=dp[dem];
for(int i=dem;i>=1;--i)
{ s2+=abs(a[i].a-a[i].b);
p.push(min(a[i].b,a[i].a));
p2.push(max(a[i].b,a[i].a));
get();
s7=min(s7,s2+dp[i-1]);
}
cout<<s7+dem+s8;
}
}
Compilation message
bridge.cpp:39:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
39 | main()
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
456 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
2512 KB |
Output is correct |
4 |
Correct |
2 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2648 KB |
Output is correct |
8 |
Correct |
2 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
2648 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2528 KB |
Output is correct |
6 |
Correct |
1 ms |
2648 KB |
Output is correct |
7 |
Correct |
1 ms |
2544 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2552 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
36 ms |
5312 KB |
Output is correct |
13 |
Correct |
56 ms |
6688 KB |
Output is correct |
14 |
Correct |
43 ms |
5456 KB |
Output is correct |
15 |
Correct |
39 ms |
5016 KB |
Output is correct |
16 |
Correct |
33 ms |
6080 KB |
Output is correct |
17 |
Correct |
49 ms |
6596 KB |
Output is correct |
18 |
Correct |
34 ms |
6332 KB |
Output is correct |
19 |
Correct |
57 ms |
6848 KB |
Output is correct |
20 |
Correct |
43 ms |
6320 KB |
Output is correct |
21 |
Correct |
54 ms |
6456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2512 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2516 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2524 KB |
Output is correct |
22 |
Correct |
1 ms |
2392 KB |
Output is correct |
23 |
Correct |
1 ms |
2536 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2408 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2648 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
2 ms |
2392 KB |
Output is correct |
24 |
Correct |
2 ms |
2396 KB |
Output is correct |
25 |
Correct |
40 ms |
5172 KB |
Output is correct |
26 |
Correct |
70 ms |
5376 KB |
Output is correct |
27 |
Correct |
83 ms |
6256 KB |
Output is correct |
28 |
Correct |
87 ms |
6720 KB |
Output is correct |
29 |
Correct |
85 ms |
6848 KB |
Output is correct |
30 |
Correct |
44 ms |
4556 KB |
Output is correct |
31 |
Correct |
38 ms |
5820 KB |
Output is correct |
32 |
Correct |
95 ms |
6548 KB |
Output is correct |
33 |
Correct |
57 ms |
6092 KB |
Output is correct |
34 |
Correct |
88 ms |
6424 KB |
Output is correct |
35 |
Correct |
59 ms |
5844 KB |
Output is correct |
36 |
Correct |
84 ms |
6332 KB |
Output is correct |
37 |
Correct |
35 ms |
5060 KB |
Output is correct |