답안 #1067376

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1067376 2024-08-20T16:00:53 Z TimDee Seesaw (JOI22_seesaw) C++17
100 / 100
59 ms 12268 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define forn(i,n) for(int i=0; i<n; ++i)
#define vii(a,n) vector<int> a(n); forn(i,n) cin>>a[i];
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()

const double eps = 1e-9;
void solve() {
	
	int n; cin>>n;
	vii(a,n);
	vector<pair<double,int>> v;
	int s=0; forn(i,n) s+=a[i];
	double c = 1.0*s/n;

	int l=0, r=n-1;
	while (l<=r) {
		int m=r-l+1;
		v.pb({1.0*s/m,m-1});
		if (l==r) break;
		double z = 1.0*(s-a[r])/(m-1);
		if (z>=c) {
			s-=a[r]; --r;
		} else {
			s-=a[l]; ++l;
		}
	}
	s=0, l=0, r=n-1; forn(i,n) s+=a[i];
	while (l<=r) {
		int m=r-l+1;
		v.pb({1.0*s/m,m-1});
		if (l==r) break;
		double z = 1.0*(s-a[l])/(m-1);
		if (z<=c) {
			s-=a[l]; ++l;
		} else {
			s-=a[r]; --r;
		}
	}

	sort(all(v));	
	//for(auto&x:v) cout<<'('<<x.f<<','<<x.s<<") ";
	vector<int> cnt(n);
	int tot=0;
	r=0;
	double ans=a[n-1]-a[0];
	forn(i,v.size()) {
		while (r<v.size() && tot<n) {
			tot+=!cnt[v[r].s];
			++cnt[v[r].s];
			++r;
		}
		if (tot==n) ans=min(ans,v[r-1].f - v[i].f);
		--cnt[v[i].s];
		tot-=!cnt[v[i].s];
	}
	cout<<fixed<<setprecision(10)<<ans<<'\n';

}

int32_t main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	solve();
}

Compilation message

seesaw.cpp: In function 'void solve()':
seesaw.cpp:5:33: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<double, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    5 | #define forn(i,n) for(int i=0; i<n; ++i)
......
   52 |  forn(i,v.size()) {
      |       ~~~~~~~~~~                 
seesaw.cpp:52:2: note: in expansion of macro 'forn'
   52 |  forn(i,v.size()) {
      |  ^~~~
seesaw.cpp:53:11: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<double, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |   while (r<v.size() && tot<n) {
      |          ~^~~~~~~~~
# 결과 실행 시간 메모리 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
# 결과 실행 시간 메모리 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 0 ms 348 KB Output is correct
7 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 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 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 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 52 ms 12268 KB Output is correct
13 Correct 59 ms 12232 KB Output is correct
14 Correct 59 ms 12248 KB Output is correct
15 Correct 53 ms 12228 KB Output is correct
16 Correct 59 ms 12108 KB Output is correct