P7287 “EZEC-5”魔法
思路
乘法的威力很大,简单计算一下就会发现乘个 下就会爆 long long,因此我们可以枚举 的次数,二分 的次数。至于操作方面,显然每次都令 ,并且先加后乘是最优的。二分范围要注意。
ACCode
#include <stdio.h>
typedef __int128 i7;
template<class T>
inline T min(const T x, const T y) {
return x < y ? x : y;
}
template<class T>
inline T max(const T x, const T y) {
return y < x ? x : y;
}
char buf[1<<23],*p1,*p2;
#define getchar_unlocked() (p1==p2 && (p2=(p1=buf)+fread(buf,1,1<<21,stdin)),*p1++)
const int maxn = 2e5+5;
inline int nextI32() {
int x = 0; bool f = 0;
char c = getchar_unlocked();
while (c < '0' || c > '9') {
f |= (c == '-');
c = getchar_unlocked();
}
while (c >= '0' && c <= '9') {
x = (x<<1)+(x<<3)+(c&15);
c = getchar_unlocked();
}
return f ? -x : x;
}
void write(long long a) {
if (a >= 10) write(a/10);
putchar((a%10)^'0');
}
int GXR[maxn],smin,n,a,b;
long long tms = 1;
inline bool chk(const long long ao) {
long long ans = -9223372036854775808, sum = 0;
for (int i = 1; i <= n; ++i) {
sum = max(sum,0ll)+GXR[i]+ao;
ans = max(ans,sum);
}
return i7(ans)*tms >= smin;
}
int main() {
n = nextI32();
a = nextI32();
b = nextI32();
smin = nextI32();
for (int i = 1; i <= n; ++i)
GXR[i] = nextI32();
long long ans = 9223372036854775807;
for (int i = 0; i < 30; ++i) {
long long l = 0, r = 2147483647;
while (l < r) {
int mid = (l+r)>>1;
if (chk(mid)) r = mid;
else l = mid+1;
}
ans = min(ans,1ll*r*a+1ll*i*b);
tms <<= 1;
}
write(ans);
return 0;
} P14684【ICPC 2025 横浜 R】切豆腐
思路
显然二分,因为每个小块越大,整个豆腐切出来的块数就越小。所以我们考虑二分小块的边长,于是你会得到(设 mid 为豆腐块的边长):
...
mid = 1
mid = 1/2
mid = 1/4
mid = 1/8
mid = 1/16
...这样你可能永远二分不到正确答案,因为答案的分母不一定是 的整数次幂。
我们转换思路:小块最大时,整个豆腐必定有一条边是整整齐齐没有余留的残块的(否则你一定可以稍稍扩大小块的边长把大块豆腐的一条边填满),这启发我们二分切一条边上的块数(设切 块)。在 那条边上切割时,小块边长为 ,则另外两条边上切割的块数分别为
和
所以一共切割
块,二分条件是这坨东西不小于 。最后注意我们求的是 不是 。另外有个注意事项就是