4.28招商fintech第一题 离散化差分

#include<iostream>
#include<vector>
#include <map>
#include <algorithm>
using namespace std;
const int N = 2010;
int a[N], b[N]; //差分数组a, 前缀和数组b
int n, t, res = N;
typedef pair<int, int> PII;
vector<int> all;//所有端点
map<int, int> alls;//离散化端点
vector<PII> diff;//所有区间端点对


int main()
{
    cin >> n;
    for (int i = 1; i <= n; i ++ ){
        int x, y;
        char c;
        cin >> c >> x;
        if(c == 'G') y = 1e9, diff.push_back({x,y});
        else y = 0, diff.push_back({y,x});
        all.push_back(x);
        all.push_back(y);
    }
    //排序加去重
    sort(all.begin(), all.end());
    all.erase(unique(all.begin(), all.end()), all.end()); 
    //map中离散化
    for(auto tmp : all){
        alls[tmp] = ++t;
    }
    //差分
    for(auto tmp : diff){
        int l = alls[tmp.first], r = alls[tmp.second]; 
        a[l] ++, a[r + 1] --;
    }
    //前缀和
    for (int i = 1; i <= alls.size(); i ++ ){
        b[i] = b[i - 1] + a[i];
        res = min(res, n - b[i]);
    }
    cout << res << endl;
    return 0;
}

全部评论

相关推荐

05-07 17:58
门头沟学院 Java
wuwuwuoow:1.简历字体有些怪怪的,用啥写的? 2.Redis 一主二从为什么能解决双写一致性? 3.乐观锁指的是 SQL 层面的库存判断?比如 stock > 0。个人认为这种不算乐观锁,更像是乐观锁的思想,写 SQL 避免不了悲观锁的 4.奖项证书如果不是 ACM,说实话没什么必要写 5.逻辑过期时间为什么能解决缓存击穿问题?逻辑过期指的是什么 其实也没什么多大要改的。海投吧
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客企业服务