sliding window leetcode 239
https://leetcode.com/problems/sliding-window-maximum/description/
1 | method 1: time complexity O(Nlog(k)) |
这里sliding window就是移动窗口的时候,每次把最左边的值pop掉,然后不断比较最右边的值与新插入的值,保证新插入的值是最好的candidate,那么窗口最左边的值就会是最大值。
Kattis
#include
#include
#include
#include
#include
#include
#include
using namespace std;
struct edge{
int from;
int to;
int weight;
};
typedef pair<int,int> ip;
#define INF 0x3f3f3f3f
void dijkstra(vector<vector
priority_queue<ip,vector<ip>,greater<ip>> q;
q.push({0,source});
dist[source] = 0;
while(!q.empty()){
auto top = q.top();
q.pop();
int u = top.second;
for(auto p:adj[u]){
int v = p.first;
int weight = p.second;
if(dist[v]==INF||dist[v]>dist[u]+weight){
dist[v] = dist[u]+weight;
q.push(make_pair(dist[v],v));
}
}
}
}
https://open.kattis.com/problems/flowerytrails
对于有多条最短路,并且求所有最短路的路径之和
1.分别对起始点和终点跑DIjkstra
2.枚举每一条初始点与终点分别到这条边距离,分别为d1,d2,如果d1+d2+w==shortestPath,则ans+=w;
1 | int main(){ |