답안 #923199

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
923199 2024-02-06T20:33:49 Z denniskim Bitaro's travel (JOI23_travel) C++17
15 / 100
19 ms 1884 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){
			cout<<abs(b-v[0])<<endl;
			return;
		}
		if(l<0){
			cout<<abs(v.bc-b)<<endl;
			return;
		}
		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);}
      |                                                         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
travel.cpp: In function 'void solve()':
travel.cpp:74:3: warning: 'l' may be used uninitialized in this function [-Wmaybe-uninitialized]
   74 |   if(l<0){
      |   ^~
# 결과 실행 시간 메모리 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 1 ms 348 KB Output is correct
6 Correct 1 ms 344 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 344 KB Output is correct
11 Correct 1 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 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 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 1 ms 348 KB Output is correct
6 Correct 1 ms 344 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 344 KB Output is correct
11 Correct 1 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 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 15 ms 1884 KB Output is correct
18 Correct 15 ms 1884 KB Output is correct
19 Correct 15 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 1880 KB Output is correct
26 Correct 19 ms 1884 KB Output is correct
27 Correct 16 ms 1884 KB Output is correct
28 Correct 14 ms 1884 KB Output is correct
29 Correct 14 ms 1884 KB Output is correct
30 Correct 15 ms 1884 KB Output is correct
31 Correct 19 ms 1884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 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 Incorrect 13 ms 1884 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 1 ms 348 KB Output is correct
6 Correct 1 ms 344 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 344 KB Output is correct
11 Correct 1 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 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 15 ms 1884 KB Output is correct
18 Correct 15 ms 1884 KB Output is correct
19 Correct 15 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 1880 KB Output is correct
26 Correct 19 ms 1884 KB Output is correct
27 Correct 16 ms 1884 KB Output is correct
28 Correct 14 ms 1884 KB Output is correct
29 Correct 14 ms 1884 KB Output is correct
30 Correct 15 ms 1884 KB Output is correct
31 Correct 19 ms 1884 KB Output is correct
32 Correct 0 ms 344 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 Incorrect 13 ms 1884 KB Output isn't correct
39 Halted 0 ms 0 KB -