跳到正文

个人记录

7.26

LLC6858约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年7月26日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 2 分钟

版本

  1. v1当前

T637845 快往上爬

分析:先按高度排序,再依次从低到高移动,并判断在过程中是否被水覆盖

C++
#include<bits/stdc++.h>
using namespace std;
int n,t,k,max1;
int a[100005];
int main(){
	cin >> t;
	while(t--){		
		cin >> n >> k;
		bool f = 0;
		int shui = 1;
		for(int i = 1;i <= n;i++){
			cin >> a[i];
		}
		int cur = a[k];
		sort(a + 1,a + 1 + n);
		for(int i = 1;i <= n;i++){
			if(cur >= a[i]) continue;//如果比当前高度小的塔,直接跳过 
			if(shui + (a[i] - cur-1) > cur){//如果当前水位加上跳走花费时间所长的水位大于水位涨上来的时间,注意-1,因为此时已经跳上去了 
				cout << "NO"<< '\n'; 
				f = 1;
				break;
			} 
			
			shui += a[i] - cur; 
			cur = a[i];
	
		}
		if(f == 0) cout << "YES" << '\n';
	}
 
	return 0;
}

T637434 小梦分糖果

分析:先进性排序,最小的两个数就是a[i] + a[i + 1],通过二分查找第一个>=最大值的位置,这个位置及其后面的所有数字都要修改

C++
#include<bits/stdc++.h>
using namespace std;
int t,n,a[100005];
int main(){
	cin >> t;
	while(t--){
		cin >> n;
		//long long sum = 0;
		for(int i = 1;i <= n;i++){
			cin >> a[i];	
		} 
		sort(a + 1,a + n + 1);
		long long cnt = 0,min1 = 1e9;
		for(int i = 1;i < n;i++){
			cnt = i - 1;//比a[i]小的数都要修改
			int pos = lower_bound(a + 1, a + 1 + n,a[i] + a[i + 1]) - a;//确定a[i] + a[i + 1]后需要修改的数量
			cnt += n - pos + 1;	
			min1 = min(min1,cnt);//最小的修改次数
		}
		cout << min1 << '\n';
	}
	


	return 0;
}
原文