【求助】改了好久,看不出来为什么第七个WA了
查看原帖
【求助】改了好久,看不出来为什么第七个WA了
859183
ReverSouth楼主2022/11/23 00:45
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <bits/stdc++.h>
using namespace std;

long long int cutTree(long long int h,long long int N, long long int treeHigh[]);

int main() {
	long long int N, H;
	scanf("%lld %lld\n", &N, &H);
	long long int cutHigh = H, noteHigh = H / 2;
	long long int treeHigh[N];
	for (int i = 0; i < N; i++) {
		scanf("%lld ", &treeHigh[i]);
	}
	for (; noteHigh > 1; noteHigh /= 2) {
		long long int h1 = cutTree(cutHigh, N, treeHigh);
		if (h1 > H)
			cutHigh += noteHigh;
		else {
			cutHigh -= noteHigh;
		}
	}

    for (int i = 0; i < 5; i++) 
    {
		if ((cutTree(cutHigh, N, treeHigh) > H)) {
			cutHigh++;
		} else {
			break;
		}
	}

	for (int i = 0; i < 5; i++) {
		if ((cutTree(cutHigh, N, treeHigh) < H)) {
			cutHigh--;
		} else {
			break;
		}
	}
	printf("%lld", cutHigh);
	return 0;
}

long long int cutTree(long long int h, long long int N, long long int treeHigh[]) {
	long long int sum = 0;
	for (int i = 0; i < N; i++) {
		if (treeHigh[i] > h)
			sum += (treeHigh[i] - h);
	}
	return sum;
}
2022/11/23 00:45
加载中...