P1114 “非常男女”计划(引入相对差值概念,通过计算相对差值来检索一段序列中两个数字相等的最大长度,时间复杂度(on)

目录

1.题目

2.题目分析

3.代码加注释。


1.题目

2.题目分析

  转化成通俗语言,0代表女,1代表男,我们要在这一段序列中找到长度最大的男女相同的子序列,也就是,0,1相同的子序列。

   那么,应该如何做呢?

  引入一个相对差的概念。


  什么是相对差?

  每一个位置我们都记录男女相对差值的大小,两个相对差值相同的端点之间,就是我们的最大长度。

9
0 1 0 0 0 1 1 0 0

  为什么呢?

  因为两个相对差之间经过了一系列变化最终又成为了那个相对差,这说明什么呢?说明之间的一系列变化全部抵消,也就是说这个区间之间男女相等。

  除此之外,我们要注意,不能更新0的左区间,为何,因为最开始就是最左边的0区间,我们要注意分开。

  再再再强调几点,我们还需要做什么呢?

  很关键的一点就是,我们要加上序列长度n防止出现负值。


3.代码加注释。

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int l[N];
int r[N];

int main() {
	int n;
	cin >> n;
	int cnt1 = 0;//统计男生到现在的人数。
	int cnt2 = 0;//统计女生到现在的人数
	for (int i = 1; i <= n; i++) {
		int x;
		cin >> x;//统计现在	是男的女的,如果是男的cnt1++反之,cnt2++;
		if (x == 1)
			cnt1++;
		else if (x == 0)
			cnt2++;
		int t = cnt2 - cnt1 + n;//计算差值。
		if (!l[t] && t != n)//如果最左端点没有被更新过,并且差值不为0(因为刚开始差值就是0
			l[t] = i;//更新左端点。
		else
			r[t] = i;//更新右端点
	}
	int ans = 0;
	for (int i = 0; i <= 2 * n; i++)
		ans = max(ans, r[i] - l[i]);
	cout << ans << "\n";
	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过以下步骤来实现Java多线程体重跟踪: 1. 定义一个包含体重数据的类,例如WeightData。 ```java public class WeightData { private double weight; public void setWeight(double weight) { this.weight = weight; } public double getWeight() { return weight; } } ``` 2. 创建一个继承Thread类的体重跟踪线程类,例如WeightTrackerThread。该线程类应该包含WeightData对象和人类对象作为属性。 ```java public class WeightTrackerThread extends Thread { private final WeightData weightData; private final Person person; public WeightTrackerThread(WeightData weightData, Person person) { this.weightData = weightData; this.person = person; } @Override public void run() { for (int i = 0; i < 30; i++) { // 模拟一个月时间 double weight = weightData.getWeight(); weight += person.getWeightGain() - person.getWeightLoss(); // 计算每天的体重变化 weightData.setWeight(weight); try { Thread.sleep(24 * 60 * 60 * 1000); // 每天更新一次体重数据 } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } } ``` 3. 创建一个人类对象,例如Person。该对象包含每天的食物和运动信息,并提供获取体重变化的方法。 ```java public class Person { private final String gender; private final double weight; private final double weightGain; private final double weightLoss; public Person(String gender, double weight, double weightGain, double weightLoss) { this.gender = gender; this.weight = weight; this.weightGain = weightGain; this.weightLoss = weightLoss; } public double getWeightGain() { // 根据每天的饮食和运动计算体重增加 return weightGain; } public double getWeightLoss() { // 根据每天的饮食和运动计算体重减少 return weightLoss; } } ``` 4. 在主线程,创建WeightData对象和两个Person对象,并启动两个WeightTrackerThread线程。 ```java public class Main { public static void main(String[] args) { WeightData weightData = new WeightData(); Person man = new Person("male", 70, 2500, 2000); WeightTrackerThread manThread = new WeightTrackerThread(weightData, man); manThread.start(); Person woman = new Person("female", 55, 2000, 1500); WeightTrackerThread womanThread = new WeightTrackerThread(weightData, woman); womanThread.start(); try { manThread.join(); womanThread.join(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } System.out.println("Man's weight after 1 month: " + man.getWeightGain() - man.getWeightLoss()); System.out.println("Woman's weight after 1 month: " + woman.getWeightGain() - woman.getWeightLoss()); System.out.println("Weight data after 1 month: " + weightData.getWeight()); } } ``` 在这个例子,我们创建了一男一女两个人类对象,模拟了每天的食物和运动情况,并通过WeightTrackerThread线程来更新体重数据。最后在主线程输出了每个人和整个月的体重数据。注意,由于男女的体重增加和减少可能不同,所以他们的最终体重也会不同。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值