Submission #146587

#TimeUsernameProblemLanguageResultExecution timeMemory
146587gs14004Safety (NOI18_safety)C++17
100 / 100
82 ms6292 KiB
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pi = pair<int, int>;
const int MAXN = 250005;

int n, h, x[MAXN], y[MAXN];

struct pque{
	bool neg;
	lint offset = 0;
	priority_queue<lint> pq;
	void push(lint x){
		if(neg) x = -x;
		pq.push(x - offset);
	}
	lint top(){
		lint x = pq.top() + offset;
		if(neg) x = -x;
		return x;
	}
	void pop(){
		pq.pop();
	}
	void add_offset(lint x){
		if(neg) x = -x;
		offset += x;
	}
}pql, pqr;

int main(){
	scanf("%d %d",&n,&h);
	lint ans = 0;
	pqr.neg = 1;
	for(int i=0; i<n; i++){
		scanf("%d",&x[i]);
		y[i] = h;
		if(i > 0){
			pql.add_offset(-y[i]);
			pqr.add_offset(y[i - 1]);
			if(pqr.top() < x[i]){
				ans += x[i] - pqr.top();
				pqr.push(x[i]);
				pqr.push(x[i]);
				pql.push(pqr.top());
				pqr.pop();
			}
			else if(pql.top() > x[i]){
				ans += pql.top() - x[i];
				pql.push(x[i]);
				pql.push(x[i]);
				pqr.push(pql.top());
				pql.pop();
			}
			else{
				pql.push(x[i]);
				pqr.push(x[i]);
			}
		}
		else{
			pql.push(x[i]);
			pqr.push(x[i]);
		}
	}
	cout << ans << endl;
}

Compilation message (stderr)

safety.cpp: In function 'int main()':
safety.cpp:32:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d",&n,&h);
  ~~~~~^~~~~~~~~~~~~~~
safety.cpp:36:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&x[i]);
   ~~~~~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...