Loading

【题解】ABC226-G - The baggage

不是很难的题,但值得一想。

显然大的方向是贪心,所以考虑当前最优决策,并验证是否为唯一最优决策。

显然 \(a_5\) 只能填到 \(b_5\),最先考虑。

其次 \(a_4\) 可以填到 \(b_4,b_5\),显然 \(a_4\to b_4\) 优先,如果没有 \(b_4\),那么我们需要且必须使用 \(b_5\),同时在 \(b_1\) 中加上剩余空间。

接着我们考虑 \(a_3\)​,同理 \(a_3 \to b_3\)​ 优先,然后考虑 \(b_4\)​ 和 \(b_5\)​,如果使用 \(b_4\)​,我们还得考虑是否会有 \(2\times a_2\to a_4\)​ 的情况,而如果使用 \(b_5\)​​,剩余的空间可以留在后面考虑。所以优先使用 \(b_5\) 更优。

对此笔者专门写代码验证了一下,如果先考虑 \(a_3 \to b_4\) 确实会挂掉。

对于 \(a_1\)\(a_2\),由于 \(a_1\) 是最小的单位,只要有空间就可以填,所以可以不用考虑顺序。

时间复杂度 \(\mathcal{O}(1)\)

LL a[6], b[6];

void calc(int x,int y){
	LL w = min(a[x], b[y]);
	a[x] -= w, b[y] -= w, b[y - x] += w;
}

bool solve(){
	rp(i, 5)a[i] = Read();
	rp(i, 5)b[i] = Read();
	calc(5, 5), 
	calc(4, 4), calc(4, 5), 
	calc(3, 3), calc(3, 5), calc(3, 4);
	pre(i, 5, 2)calc(2, i);
	pre(i, 5, 1)calc(1, i);
	rp(i, 5)if(a[i])return false;
	return true;
}

int main() {
	int T = read();
	while(T--)if(solve())Yes;else No;
	return 0;
}
posted @ 2021-11-10 20:14  7KByte  阅读(67)  评论(0编辑  收藏  举报