답안 #829002

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
829002 2023-08-18T00:42:10 Z arnold518 송신탑 (IOI22_towers) C++17
0 / 100
4000 ms 9704 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++)
      |               ~~~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2496 ms 1488 KB Output is correct
2 Execution timed out 4053 ms 2480 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Runtime error 2 ms 848 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Runtime error 2 ms 848 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4077 ms 9704 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 17 ms 4528 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Runtime error 2 ms 848 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2496 ms 1488 KB Output is correct
2 Execution timed out 4053 ms 2480 KB Time limit exceeded
3 Halted 0 ms 0 KB -