Submission #14386

# Submission time Handle Problem Language Result Execution time Memory
14386 2015-05-13T15:37:11 Z woqja125 Palembang Bridges (APIO15_bridge) C++14
100 / 100
109 ms 5996 KB
#include<stdio.h>
#include<queue>
#include<algorithm>
#include<vector>
long long abs(long long x){return x>0?x:-x;}
void solve1();
void solve2();

struct person
{
	int l, r;
};
bool operator<(const person &A, const person &B){return A.l+A.r<B.l+B.r;}

int k, N=0;
long long ans=0;
person l[100001];
int main()
{
	int i, n;
	scanf("%d%d", &k, &n);
	char a[10], b[10];
	int aa, bb;
	for(i=1; i<=n; i++)
	{
		scanf("%s%d%s%d", a, &aa, b, &bb);
		ans += abs(aa-bb);
		if(a[0] != b[0])
		{
			N++;
			l[N].l = aa>bb?bb:aa;
			l[N].r = aa<bb?bb:aa;
			ans++;
		}
	}
	if(k==1) solve1();
	else solve2();
	printf("%lld\n", ans);
	return 0;
}

struct lineSet
{
	std::priority_queue<int> l;
	std::priority_queue<int, std::vector<int>, std::greater<int> > r;
	long long ans=0;
	void add(int ll, int rl);
};

void solve1()
{
	lineSet T;
	for(int i=1; i<=N; i++)
	{
		T.add(l[i].l, l[i].r);
	}
	ans += T.ans;
}

long long dp1[100010], dp2[100010];
void solve2()
{
	int i;
	std::sort(l+1, l+1+N);
	lineSet L, R;
	for(i=1; i<=N; i++)
	{
		L.add(l[i].l, l[i].r);
		dp1[i] = L.ans;
	}
	for(i=N; i>=1; i--)
	{
		R.add(l[i].l, l[i].r);
		dp2[i] = R.ans;
	}
	long long min = dp2[1];
	for(i=1; i<=N; i++)
	{
		if(min > dp1[i] + dp2[i+1]) min = dp1[i] + dp2[i+1];
	}
	ans += min;
}

void lineSet::add(int ll, int rl)
{
	l.push(ll); r.push(rl);
//	printf("##%d %d\n", ll, rl);
	while(1)
	{
		ll = l.top();
		rl = r.top();
//		printf("#%d %d\n", ll, rl);
		if(ll<=rl) return;
		l.pop(); r.pop();
		this->ans += (ll-rl)*2;
		l.push(rl); r.push(ll);
	}
}

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:21:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &k, &n);
                       ^
bridge.cpp:26:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%s%d%s%d", a, &aa, b, &bb);
                                    ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 3520 KB Output is correct
2 Correct 0 ms 3520 KB Output is correct
3 Correct 0 ms 3520 KB Output is correct
4 Correct 0 ms 3520 KB Output is correct
5 Correct 0 ms 3520 KB Output is correct
6 Correct 0 ms 3520 KB Output is correct
7 Correct 0 ms 3520 KB Output is correct
8 Correct 0 ms 3520 KB Output is correct
9 Correct 0 ms 3520 KB Output is correct
10 Correct 0 ms 3520 KB Output is correct
11 Correct 0 ms 3520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 3520 KB Output is correct
2 Correct 0 ms 3520 KB Output is correct
3 Correct 0 ms 3520 KB Output is correct
4 Correct 0 ms 3520 KB Output is correct
5 Correct 0 ms 3520 KB Output is correct
6 Correct 0 ms 3520 KB Output is correct
7 Correct 0 ms 3520 KB Output is correct
8 Correct 0 ms 3520 KB Output is correct
9 Correct 0 ms 3520 KB Output is correct
10 Correct 0 ms 3520 KB Output is correct
11 Correct 0 ms 3520 KB Output is correct
12 Correct 39 ms 4972 KB Output is correct
13 Correct 76 ms 4972 KB Output is correct
14 Correct 33 ms 4972 KB Output is correct
15 Correct 46 ms 4200 KB Output is correct
16 Correct 39 ms 4972 KB Output is correct
17 Correct 73 ms 4972 KB Output is correct
18 Correct 83 ms 4972 KB Output is correct
19 Correct 69 ms 4972 KB Output is correct
20 Correct 73 ms 4972 KB Output is correct
21 Correct 89 ms 4972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 3520 KB Output is correct
2 Correct 0 ms 3520 KB Output is correct
3 Correct 0 ms 3520 KB Output is correct
4 Correct 0 ms 3520 KB Output is correct
5 Correct 0 ms 3520 KB Output is correct
6 Correct 0 ms 3520 KB Output is correct
7 Correct 0 ms 3520 KB Output is correct
8 Correct 0 ms 3520 KB Output is correct
9 Correct 0 ms 3520 KB Output is correct
10 Correct 0 ms 3520 KB Output is correct
11 Correct 0 ms 3520 KB Output is correct
12 Correct 0 ms 3520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 3520 KB Output is correct
2 Correct 0 ms 3520 KB Output is correct
3 Correct 0 ms 3520 KB Output is correct
4 Correct 0 ms 3520 KB Output is correct
5 Correct 0 ms 3520 KB Output is correct
6 Correct 0 ms 3520 KB Output is correct
7 Correct 0 ms 3520 KB Output is correct
8 Correct 0 ms 3520 KB Output is correct
9 Correct 0 ms 3520 KB Output is correct
10 Correct 0 ms 3520 KB Output is correct
11 Correct 0 ms 3520 KB Output is correct
12 Correct 0 ms 3520 KB Output is correct
13 Correct 0 ms 3520 KB Output is correct
14 Correct 0 ms 3520 KB Output is correct
15 Correct 0 ms 3520 KB Output is correct
16 Correct 0 ms 3520 KB Output is correct
17 Correct 0 ms 3520 KB Output is correct
18 Correct 0 ms 3520 KB Output is correct
19 Correct 0 ms 3520 KB Output is correct
20 Correct 0 ms 3520 KB Output is correct
21 Correct 0 ms 3520 KB Output is correct
22 Correct 0 ms 3520 KB Output is correct
23 Correct 0 ms 3520 KB Output is correct
24 Correct 0 ms 3520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 3520 KB Output is correct
2 Correct 0 ms 3520 KB Output is correct
3 Correct 0 ms 3520 KB Output is correct
4 Correct 0 ms 3520 KB Output is correct
5 Correct 0 ms 3520 KB Output is correct
6 Correct 0 ms 3520 KB Output is correct
7 Correct 0 ms 3520 KB Output is correct
8 Correct 0 ms 3520 KB Output is correct
9 Correct 0 ms 3520 KB Output is correct
10 Correct 0 ms 3520 KB Output is correct
11 Correct 0 ms 3520 KB Output is correct
12 Correct 0 ms 3520 KB Output is correct
13 Correct 0 ms 3520 KB Output is correct
14 Correct 0 ms 3520 KB Output is correct
15 Correct 0 ms 3520 KB Output is correct
16 Correct 0 ms 3520 KB Output is correct
17 Correct 0 ms 3520 KB Output is correct
18 Correct 0 ms 3520 KB Output is correct
19 Correct 0 ms 3520 KB Output is correct
20 Correct 0 ms 3520 KB Output is correct
21 Correct 0 ms 3520 KB Output is correct
22 Correct 0 ms 3520 KB Output is correct
23 Correct 0 ms 3520 KB Output is correct
24 Correct 0 ms 3520 KB Output is correct
25 Correct 39 ms 5996 KB Output is correct
26 Correct 66 ms 5996 KB Output is correct
27 Correct 93 ms 5996 KB Output is correct
28 Correct 99 ms 5996 KB Output is correct
29 Correct 106 ms 5996 KB Output is correct
30 Correct 53 ms 4764 KB Output is correct
31 Correct 36 ms 5996 KB Output is correct
32 Correct 86 ms 5996 KB Output is correct
33 Correct 39 ms 5996 KB Output is correct
34 Correct 106 ms 5996 KB Output is correct
35 Correct 73 ms 5996 KB Output is correct
36 Correct 109 ms 5996 KB Output is correct
37 Correct 33 ms 5996 KB Output is correct