Submission #387519

#TimeUsernameProblemLanguageResultExecution timeMemory
387519CaroLindaSafety (NOI18_safety)C++14
100 / 100
69 ms3744 KiB
#include <bits/stdc++.h>

#define ll long long
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define pb push_back
#define mk make_pair
#define pii pair<int,int>
#define sz(x) (int)(x.size())
#define mkt make_tuple
#define lp(i,a,b) for(int i = a ; i < b ; i++ )
#define debug printf

const int MAXN = 2e5+10 ;

using namespace std ;

int N ;
ll H , ans ;
priority_queue<ll> lef ;
priority_queue< ll , vector<ll>, greater<ll> > rig ;
ll lzLef , lzRig ;

int main()
{
	scanf("%d %lld", &N, &H ) ;

	for(int i = 1 ; i <= N ; i++ , lzLef -= H , lzRig += H )
	{
		ll x ;
		scanf("%lld", &x ) ;

		if(lef.empty()) 
		{
			assert(i == 1 ) ;
			lef.push(x) ;
			rig.push(x); 
		}
		else
		{
			if( x >= lef.top()+lzLef )
			{
				rig.push( x-lzRig ) ;
				rig.push( x-lzRig ) ;
				lef.push( rig.top()+lzRig-lzLef ) ;
				rig.pop() ;
				ans += abs(x-(lef.top()+lzLef) ) ;
			}
			else
			{
				lef.push(x-lzLef) ;
				lef.push( x-lzLef ) ;
				rig.push( lef.top()+lzLef-lzRig ) ;
				lef.pop() ;
				ans += abs( x-(rig.top()+lzRig) ) ;
			}
		}

	}

	printf("%lld\n" , ans ) ;
}

Compilation message (stderr)

safety.cpp: In function 'int main()':
safety.cpp:27:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   27 |  scanf("%d %lld", &N, &H ) ;
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~
safety.cpp:32:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   32 |   scanf("%lld", &x ) ;
      |   ~~~~~^~~~~~~~~~~~~
#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...