답안 #891137

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
891137 2023-12-22T08:23:34 Z vjudge1 Bitaro's travel (JOI23_travel) C++17
100 / 100
392 ms 4304 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;
	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=ub(all(v),b)-v.begin();
		if(r==n)r--;
		l=r-1;
		chmax(l,0ll);
		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;}
		while(l>=0 || r<n){
			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()-1;
				if(nl<0)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(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

travel.cpp: In function 'void fre(std::string)':
travel.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);}
      |                    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
travel.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 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 15 ms 1880 KB Output is correct
18 Correct 14 ms 1896 KB Output is correct
19 Correct 14 ms 1884 KB Output is correct
20 Correct 15 ms 1884 KB Output is correct
21 Correct 15 ms 1884 KB Output is correct
22 Correct 15 ms 1884 KB Output is correct
23 Correct 15 ms 1884 KB Output is correct
24 Correct 15 ms 1884 KB Output is correct
25 Correct 15 ms 1884 KB Output is correct
26 Correct 15 ms 1884 KB Output is correct
27 Correct 15 ms 1884 KB Output is correct
28 Correct 15 ms 1884 KB Output is correct
29 Correct 15 ms 1884 KB Output is correct
30 Correct 14 ms 1884 KB Output is correct
31 Correct 14 ms 2136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 339 ms 3412 KB Output is correct
8 Correct 343 ms 3312 KB Output is correct
9 Correct 340 ms 3412 KB Output is correct
10 Correct 340 ms 3408 KB Output is correct
11 Correct 348 ms 3592 KB Output is correct
12 Correct 339 ms 3368 KB Output is correct
13 Correct 29 ms 2388 KB Output is correct
14 Correct 21 ms 860 KB Output is correct
15 Correct 220 ms 3632 KB Output is correct
16 Correct 224 ms 3664 KB Output is correct
17 Correct 223 ms 3856 KB Output is correct
18 Correct 29 ms 2276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 15 ms 1880 KB Output is correct
18 Correct 14 ms 1896 KB Output is correct
19 Correct 14 ms 1884 KB Output is correct
20 Correct 15 ms 1884 KB Output is correct
21 Correct 15 ms 1884 KB Output is correct
22 Correct 15 ms 1884 KB Output is correct
23 Correct 15 ms 1884 KB Output is correct
24 Correct 15 ms 1884 KB Output is correct
25 Correct 15 ms 1884 KB Output is correct
26 Correct 15 ms 1884 KB Output is correct
27 Correct 15 ms 1884 KB Output is correct
28 Correct 15 ms 1884 KB Output is correct
29 Correct 15 ms 1884 KB Output is correct
30 Correct 14 ms 1884 KB Output is correct
31 Correct 14 ms 2136 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 339 ms 3412 KB Output is correct
39 Correct 343 ms 3312 KB Output is correct
40 Correct 340 ms 3412 KB Output is correct
41 Correct 340 ms 3408 KB Output is correct
42 Correct 348 ms 3592 KB Output is correct
43 Correct 339 ms 3368 KB Output is correct
44 Correct 29 ms 2388 KB Output is correct
45 Correct 21 ms 860 KB Output is correct
46 Correct 220 ms 3632 KB Output is correct
47 Correct 224 ms 3664 KB Output is correct
48 Correct 223 ms 3856 KB Output is correct
49 Correct 29 ms 2276 KB Output is correct
50 Correct 279 ms 4172 KB Output is correct
51 Correct 232 ms 4060 KB Output is correct
52 Correct 229 ms 4100 KB Output is correct
53 Correct 390 ms 4304 KB Output is correct
54 Correct 388 ms 4180 KB Output is correct
55 Correct 392 ms 4072 KB Output is correct
56 Correct 53 ms 3920 KB Output is correct
57 Correct 63 ms 4036 KB Output is correct
58 Correct 58 ms 4104 KB Output is correct