답안 #343453

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
343453 2021-01-04T00:10:11 Z Gajowy Climbers (RMI18_climbers) C++14
25 / 100
800 ms 392556 KB
#pragma GCC optimize("Ofast")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define mp make_pair
#define eb emplace_back
#define pb push_back
#define e1 first
#define e2 second
#define size(x) (int)x.size()
#define all(r) begin(r),end(r)
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define time chrono::high_resolution_clock().now().time_since_epoch().count()
#define satori int testCases; cin>>testCases; while(testCases--)

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

///////////////////
#define DEBUG if(1)
///////////////////

const int MAXN=5e3+10;
const ll inf=1e18+2137;

bool used[MAXN*MAXN*2];
ll d[MAXN*MAXN*2];
int a[MAXN],n;
//priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>> pq;
stack<pair<ll,int>> pq;
bool cango(int pos,int lvl,int pos2,int lvl2){
	if(lvl2<a[pos2]){
		if(pos2==0)
			return false;
		if(a[pos2-1]>lvl2)
			return false;
		if(abs(pos-pos2)>1)
			return false;
		if(pos==pos2)
			return true;
		if(pos==pos2-1&&lvl>a[pos])
			return false;
		if(pos==pos2+1&&lvl<a[pos2])
			return false;
		return true;
	}
	else
	if(lvl2>a[pos2]){
		if(pos2==0)
			return false;
		if(a[pos2-1]<lvl2)
			return false;
		if(abs(pos-pos2)>1)
			return false;
		if(pos==pos2)
			return true;
		if(pos==pos2-1&&lvl<a[pos])
			return false;
		if(pos==pos2+1&&lvl>a[pos2])
			return false;
		return true;
	}
	else{
		if(abs(pos-pos2)>1)
			return false;
		if(pos==pos2)
			return true;
		if(pos2+1==pos)
			return true;
		if(pos2-1==pos){
			if(a[pos2-1]==a[pos2]){
				if(lvl!=lvl2)
					return false;
				return true;
			}
			else
			if(a[pos2-1]>a[pos2]){
				if(lvl>=a[pos2-1])
					return true;
				return false;
			}
			else{
				if(lvl<=a[pos2-1])
					return true;
				return false;
			}
		}
		return true;
	}
	return false;
}

void extend(int l,int r,int who,ll dist){
	int lvl=(who==0?a[l]:a[r]);
	for(int i=max(0,l-1);i<=min(n-1,l+1);i++)
		for(int j=max(0,r-1);j<=min(n-1,r+1);j++)
			for(int wh=0;wh<2;wh++)
				if(wh==0){
					if(cango(l,lvl,i,a[i])&&cango(r,lvl,j,a[i])){
						//if(l==r&&l==3&&i==2&&j==4&&wh==0)
							//cout<<"Xd"<<' '<<d[(n*r+l)*2+who]<<' '<<abs(lvl-a[i])<<' '<<d[(n*j+i)*2+wh]<<'\n';
						if(d[(n*r+l)*2+who]+abs(lvl-a[i])<d[(n*j+i)*2+wh]){
							d[(n*j+i)*2+wh]=d[(n*r+l)*2+who]+abs(lvl-a[i]),pq.push({d[(n*j+i)*2+wh],(n*j+i)*2+wh});
							//if(l==r&&l==3&&i==2&&j==4&&wh==0)
							//cout<<"Xd";

						}
					}
				}
				else{
					if(cango(l,lvl,i,a[j])&&cango(r,lvl,j,a[j])){
						if(d[(n*r+l)*2+who]+abs(lvl-a[j])<d[(n*j+i)*2+wh])
							d[(n*j+i)*2+wh]=d[(n*r+l)*2+who]+abs(lvl-a[j]),pq.push({d[(n*j+i)*2+wh],(n*j+i)*2+wh});
					}
				}
}

int32_t main(){
	fastio;
	cin>>n;
	for(int i=0;i<n;i++)
		cin>>a[i];
	fill(d,d+n*n*2+1,inf);
	for(int i=0;i<n;i++)
		pq.push({0,(n*i+i)*2}),d[(n*i+i)*2]=0;
	while(size(pq)){
		//if(pq.top().e2==(n*n-n)*2||pq.top().e2==(n*n-n)*2+1)
			//break;
		/*if(used[pq.top().e2]){
			pq.pop();
			continue;
		}
		else
			used[pq.top().e2]=true;*/
		ll dist=pq.top().e1;
		int who=pq.top().e2&1;
		int u=((pq.top().e2)/2)%n,v=((pq.top().e2)/2)/n;
		//cout<<u<<' '<<v<<' '<<who<<'\n';
		pq.pop();
		extend(u,v,who,dist);
	}
	if(d[(n*n-n)*2]==inf&&d[(n*n-n)*2+1]==inf)
		cout<<"NO\n";
	else
		cout<<min(d[(n*n-n)*2],d[(n*n-n)*2+1])<<'\n';
	//3 3 0 -> 2 4 0
	//cout<<cango(3,7,2,2)<<'\n';
	//cout<<cango(3,7,4,2)<<'\n';
	return 0;
}

Compilation message

climbers.cpp:2: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
    2 | #pragma GCC optimization ("O3")
      | 
climbers.cpp:3: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
    3 | #pragma GCC optimization ("unroll-loops")
      |
# 결과 실행 시간 메모리 Grader output
1 Correct 313 ms 620 KB Output is correct
2 Execution timed out 1090 ms 1772 KB Time limit exceeded
3 Execution timed out 1090 ms 15980 KB Time limit exceeded
4 Execution timed out 1094 ms 141292 KB Time limit exceeded
5 Execution timed out 1077 ms 392044 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 364 KB Output is correct
2 Correct 115 ms 492 KB Output is correct
3 Execution timed out 1086 ms 748 KB Time limit exceeded
4 Correct 387 ms 620 KB Output is correct
5 Execution timed out 1094 ms 2924 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Correct 375 ms 620 KB Output is correct
2 Execution timed out 1039 ms 2028 KB Time limit exceeded
3 Execution timed out 1045 ms 16364 KB Time limit exceeded
4 Execution timed out 1072 ms 142196 KB Time limit exceeded
5 Execution timed out 1104 ms 141548 KB Time limit exceeded
6 Execution timed out 1116 ms 253036 KB Time limit exceeded
7 Execution timed out 1118 ms 251244 KB Time limit exceeded
8 Execution timed out 1116 ms 392044 KB Time limit exceeded
9 Execution timed out 1088 ms 392428 KB Time limit exceeded
10 Execution timed out 1127 ms 392556 KB Time limit exceeded