跳到正文

个人记录

9.21下午河西-蒋 总结

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

文章信息

发布
2025年9月22日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 5 分钟

版本

  1. v1当前

总结

T1

错误代码

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main()
{
	int n;
	cin>>n;
	double l=0;
	double r=n;
	while(l+0.0000001<r)
	{
		double mid=(l+r)/2;
		if(mid*mid*mid>n)
		{
			r=mid;
		}
		else
		{
			l=mid;
		}
	}
	cout<<fixed<<setprecision(0);
	cout<<signed(l);
	return 0;
}

错误原因

由于在输出是使用了double强转int,可能会导致double是四舍五入的,不满足题目要求,应当用floor向下取整。

T2

错误代码

C++
#include<bits/stdc++.h>
using namespace std;
int a[200005];
int b[200005];
int n,l;
bool check(int x)
{
	int cnt=0;
	for(int i=1;i<=n;++i)
	{
		b[i]=0;
		if(a[i]>=x)
		{
			cnt++;
		}
		else
		{
			b[i]=x-a[i];
		}
	}
	sort(b+1,b+n+1);
	int lc=l;
	for(int i=1;i<=n;++i)
	{
		if(b[i]!=0 and lc>=b[i])
		{
			lc-=b[i];
			cnt++;
		}
	}
	return cnt>=x;
}
int main()
{
	cin>>n>>l;
	for(int i=1;i<=n;++i)
	{
		cin>>a[i];
	}
	sort(a+1,a+n+1);
	int l=0;
	int r=n+1;
	while(l+1<r)
	{
		int mid=(l+r)/2;
		if(check(mid))
		{
			l=mid;
		}
		else
		{
			r=mid;
		}
	}
	cout<<l;
	return 0;
}

错误原因

变量名重名!!

正确思路

首先,我们将a数组先排序一下,然后进行第一次二分,这次二分的作用是找到不引用自己文章前的最大的h指数,然后将后面m个a[i]++,最后在跑一次二分就可以找出答案。

T3

错误代码

C++
#include<bits/stdc++.h>
using namespace std;
int n,k;
char c[100005];
bool check(int x)
{
	int lc=0;
	int cnt=0;
	int ans=0;
	for(int i=1;i<=n;++i)
	{
		if(c[i]=='1')
		{
			if(cnt+(i-lc+1)<=x)
			{
				cnt+=(i-lc+1);
			}
			else
			{
				cnt=0;
				ans++;
			}
			lc=i;
		}
	}
	return ans<=k;
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n>>k;
		for(int i=1;i<=n;++i)
		{
			cin>>c[i];
		}
		int l=0;
		int r=n+1;
		while(l+1<r)
		{
			int mid=(l+r)/2;
			if(check(mid))
			{
				r=mid;
			}
			else
			{
				l=mid;
			}
		}
		cout<<r<<'\n';
	}
	return 0;
}

错误原因

不知为何,将check函数中每个i变成i+x-1后,统计出现1的个数后,就莫名对了。

正确思路

我们二分关灯的长度,因为长度越长,越容易成功,然后将check函数里统计每次i+x-1后1出现的次数,然后看看是否小于k即可

T4

错误代码

C++
#include<bits/stdc++.h>
using namespace std;
int a[100005];
int sum[100005];
int main()
{
	int n,l;
	cin>>n>>l;
	for(int i=1;i<=n;++i)
	{
		cin>>a[i];
		sum[i]=sum[i-1]+a[i];
	}
	int lc=0;
	int maxx=INT_MIN;
	for(int i=l;i<=n;++i)
	{
		lc++;
		double ans=sum[i]-sum[lc-1];
		maxx=max(maxx,int(double(ans/l)*1000));
	}
	cout<<maxx;
	return 0;
}

错误原因

贪心错误

正确思路

我们可以二分平均数,因为我们可以知道一个性质,那就是将a数组里面的所有数减去他们的平均数后的和为0,那么check函数里就是将前缀和数组加上a[i]-平均数,然后枚举所有长度为l的子串,然后计算值看看是否小于前面的最小值,如果是的就直接return 1,否则更新最小值,最后return 0。

原文