2293: Distribution Center 中南多校
Description
The factory of the Impractically Complicated Products Corporation has many manufacturing lines and the same number of corresponding storage rooms. The same number of conveyor lanes are laid out in parallel to transfer goods from manufacturing lines directly to the corresponding storage rooms. Now, they plan to install a number of robot arms here and there between pairs of adjacent conveyor lanes so that goods in one of the lanes can be picked up and released down on the other, and also in the opposite way. This should allow mixing up goods from different manufacturing lines to the storage rooms. Depending on the positions of robot arms, the goods from each of the manufacturing lines can only be delivered to some of the storage rooms. Your task is to find the number of manufacturing lines from which goods can be transferred to each of the storage rooms, given the number of conveyor lanes and positions of robot arms
Input
The input consists of a single test case, formatted as follows.
n m
x1 y1
.
.
.
xm ym
An integer n (2 ≤ n ≤ 200000) in the first line is the number of conveyor lanes. The lanes are numbered from 1 to n, and two lanes with their numbers differing with 1 are adjacent. All of them start from the position x = 0 and end at x = 100000. The other integer m (1 ≤ m < 100000) is the number of robot arms. The following m lines indicate the positions of the robot arms by two integers xi (0 < xi < 100000) and yi (1 ≤ yi < n). Here, xi is the x-coordinate of the i-th robot arm, which can pick goods on either the lane yi or the lane yi + 1 at position x = xi , and then release them on the other at the same x-coordinate. You can assume that positions of no two robot arms have the same x-coordinate, that is, xi
Output
Output n integers separated by a space in one line. The i-th integer is the number of the manufacturing lines from which the storage room connected to the conveyor lane i can accept goods.
Sample Input
4 3 1000 1 2000 2 3000 3
Sample Output
2 3 4 4
這個題目我覺得就是思維題,需要找規律,有兩個小規律,一個就是兩條生產線之間的物品可以交換,不過這個要按照x坐標排一下順序,只有一部分可以交換,這個自己仔細讀題吧。
還有就是無論哪條線它的物品的數值都是連續的,所以我們只要維護每一個位置的左右物品的數值就可以了。
這個規律我一直沒有發現,因為我在寫題的過程中默認把所有的物品當成一樣的了,??
如果標記一下,也許這個規律就發現了。。。。
#include <cstdio> #include <algorithm> #include <cstring> #include <iostream> using namespace std; const int maxn = 2e5 + 100; int l[maxn], r[maxn]; struct node { int x, y; node(int x=0,int y=0):x(x),y(y){} // bool operator<(const node&a)const // { // return this->x < a.x; // } }exa[maxn]; bool cmp(const node &a,const node&b) { return a.x < b.x; } int min_(int a,int b,int c) { int x = min(a, b); return min(x, c); } int max_(int a,int b,int c) { int x = max(a, b); return max(x, c); } int main() { int n, m; cin >> n >> m; for (int i = 1; i <= m; i++) scanf("%d%d", &exa[i].x, &exa[i].y); sort(exa + 1, exa + 1 + m,cmp); for(int i=1;i<=n;i++) { l[i] = i; r[i] = i; } for(int i=1;i<=m;i++) { int y = exa[i].y; l[y] = min_(l[y], l[y+1], y+1); r[y] = max_(r[y], y+1,r[y+1]); l[y + 1] = min_(l[y + 1], y, l[y]); r[y + 1] = max_(r[y + 1], y, r[y]); } for(int i=1;i<n;i++) { printf("%d ", r[i] - l[i] + 1); } printf("%d\n", r[n] - l[n] + 1); return 0; }
2293: Distribution Center 中南多校