WA不理解,求助uuuuuu…
查看原帖
WA不理解,求助uuuuuu…
300145
gloom_zlyc楼主2022/8/24 08:58

错误代码:

#include<bits/stdc++.h>
using namespace std;
int n,m,a[50005],ans;
int check(int k)
{
	int t=a[1],tot=0;
	for (int i=2;i<=n;i++)
	{
		if (a[i]-t<=k) t=a[i];
    	else t=a[i],tot++;
	}
	if (tot>m) return 0;
	return 1;
}
int main()
{
	cin>>n>>m;
	for (int i=1;i<=n;i++) cin>>a[i];
	sort(a+1,a+1+n);
	int l=1,r=a[n]-a[1],mid;
	while(l<=r)
	{
		mid=(l+r)/2;
		if (check(mid))
		{
			r=mid-1;
			ans=mid;
		}
		else l=mid+1;
	}
	cout<<ans<<endl;
}

错误点: 100 3 100 76 29 65 120 83 12 44 85 104 128 129 2 36 92 151 118 87 110 42 19 139 43 32 114 135 23 112 34 142 3 115 10 60 106 107 96 30 90 40 33 132 109 53 103 150 5 38 59 137 67 27 52 101 50 25 62 17 14 0 58 21 148 11 133 95 78 41 48 130 93 64 75 89 99 15 126 70 144 80 88 4 72 8 18 6 117 81 56 51 124 61 146 122 69 55 46 98 141 73 正确答案为25, 我却输出2。

2022/8/24 08:58
加载中...