跳到正文

题解

题解:P17003 [NWERC 2019] 极速魔方 / Expeditious Cubing

CuteGielHina约 5 分钟
原文
文章信息与版本

文章信息

收录
2026年7月11日
更新
2026年7月11日
篇幅
约 5 分钟
题目
P17003

版本

  1. v1当前

小分讨。

设她最后一次还原最多可以用 yy 秒,t1,t2,t3,t4t_1,t_2,t_3,t_4 按升序排序完后的结果为 a,b,c,da,b,c,d。

对于 yy 的取值分讨:

  1. y<ay < a:

    极端情况,计算最终成绩时 yy 会作为最小值,和最大值的 dd 一起被舍去,设此时最终成绩为 s1s_1,s1=a+b+c3s_1 = \frac{a+b+c}{3}。

  2. y>dy > d:

    也是极端情况,计算最终成绩时 yy 会作为最大值,和最小值 aa 一起被舍去,设此时最终成绩为 s2s_2,s2=b+c+d3s_2 = \frac{b+c+d}{3}。

看完上面两种情况后发现,如果计算最终成绩时 y<ay < a 或 y>dy > d,绝对会被舍去,因此若要考虑 yy 的取值,我们只需考虑 a<y<da < y < d 的情况。

继续讨论:

由于此时 aa 最小,dd 最大,因此我们只用考虑 b,c,tb,c,t。

显然的,由于总成绩最大为 t×3t \times 3,此时 y=t×3−b−cy=t \times 3 - b - c。

最后进行总结:

  • 若 s1>ts_1 > t,就证明 yy 取最小值也无法满足,直接输出 impossible。

  • 若 s2≤ts_2 \le t,就证明无论 yy 取何值都可以满足,直接输出 infinite。

  • 否则令 y=t×3−b−cy=t \times 3 - b - c,若 y<ay < a,使 y=ay = a;若 y>cy > c,使 y=cy = c,最后输出 yy。

这题做完了,复杂度为常数。

代码实现的时候要小心,这题卡 double 精度,要注意误差。

没能秒掉这题是我的问题。

C++
#include<bits/stdc++.h>
#define IL inline
#define ll long long
#define ri register int
#define F(i,a,b) for(ri i=a;i<=b;i++)
#define FF(i,a,b) for(ri i=b;i>=a;i--)
#define u64 uint64_t
#define ull unsigned long long
#define i128 __int128
#define vec vector
#define vi vector<int>
#define vll vector<ll>
#define vb vector<bool>
#define prq priority_queue
#define pii pair<int,int>
#define pill pair<int,ll>
#define plli pair<ll,int>
#define um unordered_map
#define mii map<int,int>
#define us unordered_set
#define pb(x) push_back(x)
#define fi first
#define se second
#define fr() front()
#define bk() back()
#define beg() begin()
#define Fill(a,b) memset(a,b,sizeof(a))
using namespace std;
const int N=1e6+5;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-9;
double t[N],teto,ans;
IL int read(){
	int k=0,f=1;
	char c=getchar_unlocked();
	while(c<'0'||c>'9'){
		if(c=='-') f=-1;
		c=getchar_unlocked();
	}
	while(c>='0'&&c<='9') k=k*10+c-'0',c=getchar_unlocked();
	return k*f;
}
IL void write(int x){
	if(x<0) putchar('-'),x=-x;
	if(x<10) putchar(x+'0');
	else write(x/10),putchar(x%10+'0');
}
IL bool cmp(double x,double y){return x<y;}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	F(i,1,4) scanf("%lf",&t[i]);
	scanf("%lf",&teto);
	sort(t+1,t+5,cmp);
	double mn=(t[1]+t[2]+t[3])/3.0,mx=(t[2]+t[3]+t[4])/3.0; 
	if(teto<mn-eps) puts("impossible");
	else if(mx<=teto+eps) puts("infinite");
	else{
		double y=teto*3.0-t[2]-t[3];
		y=max(y,t[1]+eps);
		y=min(y,t[4]-eps);
		printf("%.2lf",y);
	}
	return 0;
}
原文