답안 #891088

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
891088 2023-12-22T07:28:09 Z vjudge1 Bitaro's travel (JOI23_travel) C++17
45 / 100
345 ms 4324 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;}
		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 solve()':
travel.cpp:55:7: warning: unused variable 'j' [-Wunused-variable]
   55 |  ll i,j;
      |       ^
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 1 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 1 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 1 ms 600 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 0 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 1 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 1 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 1 ms 600 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 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 14 ms 1884 KB Output is correct
18 Correct 14 ms 1884 KB Output is correct
19 Correct 14 ms 1884 KB Output is correct
20 Correct 15 ms 1884 KB Output is correct
21 Correct 14 ms 2136 KB Output is correct
22 Correct 14 ms 1884 KB Output is correct
23 Correct 15 ms 1884 KB Output is correct
24 Correct 14 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 14 ms 1884 KB Output is correct
30 Correct 14 ms 1880 KB Output is correct
31 Correct 15 ms 2136 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 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 337 ms 3248 KB Output is correct
8 Correct 341 ms 3384 KB Output is correct
9 Correct 345 ms 3344 KB Output is correct
10 Correct 341 ms 3408 KB Output is correct
11 Correct 342 ms 3412 KB Output is correct
12 Correct 340 ms 3412 KB Output is correct
13 Correct 29 ms 2388 KB Output is correct
14 Correct 21 ms 856 KB Output is correct
15 Correct 220 ms 3664 KB Output is correct
16 Correct 220 ms 3740 KB Output is correct
17 Correct 216 ms 3820 KB Output is correct
18 Correct 33 ms 2132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 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 1 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 1 ms 600 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 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 14 ms 1884 KB Output is correct
18 Correct 14 ms 1884 KB Output is correct
19 Correct 14 ms 1884 KB Output is correct
20 Correct 15 ms 1884 KB Output is correct
21 Correct 14 ms 2136 KB Output is correct
22 Correct 14 ms 1884 KB Output is correct
23 Correct 15 ms 1884 KB Output is correct
24 Correct 14 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 14 ms 1884 KB Output is correct
30 Correct 14 ms 1880 KB Output is correct
31 Correct 15 ms 2136 KB Output is correct
32 Correct 0 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 344 KB Output is correct
36 Correct 0 ms 344 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 337 ms 3248 KB Output is correct
39 Correct 341 ms 3384 KB Output is correct
40 Correct 345 ms 3344 KB Output is correct
41 Correct 341 ms 3408 KB Output is correct
42 Correct 342 ms 3412 KB Output is correct
43 Correct 340 ms 3412 KB Output is correct
44 Correct 29 ms 2388 KB Output is correct
45 Correct 21 ms 856 KB Output is correct
46 Correct 220 ms 3664 KB Output is correct
47 Correct 220 ms 3740 KB Output is correct
48 Correct 216 ms 3820 KB Output is correct
49 Correct 33 ms 2132 KB Output is correct
50 Correct 282 ms 4324 KB Output is correct
51 Correct 229 ms 3980 KB Output is correct
52 Incorrect 226 ms 4180 KB Output isn't correct
53 Halted 0 ms 0 KB -