# | Problem | Pass Rate (passed user / total user) |
---|---|---|
11945 | Treasure Hunting |
|
13930 | I'm reference |
|
14299 | Field Trip |
|
Description
Niflheimr is playing Minecraft recently. He occupies a lot of towns, and there are many diamonds inside some of those towns.
Whenever he runs out of diamond, he has to figure out the nearest town with diamonds from his current location.
Doing this task every time waste a lot of time. He asks you to help him for the calculation. For every town, find the shortest distance to get diamonds from a nearby town which has diamonds. If the town itself has diamonds, the distance would be 0.
Hint: Use BFS (Breadth First Search) and std::queue.
If you are not familiar with BFS, you should take a look at the following article:
http://alrightchiu.github.io/SecondRound/graph-breadth-first-searchbfsguang-du-you-xian-sou-xun.html
[IMPORTANT] For those almost run out of time, maybe this note can help you figure out BFS and solve this problem faster!
You are provided with the following code:
#include <queue> #include <iostream> #include <vector> #include <algorithm> using namespace std; // G[i] is the neighbor towns of town i vector<int> diamondTowns; vector<int> G[100005]; int Dist[100005]; struct node { int id; int dist; node(int id, int l) { this->id = id; this->dist = l; } }; int main() { int N, M, K; cin >> N >> M >> K; for (int i = 0; i < M; i++) { int a, b; cin >> a >> b; G[a].push_back(b); G[b].push_back(a); } for (int i = 0; i < K; i++) { int x; cin >> x; diamondTowns.push_back(x); } fill(Dist, Dist+100005, -1); queue<node> Q; // [TODO] complete the task! // Output for (int i = 1; i <= N; i++) { cout << Dist[i] << '\n'; } return 0; }
You may take it as reference. You are not required to use this code, though.
Input
First line of input contains three integer N, M, K, denoting the number of towns, the amount of roads between towns and the number of towns which has diamond.
All the towns are numbered from 1 to N.
For the next M lines, each of them contains two integer a, b, meaning there is a road between a and b. All the roads are bidirectional, and the length of these roads are all 1.
The last line of input contains K distinct integers, denoting the towns which has diamonds.
It is guaranteed that:
- 1 <= K <= N <= 105
- 1 <= M <= min(106, N*(N+1)/2)
- All the towns can reach each other, i.e. the graph is connected.
Output
Output consists of N lines.
For i-th line, output the shortest distance Niflheimr has to travel from town i to get a diamond.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
下載後將副檔名改成zip(.cpp和.h為相同檔案,下載一個即可)
更改副檔名教學 :
打開任意資料夾 >> 點擊上方 "檢視" >> 勾選 "副檔名" >> 將.cpp或.h更改成.zip
解壓縮 >> 點進 reference 目錄 >> 點進 en 目錄 >> Main_Page.html 就可以看到完整的cpprefernce.com離線版
===================================================================================================
Download the partial judge code and change the extension to .zip. (.cpp and .h is the same file. Donwload one of them is enough.)
Open the folder with the file in it >> click "view" >> check "File name extensions" >> change .cpp or .h to .zip
Unzip the file >> click "reference" > click "en" >> open "Main_Page.html". Then you can use the offline version of cppreference.com.
Input
Output
Sample Input Download
Sample Output Download
Partial Judge Code
13930.cppPartial Judge Header
13930.hTags
Discuss
Description
Frank Lee's class is on a field trip, and there are N students standing in a line, numbered from 1 to N from left to right. Each student's height is represented by an integer, forming a sequence H of length N.
The teacher wants to know the difference between the maximum and minimum heights within any continuous subinterval of length K during the trip.
Your task is to help the teacher calculate and list these differences for all continuous subintervals of length K.
Input
- The first line contains two integers N and K, representing the total number of students and the length of the continuous subinterval.
- The second line contains N integers, representing the heights H of each student from left to right.
Constraints
- 1 ≤ Hi ≤ 109
- 1 ≤ K ≤ N ≤ 2 × 105
Output
- Output N−K+1 lines. The i-th line should contain a single integer, representing the difference between the maximum and minimum heights in the continuous subinterval [i, i+K-1].
Testcases
- Testcases 1, 2: guarantee that K ≤ 10 .
- Testcases 3, 4: guarantee the height of students is non-decreasing (from left to right).
- Testcases 5, 6: no additional constraint.
Hint
Think about how to quickly derive the answer for one interval from the answer of another interval using only a few insert and delete operations in std::set
or std::multiset
.