货物种类

货物种类

https://ac.nowcoder.com/acm/contest/20960/1019

thinking process

use struct to store order info.

struct ty{
  int l, r, c;
}a[100010];

compare operator defination:

a.c == b.c ? a.l < b.l : a.c < b.c

the reason why we do this is simple. Given (l,r) and let u solve the individual item value, it's inevitable to be striked that merge intervals and difference. when the code is the same, and the ranges are crossed, we merge, if not we make difference. On the contrary, we directly merge and update the l,r,c.
ok, everything is ok. let's code!

#include<stdio.h>
#include<algorithm>
#include<math.h>
struct ty{
    int l, r, c;
}a[100010];

int delta[100010];
int kind[100010];

bool cmp(ty a, ty b){
    if(a.c != b.c) return a.c < b.c;
    return a.l < b.l;
}
using namespace std;
int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    for(int i = 0;i < m;i ++) {
        scanf("%d%d%d", &a[i].l, &a[i].r, &a[i].c);
    }
    sort(a, a + m, cmp);
    int l = a[0].l, r = a[0].r, c = a[0].c;
    for(int i = 1;i < m;i ++) {
        // order with the same code
        if(a[i].c == c) {
            // merge intervals
            if(a[i].l <= r) {
                r = max(r, a[i].r);
            }
            // not 
            else {
                ++ delta[l], -- delta[r + 1];
                l = a[i].l, r = a[i].r, c = a[i].c;
            }
        }
        // not
        else {
            ++ delta[l], -- delta[r + 1];
            l = a[i].l, r = a[i].r, c = a[i].c;
        }
    }
    ++ delta[l], -- delta[r + 1];
    for(int i = 1; i <= n ;i ++ ) kind[i] = kind[i - 1] + delta[i];
    int max = 0, pos = -1;
    for(int i = 0;i <= n;i ++ ) 
        if(max < kind[i]) 
            max = kind[i], pos = i;
    printf("%d", pos);
}
全部评论

相关推荐

面了100年面试不知...:被割穿了才想起来捞人了
投递哔哩哔哩等公司6个岗位
点赞 评论 收藏
分享
12-13 20:26
浙江大学 Java
淬月星辉:把浙大的校名加大加粗,把校徽再贴出来,就OK了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务