《百度之星初赛三 * 补》

Intersection

思路:可以发现,如果出现了重复的情况,那么就会导致时间变化。

那么,当出现了重复情况时,我们就让左边的往上走到上面那层,这样对于第一个的时间,没有变化。

因为停在原地的话,和往上走一次都+1.所以时间一样。

但是向上走之后,对于下面的都可以向上1步,所以当发现碰撞时,都保持向上走,那么对于碰撞的这个,它的时间到达+1。

这里清理一个误区,可以发现左右是一直保持着不断运动的,因为碰撞时左边向上走,右边也会向右走,所以不存在改变相对位置的情况。

所以可以直接统计重复即可。

Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef long double ld;
typedef pair<int,int> pii;
const int N = 20;
const int M = 1e3+5;
const LL Mod = 2505;
#define pi acos(-1)
#define INF 1e8
#define INM INT_MIN
#define dbg(ax) cout << "now this num is " << ax << endl;
inline int read()
{
    int x = 0,f = 1;char c = getchar();
    while(c < '0' || c > '9'){if(c == '-') f = -1;c = getchar();}
    while(c >= '0' && c <= '9'){x = (x<<1)+(x<<3)+(c^48);c = getchar();}
    return x*f;
}
unordered_map<int,int> mp1,mp2;
vector<int> G[3];
int main()
{
    int ca;ca = read();
    while(ca--)
    {
        int n;n = read();
        G[1].clear(),G[2].clear();
        mp1.clear(),mp2.clear();
        for(int i = 1;i <= n;++i) 
        {
            int id,x;
            id = read(),x = read();
            int tim = x + (id == 1 ? 1 : 2);
            G[id].push_back(tim);
            if(id == 1) mp1[tim]++;
            else mp2[tim]++;
        }
        int ans = -1;
        for(auto v : G[1]) ans = max(ans,v+mp2[v]);
        for(auto v : G[2]) ans = max(ans,v+mp1[v]);
        printf("%d\n",ans);
    }
    system("pause");
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/zwjzwj/p/13383558.html