求助!只是更改了变量声明位置测评结果却不同
查看原帖
求助!只是更改了变量声明位置测评结果却不同
496340
Cangshu楼主2021/10/27 18:14
#include <bits/stdc++.h>
using namespace std;
#define maxn 1000010
long long n, m, a[maxn];
bool P (int x) {
	long long tot = 0;
	for (int i = 1; i <= n; i++) {
		if (a[i] > x) 
			tot += a[i] - x;
	}
	return tot >= m;
}
		
int main () {
	long long ans = 0, max;
	scanf ("%lld%lld", &n, &m);
	for (int i = 1; i <= n; i++) {
		scanf ("%lld", &a[i]);
		if (max <= a[i]) max = a[i];
	}
	long long L = 0, R = max + 1, mid;
	while (L <= R) {
		mid = (L + R) >> 1;
		if (P (mid) ) {
			ans = mid;
			L = mid + 1;
		}
		else
			R = mid - 1;
	} 
	cout << ans;
	return 0;
}

只是把ans和max的声明位置改了一下,就完全不一样。。。。上面的0分,有时候六七十,下面的AC。样例两个都能过

#include <bits/stdc++.h>
using namespace std;
#define maxn 1000010
long long n, m, a[maxn], mmax;
bool P (int x) {
	long long tot = 0;
	for (int i = 1; i <= n; i++) {
		if (a[i] > x) 
			tot += a[i] - x;
	}
	return tot >= m;
}
		
int main () {
	scanf ("%lld%lld", &n, &m);
	for (int i = 1; i <= n; i++) {
		scanf ("%lld", &a[i]);
		if (mmax <= a[i]) mmax = a[i];
	}
	long long L = 0, R = mmax + 1, mid, ans;
	while (L <= R) {
		mid = (L + R) >> 1;
		if (P (mid) ) {
			ans = mid;
			L = mid + 1;
		}
		else
			R = mid - 1;
	} 
	cout << ans;
	return 0;
}
		
2021/10/27 18:14
加载中...