Submission #676509

#TimeUsernameProblemLanguageResultExecution timeMemory
676509numberesSafety (NOI18_safety)C++17
100 / 100
60 ms6184 KiB
/**
 * @date    2022-12-31 10:53:37
 * @author  numberes
 * 煮不在乎.RAmen!
 * https://oj.uz/problem/view/NOI18_safety
 */
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define ll long long
using namespace std;
priority_queue<ll> l;
priority_queue<ll,vector<ll>,greater<ll> > r;
ll ls,rs;
int n,h,s[200005];
int main(){
	ls=rs=0;
	scanf("%d %d",&n,&h);
	ll res=0;
	for(int i=1;i<=n;i++){
		scanf("%d",&s[i]);
		if(i==1){
			l.push(s[i]);
			r.push(s[i]);
			continue;
		}
		ls-=h;rs+=h;
		if(s[i]<l.top()+ls){
			l.push(s[i]-ls);
			l.push(s[i]-ls);
			r.push(l.top()+ls-rs);
			res+=l.top()+ls-s[i];
			l.pop();
		}
		else if(s[i]>r.top()+rs){
			r.push(s[i]-rs);
			r.push(s[i]-rs);
			l.push(r.top()+rs-ls);
			res+=s[i]-r.top()-rs;
			r.pop();
		}
		else{
			l.push(s[i]-ls);
			r.push(s[i]-rs);
		}
	}
	printf("%lld\n",res);
    return 0;
}

Compilation message (stderr)

safety.cpp: In function 'int main()':
safety.cpp:21:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |  scanf("%d %d",&n,&h);
      |  ~~~~~^~~~~~~~~~~~~~~
safety.cpp:24:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   24 |   scanf("%d",&s[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...