[Random Coding]Interval Related Questions

本文介绍了一种用于合并具有相同权重的相邻区间的算法。通过创建节点列表并对其进行排序,算法能够有效地跟踪权重的变化,并在权重发生变化时记录区间的开始和结束位置。最终,将连续且权重相同的区间进行合并,得到简化的区间列表。

对于每一个end point都生成了新的interval,所以最后一步要把相邻并且weight相同的interval合并。

本例中,res中依然存在可以合并的interval。

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.PriorityQueue;

public class WeightedInterval {
	public static class Interval {
		int start;
		int end;
		int weight;

		Interval(int start, int end, int weight) {
			this.start = start;
			this.end = end;
			this.weight = weight;
		}
	}

	public ArrayList<Interval> solve(ArrayList<Interval> input) {
		ArrayList<Node> nodes = new ArrayList<Node>();
		for (int i = 0; i < input.size(); i++) {
			nodes.add(new Node(input.get(i).start, true, input.get(i).weight));
			nodes.add(new Node(input.get(i).end, false, input.get(i).weight));
		}
		Comparator<Node> comp = new Comparator<Node>() {
			public int compare(Node node1, Node node2) {
				if (node1.pos > node2.pos)
					return 1;
				else if (node1.pos == node2.pos) {
					if (node1.start)
						return -1;
					else if (node2.start)
						return 1;
					else
						return -1;
				} else
					return -1;
			}
		};
		Collections.sort(nodes, comp);
		Comparator<Integer> heap_comp = new Comparator<Integer>() {
			public int compare(Integer i1, Integer i2) {
				return i2 - i1;
			}
		};
		PriorityQueue<Integer> max_heap = new PriorityQueue<Integer>(5,
				heap_comp);
		ArrayList<Interval> res = new ArrayList<Interval>();
		int i = 0;
		int last_pos = -1;
		while (i < nodes.size()) {
			Node cur = nodes.get(i);
			if (last_pos == -1 && cur.start) {
				max_heap.add(cur.weight);
				last_pos = cur.pos;
				i++;
				continue;
			}
			if (cur.start) {
				if (cur.pos != last_pos) {
					res.add(new Interval(last_pos, cur.pos, max_heap.peek()));
					last_pos = cur.pos;
				}
				max_heap.add(cur.weight);
			} else { // cur.start = false
				if (cur.pos != last_pos) {
					res.add(new Interval(last_pos, cur.pos, max_heap.peek()));
					// need to delete weight before deciding last_pos
					max_heap.remove(cur.weight);
					if (max_heap.isEmpty())
						last_pos = -1;
					else
						last_pos = cur.pos;
				} else
					max_heap.remove(cur.weight);
			}
			i++;
		}
		ArrayList<Interval> final_res = new ArrayList<Interval>();
		if (res.size() != 0) {
			Interval cur = res.get(0);
			for (int j = 1; j < res.size(); j++) {
				Interval next = res.get(j);
				if (cur.end == next.start && cur.weight == next.weight)
					cur.end = next.end;
				else {
					final_res.add(cur);
					cur = next;
				}
			}
			final_res.add(cur);
		}
		return final_res;
	}

	public static class Node {
		int pos;
		boolean start;
		int weight;

		Node(int pos, boolean start, int weight) {
			this.pos = pos;
			this.start = start;
			this.weight = weight;
		}
	}

	public static void main(String[] args) {
		ArrayList<Interval> input = new ArrayList<Interval>();
		input.add(new Interval(0, 1, 14));
		input.add(new Interval(0, 5, 17));
		input.add(new Interval(3, 7, 12));
		WeightedInterval test = new WeightedInterval();
		test.solve(input);
	}
}


内容概要:本文系统研究了直流微网中直流母线电压恢复的二次控制策略,重点提出并实现了基于虚拟压降补偿的方法在并联双向Buck-boost变换器中的应用。通过Simulink搭建详细的仿真模型,深入分析了虚拟压降原理及其在多变换器并联系统中的协调控制机制,有效解决了因线路阻抗差异导致的电压偏差与电流分配不均问题,实现了母线电压的精确调节与快速恢复,显著提升了系统的稳定性、均流性能与电能质量。研究涵盖了控制策略设计、关键参数整定及动态响应特性验证,提供了完整的仿真流程与结果分析。; 适合人群:具备电力电子、自动控制及微电网相关专业知识背景,熟悉Simulink仿真环境,从事新能源发电、直流配电系统、分布式能源控制等领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①深入理解直流微网中母线电压稳定与均流控制的关键技术;②掌握虚拟压降补偿在二次控制中的理论基础与实现方法;③构建并调试并联Buck-boost变换器的协同控制系统仿真模型,服务于学术研究、课程设计或实际工程项目开发; 阅读建议:学习过程中应结合Simulink模型细致剖析控制回路结构,重点关注虚拟阻抗参数对系统动态性能与鲁棒性的影响,建议通过改变负载工况、线路参数或增加变换器数量等方式进行对比仿真,以全面评估控制策略的有效性与适应性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值