Submission #829003

# Submission time Handle Problem Language Result Execution time Memory
829003 2023-08-18T00:47:50 Z arnold518 Radio Towers (IOI22_towers) C++17
23 / 100
4000 ms 13060 KB
#include "towers.h"
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 1e5;
const int INF = 1e9+7;

int N, A[MAXN+10], B[MAXN+10], P[MAXN+10];

struct SEG
{
	int tree[MAXN*4+10];
	void init(int node, int tl, int tr)
	{
		if(tl==tr)
		{
			tree[node]=A[tl];
			return;
		}
		int mid=tl+tr>>1;
		init(node*2, tl, mid);
		init(node*2+1, mid+1, tr);
		tree[node]=min(tree[node*2], tree[node*2+1]);
	}
	int query(int node, int tl, int tr, int l, int r)
	{
		if(r<tl || tr<l) return INF;
		if(l<=tl && tr<=r) return tree[node];
		int mid=tl+tr>>1;
		return min(query(node*2, tl, mid, l, r), query(node*2+1, mid+1, tr, l, r));
	}
}seg;

void init(int _N, vector<int> _H)
{
	N=_N;
	for(int i=1; i<=N; i++) A[i]=_H[i-1];
	seg.init(1, 1, N);

	vector<int> V;
	V.push_back(1);
	for(int i=2; i<=N; i++)
	{
		if(V.size()%2)
		{
			if(A[V.back()]<A[i]) V.push_back(i);
			else V.back()=i;
		}
		else
		{
			if(A[V.back()]>A[i]) V.push_back(i);
			else V.back()=i;
		}
	}
	if(V.size()%2==0) V.pop_back();

	set<int> S;
	set<pii> S2;
	for(int i=0; i<V.size(); i++)
	{
		S.insert(V[i]);
		if(i%2==0) B[V[i]]=1;
		else B[V[i]]=2;
	}
	for(int i=0; i+1<V.size(); i++) S2.insert({abs(A[V[i+1]]-A[V[i]]), V[i]});
	
	while(!S2.empty())
	{
		auto [val, p] = *S2.begin(); S2.erase(S2.begin());
		auto it=S.find(p);
		int l=-1, r=-1;

		if(it!=S.begin())
		{
			int t1=*prev(it), t2=*it;
			S2.erase({abs(A[t1]-A[t2]), t1});
			l=t1;
		}
		if(next(next(it))!=S.end())
		{
			int t1=*next(it), t2=*next(next(it));
			S2.erase({abs(A[t1]-A[t2]), t1});
			r=t2;
		}
		if(l!=-1 && r!=-1) S2.insert({abs(A[r]-A[l]), l});

		P[*next(it)]=val; P[*it]=val;
		S.erase(*next(it));
		S.erase(it);
	}
}

int max_towers(int L, int R, int D) 
{
	L++; R++;
	vector<int> V;
	for(int i=L; i<=R; i++) if(P[i]>=D) V.push_back(i);
	for(int i=0; i+1<V.size(); i++)
	{
		//assert(abs(A[V[i]]-A[V[i+1]])>=D);
		//assert(B[V[i]]!=B[V[i+1]]);
	}
	if(V.empty()) return 1;

	int ans=V.size();
	if(B[V[0]]==2)
	{
		if(seg.query(1, 1, N, L, V[0]-1)<=A[V[0]]-D) ans++;
		else ans--;
	}
	if(B[V.back()]==2)
	{
		if(seg.query(1, 1, N, V.back()+1, R)<=A[V.back()]-D) ans++;
		else ans--;
	}
	return ans/2+1;
}

Compilation message

towers.cpp: In member function 'void SEG::init(int, int, int)':
towers.cpp:24:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   24 |   int mid=tl+tr>>1;
      |           ~~^~~
towers.cpp: In member function 'int SEG::query(int, int, int, int, int)':
towers.cpp:33:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   33 |   int mid=tl+tr>>1;
      |           ~~^~~
towers.cpp: In function 'void init(int, std::vector<int>)':
towers.cpp:63:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |  for(int i=0; i<V.size(); i++)
      |               ~^~~~~~~~~
towers.cpp:69:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |  for(int i=0; i+1<V.size(); i++) S2.insert({abs(A[V[i+1]]-A[V[i]]), V[i]});
      |               ~~~^~~~~~~~~
towers.cpp: In function 'int max_towers(int, int, int)':
towers.cpp:102:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |  for(int i=0; i+1<V.size(); i++)
      |               ~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2512 ms 1488 KB Output is correct
2 Execution timed out 4048 ms 2488 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 464 KB Output is correct
3 Correct 1 ms 464 KB Output is correct
4 Correct 2 ms 464 KB Output is correct
5 Correct 2 ms 464 KB Output is correct
6 Correct 2 ms 464 KB Output is correct
7 Correct 2 ms 464 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 0 ms 208 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 0 ms 336 KB Output is correct
15 Correct 2 ms 464 KB Output is correct
16 Correct 2 ms 464 KB Output is correct
17 Correct 2 ms 464 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 0 ms 336 KB Output is correct
20 Correct 1 ms 464 KB Output is correct
21 Correct 2 ms 464 KB Output is correct
22 Correct 2 ms 464 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 464 KB Output is correct
27 Correct 1 ms 464 KB Output is correct
28 Correct 2 ms 464 KB Output is correct
29 Correct 2 ms 464 KB Output is correct
30 Correct 2 ms 464 KB Output is correct
31 Correct 2 ms 464 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 0 ms 336 KB Output is correct
35 Correct 0 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 464 KB Output is correct
3 Correct 1 ms 464 KB Output is correct
4 Correct 2 ms 464 KB Output is correct
5 Correct 2 ms 464 KB Output is correct
6 Correct 2 ms 464 KB Output is correct
7 Correct 2 ms 464 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 0 ms 208 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 0 ms 336 KB Output is correct
15 Correct 2 ms 464 KB Output is correct
16 Correct 2 ms 464 KB Output is correct
17 Correct 2 ms 464 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 0 ms 336 KB Output is correct
20 Correct 1 ms 464 KB Output is correct
21 Correct 2 ms 464 KB Output is correct
22 Correct 2 ms 464 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 464 KB Output is correct
27 Correct 1 ms 464 KB Output is correct
28 Correct 2 ms 464 KB Output is correct
29 Correct 2 ms 464 KB Output is correct
30 Correct 2 ms 464 KB Output is correct
31 Correct 2 ms 464 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 0 ms 336 KB Output is correct
35 Correct 0 ms 336 KB Output is correct
36 Correct 44 ms 6328 KB Output is correct
37 Correct 75 ms 9748 KB Output is correct
38 Correct 73 ms 9740 KB Output is correct
39 Correct 117 ms 12964 KB Output is correct
40 Correct 126 ms 13056 KB Output is correct
41 Correct 123 ms 13008 KB Output is correct
42 Correct 115 ms 12992 KB Output is correct
43 Correct 15 ms 2468 KB Output is correct
44 Correct 9 ms 2512 KB Output is correct
45 Correct 9 ms 2464 KB Output is correct
46 Correct 9 ms 2496 KB Output is correct
47 Correct 78 ms 9756 KB Output is correct
48 Correct 124 ms 13036 KB Output is correct
49 Correct 116 ms 13060 KB Output is correct
50 Correct 13 ms 2484 KB Output is correct
51 Correct 10 ms 2476 KB Output is correct
52 Correct 73 ms 9792 KB Output is correct
53 Correct 122 ms 12936 KB Output is correct
54 Correct 138 ms 12956 KB Output is correct
55 Correct 9 ms 2468 KB Output is correct
56 Correct 10 ms 2480 KB Output is correct
57 Correct 70 ms 9520 KB Output is correct
58 Correct 75 ms 9732 KB Output is correct
59 Correct 84 ms 9728 KB Output is correct
60 Correct 124 ms 12956 KB Output is correct
61 Correct 120 ms 12964 KB Output is correct
62 Correct 123 ms 12996 KB Output is correct
63 Correct 123 ms 12964 KB Output is correct
64 Correct 16 ms 2384 KB Output is correct
65 Correct 10 ms 2512 KB Output is correct
66 Correct 14 ms 2488 KB Output is correct
67 Correct 9 ms 2468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 4080 ms 9644 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3781 ms 2636 KB Output is correct
2 Execution timed out 4021 ms 9792 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 464 KB Output is correct
3 Correct 1 ms 464 KB Output is correct
4 Correct 2 ms 464 KB Output is correct
5 Correct 2 ms 464 KB Output is correct
6 Correct 2 ms 464 KB Output is correct
7 Correct 2 ms 464 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 0 ms 208 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 0 ms 336 KB Output is correct
15 Correct 2 ms 464 KB Output is correct
16 Correct 2 ms 464 KB Output is correct
17 Correct 2 ms 464 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 0 ms 336 KB Output is correct
20 Correct 1 ms 464 KB Output is correct
21 Correct 2 ms 464 KB Output is correct
22 Correct 2 ms 464 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 464 KB Output is correct
27 Correct 1 ms 464 KB Output is correct
28 Correct 2 ms 464 KB Output is correct
29 Correct 2 ms 464 KB Output is correct
30 Correct 2 ms 464 KB Output is correct
31 Correct 2 ms 464 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 0 ms 336 KB Output is correct
35 Correct 0 ms 336 KB Output is correct
36 Correct 44 ms 6328 KB Output is correct
37 Correct 75 ms 9748 KB Output is correct
38 Correct 73 ms 9740 KB Output is correct
39 Correct 117 ms 12964 KB Output is correct
40 Correct 126 ms 13056 KB Output is correct
41 Correct 123 ms 13008 KB Output is correct
42 Correct 115 ms 12992 KB Output is correct
43 Correct 15 ms 2468 KB Output is correct
44 Correct 9 ms 2512 KB Output is correct
45 Correct 9 ms 2464 KB Output is correct
46 Correct 9 ms 2496 KB Output is correct
47 Correct 78 ms 9756 KB Output is correct
48 Correct 124 ms 13036 KB Output is correct
49 Correct 116 ms 13060 KB Output is correct
50 Correct 13 ms 2484 KB Output is correct
51 Correct 10 ms 2476 KB Output is correct
52 Correct 73 ms 9792 KB Output is correct
53 Correct 122 ms 12936 KB Output is correct
54 Correct 138 ms 12956 KB Output is correct
55 Correct 9 ms 2468 KB Output is correct
56 Correct 10 ms 2480 KB Output is correct
57 Correct 70 ms 9520 KB Output is correct
58 Correct 75 ms 9732 KB Output is correct
59 Correct 84 ms 9728 KB Output is correct
60 Correct 124 ms 12956 KB Output is correct
61 Correct 120 ms 12964 KB Output is correct
62 Correct 123 ms 12996 KB Output is correct
63 Correct 123 ms 12964 KB Output is correct
64 Correct 16 ms 2384 KB Output is correct
65 Correct 10 ms 2512 KB Output is correct
66 Correct 14 ms 2488 KB Output is correct
67 Correct 9 ms 2468 KB Output is correct
68 Execution timed out 4080 ms 9644 KB Time limit exceeded
69 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2512 ms 1488 KB Output is correct
2 Execution timed out 4048 ms 2488 KB Time limit exceeded
3 Halted 0 ms 0 KB -