1648B 1661C 1665C

1648B Integral Array

题意:

给定一个大小为 n n n的正整数数组,询问数组是否满足这样一个条件:对数组中任意两个元素 x , y x, y x,y ( x < y ) (x<y) (x<y), ⌊ x y ⌋ \lfloor \frac{x}{y} \rfloor yx也在数组中。数组中所有元素大小不超过 c c c
1 ≤ n ≤ 1 0 6 , 1 ≤ c ≤ 1 0 6 1 \le n \le 10^6, 1 \le c \le 10^6 1n106,1c106

题解:

⌊ x y ⌋ = r \lfloor \frac{x}{y} \rfloor = r yx=r,则 y ⋅ r ≤ x ≤ y ⋅ ( r + 1 ) y\cdot r \le x \le y \cdot (r+1) yrxy(r+1),当 r r r不在数组中的时候,答案为 n o no no。考虑 r r r不在数组中的时候,枚举 r , y r, y r,y,判断对应的 x x x是否在数组中。如果 x x x都在数组中,继续检查;如果不在数组中,答案为 n o no no。利用元素数量的前缀和可以在 O ( 1 ) O(1) O(1)的时间内检查 x x x
时间复杂度: O ( c l o g c ) O(clogc) O(clogc)

代码:
#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn = 1e6+10;
inline int read(){//读入优化 
	char ch;
	int sign = 1;
	while((ch=getchar())<'0'||ch>'9')
		if(ch == '-')
			sign = -1;
	int res = ch-48;
	while((ch=getchar())>='0'&&ch<='9')
		res = res*10+ch-48;
	return res*sign;
}
int n, c;
int cnt[maxn];
void solve(){
	memset(cnt, 0, sizeof(cnt));
	cin >> n >> c;
	int x;
	for(int i = 1; i <= n; i++){
		x = read();
		cnt[x]++;
	}
	for(int i = 1; i <= c; i++)
		cnt[i] += cnt[i-1];
	for(int i = 1; i <= c; i++){//r
		if(cnt[i] != cnt[i-1])
			continue;
		for(int j = 1; j*i <= c; j++){//y
			if(cnt[j] == cnt[j-1])
				continue;
			if(cnt[min(c, j*(i+1)-1)] != cnt[j*i-1]){
				printf("NO\n");
				return;
			}
		}			
	}
	printf("YES\n");
	return;
}

int main(){
	int t;
	cin >> t;
	while(t--)
		solve();
	return 0;
}

1661C Water the Trees

题意:

n n n棵树,每棵树的初始树高为 h i h_i hi。如果当前是奇数天,可以令其中一棵树的高度增加 1 1 1,偶数天可以令一棵树的高度增加 2 2 2。每一天可以执行对应的增加,也可以不增加。询问让所有树高度相同需要的最少天数。
1 ≤ n ≤ 3 ⋅ 1 0 5 , 1 ≤ h i ≤ 1 0 9 1 \le n \le 3\cdot 10^5,1 \le h_i \le 10^9 1n3105,1hi109

解析

设初始最高树的高度为 m a x h maxh maxh,最终树的高度一定为 m a x h maxh maxh m a x h + 1 maxh+1 maxh+1.
如果最终的高度为 m a x h + 2 maxh+2 maxh+2,显然可以用更少的时间让所有树的高度为 m a x h maxh maxh.
二分答案。对于时间t,奇数天为 ⌈ t 2 ⌉ \lceil \frac{t}{2} \rceil 2t,偶数天为 ⌊ t 2 ⌋ \lfloor \frac{t}{2}\rfloor 2t,让每棵树增长到最终的高度,判断时间是否足够。(开 longlong)
时间复杂度: O ( n l o g ) O(nlog) O(nlog)

代码
#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
#define int ll
const int maxn = 3e5+10; 
const int INF = 0X3F3F3F3F3F3F3F3F;
inline int read(){//读入优化 
	char ch;
	int sign = 1;
	while((ch=getchar())<'0'||ch>'9')
		if(ch == '-')
			sign = -1;
	int res = ch-48;
	while((ch=getchar())>='0'&&ch<='9')
		res = res*10+ch-48;
	return res*sign;
}
int h[maxn];
int ans, maxh, n;
int find(int x){
	int l = 0, r = INF;
	int res = 0;
	while(l <= r){
		int mid = (l+r) >> 1;
		int cnt1 = (mid+1)/2;
		int cnt2 = mid-cnt1;
		int num1 = 0, num2;
		for(int i = 1; i <= n; i++){
			int hh = h[i];
			num2 = (x-hh)/2;
			if(cnt2 >= num2){
				cnt2 -= num2;
			}
			else{
				num2 -= cnt2;
				cnt2 = 0;
				num1 += num2*2;
			}
			if((x-hh)%2)
				num1 ++;
		} 
		if(num1 <= cnt1){
			res = mid;
			r = mid-1;
		}
		else
			l = mid+1;
	}
	return res;
}
void solve(){
	ans = 0, maxh = 0;
	n = read();
	for(int i = 1; i <= n; i++){
		h[i] = read();
		maxh = max(h[i], maxh);
	}
	int a = find(maxh);
	int b = find(maxh+1);
	ans = min(a, b);
	cout << ans << endl;
	return;
}
signed main(){
	int t;
	cin >> t;
	while(t--)
		solve();
	return 0;
}

1665C Tree Infection

题意

有一棵 n n n节点的树,根节点是 1 1 1,初始的时候每个节点都是健康的。每一秒可以执行以下两个操作:

  • 感染:选择任意一个节点感染
  • 传播:对每个已感染的节点,将一个兄弟节点感染

询问将树上所有节点都感染需要的时间。 2 ≤ n ≤ 2 ⋅ 1 0 5 \quad 2 \le n \le 2\cdot10^5 2n2105

解析

将节点分组,每一组的节点有拥有相同的父亲。感性的看,规模越大的组,越早注射越好,尽可能的传播。
按照大小将组降序排列。然后依次在每个组中注射。然后模拟操作,每次选择健康节点数最多的组注射。

  • 将根节点单独分为一组

蒟蒻不会 O ( n ) O(n) O(n)的方法,只会 O ( n l o g n ) O(nlogn) O(nlogn)的方法

代码:
#include<iostream>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 2e5+10;
inline int read(){//读入优化 
	char ch;
	int sign = 1;
	while((ch=getchar())<'0'||ch>'9')
		if(ch == '-')
			sign = -1;
	int res = ch-48;
	while((ch=getchar())>='0'&&ch<='9')
		res = res*10+ch-48;
	return res*sign;
}
int a[maxn], b[maxn];
int n, ans, cnt;
priority_queue<int> q;
queue<int> qq;
bool cmp(int a, int b){
	return a > b; 
}
void init(){
	memset(a, 0, sizeof(a));
	memset(b, 0, sizeof(b));
	ans = 0;
	cnt = 0;
	while(!q.empty())
		q.pop();
}
void solve(){
	init();
	n = read();
	for(int i = 1; i < n; i++){
		int x = read();
		a[x]++;
		if(a[x] == 1)
			cnt++;
	}
	int tot = 0;
	a[0] = 1;
	for(int i = 0; i <= n; i++){
		if(a[i])
			b[++tot] = a[i];
	}
	sort(b+1, b+tot+1, cmp);
	for(int i = 1; i <= tot; i++){
		int d = tot-i+1;
		if(b[i] > d)
			qq.push(b[i]-d);	
	}
	ans += tot;
	while(!qq.empty()){
		ans++;
		int x;
		while(!qq.empty()){
			x = qq.front();
			qq.pop();
			if(x>1)
				q.push(x-1);
		}
		if(q.empty())
			break;
		x = q.top();
		q.pop();
		q.push(x-1);
		while(!q.empty()){
			x = q.top();
			q.pop();
			if(x)
				qq.push(x);
		}
	}
	cout << ans << endl;
	//cout << "ans = " << ans << endl;
}
int main(){
	int t;
	cin >> t;
	while(t--){
		solve();
	}		 
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值