hdu 4393 Throw nails(STL之优先队列)

本文详细介绍了一种解决自行车竞赛中干扰者问题的算法。通过使用优先队列和双层循环,该算法能够准确地预测在每一轮比赛中,哪位选手会因干扰者的行动而被淘汰。文章提供了完整的代码实现,并解释了如何利用选手的速度和初始位置来计算其在比赛中的位置,进而确定被淘汰的选手。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description
The annual school bicycle contest started. ZL is a student in this school. He is so boring because he can't ride a bike!! So he decided to interfere with the contest. He has got the players' information by previous contest video. A player can run F meters the first second, and then can run S meters every second.
Each player has a single straight runway. And ZL will throw a nail every second end to the farthest player's runway. After the "BOOM", this player will be eliminated. If more then one players are NO.1, he always choose the player who has the smallest ID.

 

Input
In the first line there is an integer T (T <= 20), indicates the number of test cases.
In each case, the first line contains one integer n (1 <= n <= 50000), which is the number of the players. 
Then n lines follow, each contains two integers Fi(0 <= Fi <= 500), Si (0 < Si <= 100) of the ith player. Fi is the way can be run in first second and Si is the speed after one second .i is the player's ID start from 1.

 


Hint

Huge input, scanf is recommended.
Huge output, printf is recommended.
 

 

Output
For each case, the output in the first line is "Case #c:".
c is the case number start from 1.
The second line output n number, separated by a space. The ith number is the player's ID who will be eliminated in ith second end.

 

 

 

Sample Input
2
3
100 1
100 2
3 100
5
1 1
2 2
3 3
4 1
3 4

 

 

 

Sample Output
Case #1:
1 3 2
Case #2:
4 5 3 2 1

 


Hint
Hint The first case: 1st Second end Player1 100m (BOOM!!) Player2 100m Player3 3m 2nd Second end Player2 102m Player3 103m (BOOM!!) 3rd Second end Player2 104m (BOOM!!)
 

 

Source
 
 
这道题要用优先队列来解决。
首先定义一个优先队列,类型为Node,Node里面有两个参数f和index,f表示当前的距离,index表示下标。在Node里面排序(如果f大则优先,如果f相等则下标小的优先)
优先队列的数量100个就够了,因为s最大才100.
然后就是两个for循环,第一个for循环0~n-1,表示一共要淘汰n次,且这个循环的i可以表示当前时间的秒数供下面计算距离
第二个for循环j表示s,范围为1~100,条件是q[j].empty()==0.找出最大的距离。id表示s,如果有多个最大的距离相等,id表示最小的s。
输出printf("%d",q[id].top().index);然后q[id].pop();即可
有够详细了。
 1 #pragma comment(linker, "/STACK:1024000000,1024000000")
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<math.h>
 7 #include<algorithm>
 8 #include<queue>
 9 #include<set>
10 #include<bitset>
11 #include<map>
12 #include<vector>
13 #include<stdlib.h>
14 #include <stack>
15 using namespace std;
16 #define PI acos(-1.0)
17 #define max(a,b) (a) > (b) ? (a) : (b)
18 #define min(a,b) (a) < (b) ? (a) : (b)
19 #define ll long long
20 #define eps 1e-10
21 #define MOD 1000000007
22 #define N 50006
23 #define inf 1e12
24 int n;
25 struct Node{
26    int f,index;
27    friend bool operator < (Node a,Node b){
28       if(a.f!=b.f) return a.f<b.f;
29       return a.index>b.index;
30    }
31 };
32 int main()
33 {
34    int t;
35    int ac=0;
36    scanf("%d",&t);
37    while(t--){
38 
39       priority_queue<Node>q[106];
40 
41       scanf("%d",&n);
42       Node tmp;
43       int s;
44       for(int i=1;i<=n;i++){
45          scanf("%d%d",&tmp.f,&s);
46          tmp.index=i;
47          q[s].push(tmp);
48       }
49       printf("Case #%d:\n",++ac);
50       for(int i=0;i<n;i++){
51          int fast=-1,id=1;
52          for(int j=1;j<=100;j++){
53             if(!q[j].empty()){
54                Node cnt=q[j].top();
55                int now_dit=cnt.f+i*j;
56                if(now_dit>fast) fast=now_dit,id=j;
57                else if(now_dit==fast){
58                   if(q[id].top().index>cnt.index){
59                      id=j;
60                   }
61                }
62             }
63          }
64          printf("%d",q[id].top().index);
65          q[id].pop();
66          if(i!=n-1){
67             printf(" ");
68          }
69       }
70       printf("\n");
71 
72 
73 
74    }
75     return 0;
76 }
View Code

 

转载于:https://www.cnblogs.com/UniqueColor/p/4988928.html

内容概要:本文详细探讨了双馈风力发电机(DFIG)在Simulink环境下的建模方法及其在不同风速条件下的电流与电压波形特征。首先介绍了DFIG的基本原理,即定子直接接入电网,转子通过双向变流器连接电网的特点。接着阐述了Simulink模型的具体搭建步骤,包括风力机模型、传动系统模型、DFIG本体模型和变流器模型的建立。文中强调了变流器控制算法的重要性,特别是在应对风速变化时,通过实时调整转子侧的电压和电流,确保电流和电压波形的良好特性。此外,文章还讨论了模型中的关键技术和挑战,如转子电流环控制策略、低电压穿越性能、直流母线电压脉动等问题,并提供了具体的解决方案和技术细节。最终,通过对故障工况的仿真测试,验证了所建模型的有效性和优越性。 适用人群:从事风力发电研究的技术人员、高校相关专业师生、对电力电子控制系统感兴趣的工程技术人员。 使用场景及目标:适用于希望深入了解DFIG工作原理、掌握Simulink建模技能的研究人员;旨在帮助读者理解DFIG在不同风速条件下的动态响应机制,为优化风力发电系统的控制策略提供理论依据和技术支持。 其他说明:文章不仅提供了详细的理论解释,还附有大量Matlab/Simulink代码片段,便于读者进行实践操作。同时,针对一些常见问题给出了实用的调试技巧,有助于提高仿真的准确性和可靠性。
对于HDU4546问题,还可以使用优先队列(Priority Queue)来解决。以下是使用优先队列的解法思路: 1. 首先,将数组a进行排序,以便后续处理。 2. 创建一个优先队列(最小堆),用于存储组合之和的候选值。 3. 初始化优先队列,将初始情况(即前0个数的组合之和)加入队列。 4. 开始从1到n遍历数组a的元素,对于每个元素a[i],将当前队列中的所有候选值取出,分别加上a[i],然后再将加和的结果作为新的候选值加入队列。 5. 重复步骤4直到遍历完所有元素。 6. 当队列的大小超过k时,将队列中的最小值弹出。 7. 最后,队列中的所有候选值之和即为前k小的组合之和。 以下是使用优先队列解决HDU4546问题的代码示例: ```cpp #include <iostream> #include <vector> #include <queue> #include <functional> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); // 对数组a进行排序 priority_queue<long long, vector<long long>, greater<long long>> pq; // 最小堆 pq.push(0); // 初始情况,前0个数的组合之和为0 for (int i = 0; i < n; i++) { long long num = pq.top(); // 取出当前队列中的最小值 pq.pop(); for (int j = i + 1; j <= n; j++) { pq.push(num + a[i]); // 将所有加和结果作为新的候选值加入队列 num += a[i]; } if (pq.size() > k) { pq.pop(); // 当队列大小超过k时,弹出最小值 } } long long sum = 0; while (!pq.empty()) { sum += pq.top(); // 求队列中所有候选值之和 pq.pop(); } cout << sum << endl; return 0; } ``` 使用优先队列的方法可以有效地找到前k小的组合之和,时间复杂度为O(nklog(k))。希望这个解法对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值