【P1161 开灯】TLE
查看原帖
【P1161 开灯】TLE
495670
鸭鸭吃香蕉吗楼主2021/3/22 18:56

题目地址

题目描述说是无限长的路上有路灯于是我就用了map替代数组,于是乎TLE了。为什么会超时呢?是不是map在插入数据的时候也会发生而外的耗时操作?

#include <iostream>
#include <map>
using namespace std;

map<int, bool> mp;

int main()
{
    double a;
    int t, n, tmp, p;
    cin >> n;
    
    for (int i = 0; i < n; i++)
    {
        cin >> a >> t;
        for (int j = 1; j <= t; j++)
        {           
            tmp = (int)(a * j); 
            mp[tmp];
            if (mp[tmp] == 1)
                mp[tmp] = 0;
            else
            {
                mp[tmp] = 1;                
            }
        }
    }
    for( map<int,bool>::iterator i = mp.begin();i!=mp.end();i++)
    {
        if(i->second )
        {p=i->first;break;}
    }
    cout<<p;
    return 0;
}
2021/3/22 18:56
加载中...