답안 #891057

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
891057 2023-12-22T07:02:29 Z vjudge1 Jail (JOI22_jail) C++17
0 / 100
1 ms 348 KB
#include <bits/stdc++.h>
#define ll long long
#define str string
#define ins insert
#define ld long double
#define pb push_back
#define pf push_front
#define pof pop_front()
#define pob pop_back()
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
#define fr first
#define sc second
#define mpa make_pair
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define sz size()
#define bc back()
#define ar array
#define vll vector<ll> 
using namespace std;/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;*/
template <class _T>
bool chmin(_T &x, const _T &y){
    if(x>y){
        x=y;
        return true;
    }
    return false;
}
template <class _T>
bool chmax(_T &x, const _T &y){
    bool flag=false;
    if (x<y){
        x=y;flag|=true;
    }
    return flag;
}
//#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
void start(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}
const ll inf=2e18+7;
const ll mod=1e9+7;
const ll N=2e5+7;
const ld eps=1e-9;

void solve(){
	ll i,j;
	ll n,m;
	ll b;
	cin>>n;
	vector<ll> v(n);
	for(i=0;i<n;i++) cin>>v[i];
	cin>>m;
	for(i=0;i<m;i++){
		cin>>b;
		if(n==1){
			cout<<abs(b-v[0])<<endl;
			continue;
		}
		ll sum=0;
		ll l,r=lb(all(v),b)-v.begin();
		l=r-1;
		bool flag=true;
		if(abs(v[l]-b)<=abs(v[r]-b)) sum=abs(v[l]-b);
		else {sum=abs(v[r]-b);flag=false;}
		for(j=0;j<=35;j++){
			if(l==-1 && r==n)break;
			if(flag){
				if(r==n){
					sum+=v[l]-v[0];
					l=-1;
					
					continue;
				}
				if(l==0){
					sum+=v[r]-v[l];
					l=-1;
					flag=false;
					continue;
				}
				ll nl=ub(all(v),v[l]-(v[r]-v[l]))-v.begin();
				if(nl==l)nl--;
				if(v[nl+1]-v[nl]>v[r]-v[nl+1]){
					flag=false;
					sum+=v[r]-v[nl+1]+v[l]-v[nl+1];
					l=nl;
				}else{
					sum+=v[l]-v[nl];
					l=nl;
				}
			}else{
				if(l==-1){
					sum+=v[n-1]-v[r];
					r=n;
					continue;
				}
				if(r==n-1){
					sum+=v[r]-v[l];
					r=n;
					flag=true;
					continue;
				}
				ll nr=lb(all(v),v[r]+(v[r]-v[l]))-v.begin();
				if(nr==n)nr--;
				if(nr==r)nr++;
				if(v[nr]-v[nr-1]>=v[nr-1]-v[l]){
					flag=true;
					sum+=v[nr-1]-v[r]+v[nr-1]-v[l];
					r=nr;
				}else{
					sum+=v[nr]-v[r];
					r=nr;
				}
			}
		}
		cout<<sum<<endl;
	}
}
signed main(){
	start();
    ll t=1;
	//cin>>t;
    while(t--) solve();
    return 0;
}
/*
1
7
1 2
2 3
3 4
4 5
3 6
6 7
2
4 1
5 7






*/

Compilation message

jail.cpp: In function 'void fre(std::string)':
jail.cpp:43:27: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   43 | void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
      |                    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
jail.cpp:43:64: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   43 | void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
      |                                                         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -