1 条题解

  • 1
    @ 2021-11-19 10:24:03

    @ 的题解

    非常妙的一题。

    比较困难的地方在于,虫子撞来撞去,方向不断改变,比较难以考虑。但是因为虫子的无序的,完全相同,因此两个虫子相撞改变方向相当于直接穿了过去。或者你可以这样理解,每个虫子有标号,相撞后交换标号。


    因此一个虫子走出范围的时间就是到边上的时间加上会撞上的虫子数量,时间复杂度 Θ(n)\Theta(n)


    CODE
    #include <bits/stdc++.h>
    using namespace std;
    
    inline int read() {
        int x = 0, f = 0; char c = 0;
        while (!isdigit(c)) f |= c == '-', c = getchar();
        while (isdigit(c)) x = (x << 3) + (x << 1) + (c & 15), c = getchar();
        return f ? -x : x;
    }
    
    #define N 1000010
    int n, m, res = 0;
    pair<int, int> a[N];
    
    int main() {
    	n = read(), m = read();
    	for (int i = 1; i <= m; i ++) a[i].second = read();
    	for (int i = 1; i <= m; i ++) a[i].first = read();
    
    	sort(a + 1, a + m + 1);
    
    	for (int i = 1, now = 0; i <= m; i ++) {
    		if (a[i].second == 1) now ++;
    		else res = max(res, a[i].first + now);
    	}
    	for (int i = m, now = 0; i >= 1; i --) {
    		if (a[i].second == 0) now ++;
    		else res = max(res, n - a[i].first + 1 + now);
    	}
    	printf("%d\n", res);
    	return 0;
    }
    
    • 1

    信息

    ID
    47
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    4
    已通过
    3
    上传者