POJ 2182 Lost Cows 【树状数组+二分】

本文介绍了一道经典的排序问题“失序牛群”,通过两种算法解决:一是逆向遍历并使用标记排除法确定每头牛的位置;二是利用树状数组与二分查找高效确定牛的位置。

题目链接:http://poj.org/problem?id=2182

Lost Cows

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12736 Accepted: 8168

Description

N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular display of poor judgment, they visited the neighborhood 'watering hole' and drank a few too many beers before dinner. When it was time to line up for their evening meal, they did not line up in the required ascending numerical order of their brands. 

Regrettably, FJ does not have a way to sort them. Furthermore, he's not very good at observing problems. Instead of writing down each cow's brand, he determined a rather silly statistic: For each cow in line, he knows the number of cows that precede that cow in line that do, in fact, have smaller brands than that cow. 

Given this data, tell FJ the exact ordering of the cows. 

Input

* Line 1: A single integer, N 

* Lines 2..N: These N-1 lines describe the number of cows that precede a given cow in line and have brands smaller than that cow. Of course, no cows precede the first cow in line, so she is not listed. Line 2 of the input describes the number of preceding cows whose brands are smaller than the cow in slot #2; line 3 describes the number of preceding cows whose brands are smaller than the cow in slot #3; and so on. 

Output

* Lines 1..N: Each of the N lines of output tells the brand of a cow in line. Line #1 of the output tells the brand of the first cow in line; line 2 tells the brand of the second cow; and so on.

Sample Input

5
1
2
1
0

Sample Output

2
4
5
3
1

Source

 

题意概括:

有 N 头牛跑散了位置,现在我们可以依次知道他们前面有几头原来编号比他们原来编号小的牛,求他们原来的编号是多少。

解题思路:

①可以直接逆推暴力,因为草稿模拟一下可以知道,我们可以求最后一头牛他的编号是多少(即 F[ N ] + 1,F[ N ] 为他前面有多少头序号比他小的牛,并且他后面没有牛了,所以可以从最小的1开始逆推)。推出了最后一个,接下来可以推出倒数第二个,这里需要用到一个 visi [ x ] 来标记 编号X 是否已经被用;已经被用的编号可以丢掉不考虑了。推倒数第二个的推法跟推倒数第一个的一样,只不过有些比它小的编号已经被用掉了,只需要考虑那些还没用的编号,从这些编号里的最小编号开始往前逆推。所以双重循环可以搞定全部的牛牛了。

②树状数组+二分

树状数组的 SUM( X ) 用于记录 编号X 后面满足小于等于 X 的已经用掉了的编号的个数;F[ i ] 就是题目给出的 第 i 个牛 前面比 第i 个牛的编号小的编号的个数; 我们需要二分的就是 X,判断 X是不是当前第 i 头牛的编号。

如果 (X-1)-SUM( X - 1) == F[ i ] (即 编号X 前面剩下的小于 X 的编号的数量恰好等于 第 i 头牛编号的条件, 则 X 就是 第 i 头牛的编号啦)

如果(X-1)-SUM( X - 1)  > F[ i ]   (说明编号偏大咯)

如果(X-1)-SUM( X - 1) < F[ i ]      (说明编号偏小咯)

 

AC code:

 1 ///树状数组+二分
 2 #include <cstdio>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <cmath>
 6 #include <cstring>
 7 #include <map>
 8 #define ll long long int
 9 #define INf 0x3f3f3f3f
10 using namespace std;
11 
12 const int MAXN = 8e3+10;
13 int f[MAXN];
14 int b[MAXN];
15 int num[MAXN];
16 int N;
17 int lowbit(int x)
18 {
19     return x&(-x);
20 }
21 void add(int x, int value)
22 {
23     for(int i = x; i <= N; i+=lowbit(i))
24         b[i]+=value;
25 }
26 int sum(int x)                       ///后面比x小的数的个数
27 {
28     int res = 0;
29     for(int i = x; i > 0; i-=lowbit(i))
30         res+=b[i];
31     return res;
32 }
33 int main()
34 {
35     scanf("%d", &N);
36     num[1] = 0;
37     for(int i = 2; i <= N; i++)  ///前面比编号为i的数小的数的个数
38         scanf("%d", &f[i]);
39 
40     num[N] = f[N]+1;
41     add(num[N], 1);
42     for(int i = N-1; i > 0; i--)
43     {
44         int l = 1, r = N;
45         while(r > l)
46         {
47             int mid = (l+r)>>1;
48             if(mid-1-sum(mid) >= f[i])
49             {
50                 r = mid;
51             }
52             else
53             {
54                 l = mid+1;
55             }
56         }
57         num[i] = l;
58         add(num[i], 1);
59     }
60     for(int i = 1; i <= N; i++)
61         printf("%d\n", num[i]);
62     return 0;
63 
64 }
View Code

 

【直流微电网】径向直流微电网的状态空间建模与线性化:一种耦合DC-DC变换器状态空间平均模型的方法 (Matlab代码实现)内容概要:本文介绍了径向直流微电网的状态空间建模与线性化方法,重点提出了一种基于耦合DC-DC变换器状态空间平均模型的建模策略。该方法通过对系统中多个相互耦合的DC-DC变换器进行统一建模,构建出整个微电网的集中状态空间模型,并在此基础上实施线性化处理,便于后续的小信号分析与稳定性研究。文中详细阐述了建模过程中的关键步骤,包括电路拓扑分析、状态变量选取、平均化处理以及雅可比矩阵的推导,最终通过Matlab代码实现模型仿真验证,展示了该方法在动态响应分析和控制器设计中的有效性。; 适合人群:具备电力电子、自动控制理论基础,熟悉Matlab/Simulink仿真工具,从事微电网、新能源系统建模与控制研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①掌握直流微电网中多变换器系统的统一建模方法;②理解状态空间平均法在非线性电力电子系统中的应用;③实现系统线性化并用于稳定性分析与控制器设计;④通过Matlab代码复现和扩展模型,服务于科研仿真与教学实践。; 阅读建议:建议读者结合Matlab代码逐步理解建模流程,重点关注状态变量的选择与平均化处理的数学推导,同时可尝试修改系统参数或拓扑结构以加深对模型通用性和适应性的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值