猜数字游戏(引入)
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
// 初始化随机数生成器
srand(time(0));
int lower = 1, upper = 100; // 设置猜数字的范围
int number = rand() % (upper - lower + 1) + lower; // 生成一个随机数
int guess, attempts = 0; // 初始化猜测次数
cout << "欢迎来到猜数字游戏!" << endl;
cout << "我想了一个数字,范围是 " << lower << " 到 " << upper << " 之间。" << endl;
// 游戏开始,直到猜对为止
do {
cout << "请输入你的猜测:";
cin >> guess; // 获取用户输入
attempts++; // 增加猜测次数
if (guess > number) {
cout << "你猜的数字太大了!" << endl;
} else if (guess < number) {
cout << "你猜的数字太小了!" << endl;
} else {
cout << "恭喜你,猜对了!" << endl;
cout << "你用了 " << attempts << " 次尝试!" << endl;
}
} while (guess != number); // 直到猜对为止
return 0;
}
整数二分(模板题)
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int a[N];
int n, q;
int main() {
scanf("%d %d", &n, &q); // 输入n和q
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
while (q--) {
int op, l, r, x;
scanf("%d %d %d %d", &op, &l, &r, &x);
if (op == 1) {
// 查找等于x最左边的位置
while (l < r) {
int mid = l + r >> 1;
if (x <= a[mid]) {
r = mid;
} else {
l = mid + 1;
}
}
if (a[l] == x) {
printf("%d\n", l);
} else {
printf("%d\n", -1);
}
} else if (op == 2) {// 查找等于x最右边的位置
while (l < r) {
int mid = (l + r + 1) >> 1;
if (x < a[mid]) {
r = mid - 1;
} else {
l = mid;
}
}
if (a[l] == x) {
printf("%d\n", l);
} else {
printf("%d\n", -1);
}
} else if (op == 3) {
// 查找第一个不小于x的元素
if (a[r] < x) {
printf("-1\n");
continue;
}
int idx = lower_bound(a + l, a + r + 1, x) - a;
printf("%d\n", idx);
} else {
// 查找第一个大于x的元素
if (a[r] <= x) {
printf("-1\n");
continue;
}
int idx = upper_bound(a + l, a + r + 1, x) - a;
printf("%d\n", idx);
}
}
return 0;
}
浮点数二分(模板题)
如果题目要求保留 位小数,
eps一般设置成 。
#include <iostream>
using namespace std;
double check(double x,double y){
double sum=1;
for(int i=0;i<y;i++)
sum*=x;
return sum;
}
int main()
{
double x,y;
cin >> x >> y;
double l=0,r=10000;
while(r-l>1e-8)
{
double mid=(l+r)/2;
if(check(mid,y)>=x) r=mid;
else l=mid;
}
printf("%.6lf",l);
return 0;
}二分答案(模板题)
考虑利用小根堆,每次取出序列中最小元素再 ,时间复杂度为 。一般一秒钟约在 左右,本题 最大为 ,会超时
考虑依次枚举最小值,一一验证当前值是否可行,第一个不可行的数 即是最大值。时间复杂度为 。
注意到,随着最小值变大,需要消耗的 值越多,因此具有单调性关系,可以二分最小值,时间复杂度为 。需要注意,最坏情况下所有 均是最大值, 也是最大值,因此二分上界至少为 。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
ll k;
const int N=1e5+10;
ll a[N];
bool check(ll x,ll k){
for(int i=1;i<=n;i++){
if(a[i]-x<0){
k=k+(a[i]-x);
}
if(k<0) return false;
}
return true;
}
int main(){
scanf("%d %lld",&n,&k);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
ll l=1,r=(ll)(1e14);
while(l<r){
ll mid=l+r+1>>1;
if(check(mid,k)){
l=mid;
}else{
r=mid-1;
}
}
printf("%lld",l);
}
分巧克力
考虑从小向大依次枚举巧克力的高度,一一验证,时间复杂度为 。
注意到随着巧克力高度变高,分出来的块数变少,具有单调性,因此可以二分高度。时间复杂度为
#include<bits/stdc++.h>
using namespace std;
#define int long long //数据范围要求很大,将int改为long long,可以有效避免超出数据范围
const int N = 1e5+11;
int n,k;
int a[N];
int check(int hight)
{
int nume = 0;
for(int i =0;i <= n;i++)
{
nume += a[i] / hight;
}
return nume>=k;
}
signed main()
{
scanf("%lld %lld",&n,&k);
for(int i = 0;i <= n;i++)
{
scanf("%d",&a[i]);
}
int l = 0; //l代表左范围
int r = 1e9; //r代表右范围
while(l < r)
{
int mid = (l + r + 1)/2; //向下取整
if(check(mid)) //更改区间范围 进一步进行判断
{
l = mid;
}
else
{
r = mid - 1;
}
}
if(l == 0)
{
printf("-1");
}
else
{
printf("%lld",l);
}
return 0;
}尾0的数量
阶乘尾 ,其只有 和 可以贡献,而 的数量一定大于 。因此,只需要算 中有多少个数因子含有 即可。
很明显,随着 变大,其 的尾 数量是会变多的,因此可以直接二分答案。
注意边界,可以通过打表测出 个尾 的数约为 。
时间复杂度为
#include<bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const ll inf = 6e18;
ll cal(ll mid){
ll ans = 0;
while(mid){
ans += mid / 5;
mid /= 5;
}
return ans;
}
void solve()
{
ll x;
cin >> x;
ll l = 1, r = inf;
while(l < r){
ll mid = l + r >> 1;
if(cal(mid) >= x) r = mid;
else l = mid + 1;
}
if(cal(l) == x) cout << l << endl;
else cout << -1 << endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t = 1;
// cin >> t;
while(t --) solve();
return 0;
}端木将军的追击
二分 + 前缀和,枚举第一个山洞,二分第二个山洞即可,时间复杂度为 。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> PII;
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const int N = 200010;
int n, m, k;
LL a[N], b[N];
void solve(){
cin >> n >> m >> k;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
a[i] += a[i - 1];
}
for (int i = 1; i <= m; ++i) {
cin >> b[i];
b[i] += b[i - 1];
}
int ans = 0;
for (int i = 0; i <= n; ++i) {
if (a[i] > k) break;
LL res = k - a[i];
int x = upper_bound(b, b + m + 1, res) - b - 1;
ans = max(ans, i + x );
}
cout << ans << '\n';
}
int main(){
ios_base :: sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t = 1;
while (t--)
{
solve();
}
return 0;
}数列分段
二分+前缀和
题目要求将长度为 的正整数序列分为连续的 段并且使得每段最大的值最小。
首先观察美味值的公式 ,并注意到序列中的元素为正整数,所以如果一个区间向左右两个方向扩张,那么它的值是单调递增的。
值可以用前缀和来求,例如 ,求区间 的值,暴力求法为 ,利用前缀和便是对于每个 ,我们可以 。
有这个性质的存在,我们可以在 的时间复杂度内判断出:如果给一个定值 ,能否把序列分成 段且使得每段的滑稽值不超过 。
同时,如果一个序列能够分成 段且使得每段的滑稽值不超过 ,那么它同样能分成 段且使得每段的滑稽值不超过 ,满足二分性。
对于 check 函数的编写,我们需要限定每个区间在小于等于 的情况下,是否能分成小于等于 段。
bool check(LL mid) {
int cnt = 1, last = 0;
LL range_sum = 0;
for (int i = 1; i <= n; i ++) {
if (range_sum + (LL)a[i] * (sum[i - 1] - sum[last]) > mid) {
if ( ++ cnt > m) return false;
range_sum = 0;
last = i - 1;
} else {
range_sum += (LL)a[i] * (sum[i - 1] - sum[last]);
}
}
if(cnt!=m) return false;
return true;
}最终做法为直接二分法答案,也就是二分找到最小的 ,使其能够满足把序列分成不超过 段且使得每段的美味值不超过 。
注意数据范围 ,,当 时,序列仅能分成 个区间,且滑稽值为 1,999,990,000,000,000,000 ,因此二分上界为 ,时间复杂度为 。
#include<cstdio>
typedef long long LL;
const int N = 200010;
int n, m;
int a[N];
LL sum[N];
bool check(LL mid) {
int cnt = 1, last = 0;
LL range_sum = 0;
for (int i = 1; i <= n; i ++) {
if (range_sum + (LL)a[i] * (sum[i - 1] - sum[last]) > mid) {
if ( ++ cnt > m) return false;
range_sum = 0;
last = i - 1;
} else {
range_sum += (LL)a[i] * (sum[i - 1] - sum[last]);
}
}
return true;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i ++) {
scanf("%d", &a[i]);
sum[i] = sum[i - 1] + a[i];
}
LL l = 0, r = 4e18;
while (l < r) {
LL mid = l + r >> 1;
if (check(mid)) r = mid;
else l = mid + 1;
}
printf("%lld\n", r);
return 0;
}第K个二元组
问题分析
直接暴力枚举去求解第 个二元组,复杂度非常高,因此我们肯定要考虑一些特殊的优化。
二元组性质分析
假设 ,我们依据题意列出如下二元组:
(1,1)
(1,2),(2,1)
(1,3),(2,2),(3,1)
(2,3),(3,2)
(3,3)假设 ,我们依据题意列出如下二元组:
(1,1)
(1,2),(2,1)
(1,3),(2,2),(3,1)
(1,4),(2,3),(3,2)(4,1)
(2,4),(3,3),(4,2)
(3,4),(4,3)
(4,4)我们可以发现,以 的和来分组,二元组的数量是一个先递增,后递减的等差数列。因此我们可以用公式 来求出元素数量。
求解第 个二元组的行号:
这里 为行号,关于 在第几行我们有如下方式去求解:
- 如果 ,则公式为 。
- 如果 ,则公式为 。
求解第 个二元组的列号:
这样我们可以用二分求解 ,便能求出 所在的行号,然后列号情况如下:
- 用 减去前面的所有元素,得出列号,我们设为 。
求解二元组
得出列号后,二元组的二个元素情况如下:
- 如果 ,则 。
- 如果 ,则 。
注意,在实际求解中,我们可以求解前 所在行的元素数量再加一,这样我们可以更方便的体现二分性。小于该元素数量便是 行,大于等于便是 行。
#include <bits/stdc++.h>
using namespace std;
long long n, k, N;
long long calc(long long mid) {
mid = mid - 1;
if (mid <= N) {
return mid + (mid) * (mid - 1) / 2 + 1;
}
return N + N * (N - 1) / 2 + ((N - 1) * (mid - N) - (mid - N) * (mid - N - 1) / 2) + 1;
}
void solve() {
cin >> N >> k;
long long l = 1, r = 2 * N - 1;
while (l < r) {
long long mid = (l + r + 1) / 2;
if (calc(mid) <= k) {
l = mid;
} else {
r = mid - 1;
}
}
n = l;
long long C = k - (calc(l) - 1);
if (n <= N) {
cout << C << " " << (n + 1 - C) << "\n";
} else {
cout << (n - N + C) << " " << (n + 1 - (n - N + C)) <<"\n";
}
}
int main() {
int t;
ios::sync_with_stdio(false);cin.tie(0);
cin >> t;
for (int zu = 1; zu <= t; zu++) {
solve();
}
return 0;
}
参加比赛
贪心+二分+前缀和
需要注意,最后一次比赛是不需要休息的,直接排序可能会出现问题。因此需要先排序,然后枚举最后一场比赛,然后再进行二分
首先对 进行排序
预处理出前缀和
s[i]=s[i-1]+a[i]+b[i]
对于当前枚举的最后一场项目,处理剩余时间 ,然后开始二分,如果二分的位置小于当前项目,则总时间为 ,将其与 做比较,如果位置大于当前项目,需要减去 ,即将多计算的比赛时间与休息时间减去,再与 作比较。
最终,要是二分出来的边界 ,其值比 小,需要 ,也就是包含本身。
#include <bits/stdc++.h>
using namespace std;
const int N = 4e5;
int n,k,ans;
long long s[N];
struct node {
int et,br;
}item[N];
bool cmp(const node &a, const node &b) {
return a.et + a.br <= b.et + b.br;
}
bool check(int x, int y,int time) {
if(x < y) {
if(s[x] <= time) return true;
else return false;
}
else {
if(x + 1 > n) return false;
if(s[x + 1] - item[y].et - item[y].br <= time) return true;
else return false;
}
}
int main() {
ios::sync_with_stdio(false),cin.tie(0);
cin>>n>>k;
for(int i = 1; i <= n; i ++) cin>>item[i].et;
for(int i = 1; i <= n; i ++) cin>>item[i].br;
sort(item+1,item+1+n,cmp);
for(int i = 1; i <= n; i ++) s[i] = s[i-1] + item[i].et + item[i].br;
for(int i = 1; i <= n; i ++) {
int temp = k;
if(temp < item[i].et) continue;
temp -= item[i].et;
int l = 0, r = n;
while(l + 1 != r) {
int mid = (l + r) >> 1;
if(check(mid,i,temp)) l = mid;
else r = mid;
}
ans = max(l+1,ans);
}
cout<<ans;
return 0;
}
COPY