前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ACMSGURU 133 - Border

ACMSGURU 133 - Border

作者头像
Reck Zhang
发布2021-08-11 10:50:58
3770
发布2021-08-11 10:50:58
举报
文章被收录于专栏:Reck Zhang

Friends of Friends

Problem Description

Along the border between states A and B there are N defence outposts. For every outpost k, the interval [Ak,Bk] which is guarded by it is known. Because of financial reasons, the president of country A decided that some of the outposts should be abandoned. In fact, all the redundant outposts will be abandoned. An outpost i is redundant if there exists some outpost j such that Aj<Ai and Bi<Bj. Your task is to find the number of redundant outposts.

Input

The first line of the input will contain the integer number N (1<=N<=16 000). N lines will follow, each of them containing 2 integers: Ak and Bk (0<= Ak < Bk <= 2 000 000 000), separated by blanks. All the numbers Ak will be different. All the numbers Bk will be different.

Output

You should print the number of redundant outposts.

Sample Input

代码语言:javascript
复制
5
0 10
2 9
3 8
1 15
6 11

Sample Output

代码语言:javascript
复制
3

Solution

代码语言:javascript
复制
#include <bits/stdc++.h>

int main() {
#define int long long
    std::ios::sync_with_stdio(false);

    int n;
    std::cin >> n;

    std::vector<std::pair<int, int>> outposts(n, std::pair<int, int>{});

    for(int i = 0; i < n; i++) {
        std::cin >> outposts[i].first >> outposts[i].second;
    }

    std::sort(outposts.begin(), outposts.end(), [](std::pair<int, int> a, std::pair<int, int> b) {
        return a.first < b.first;
    });

    int max_index = 0;
    int count = 0;
    for(int i = 0; i < outposts.size(); i++) {
        if(outposts[i].second > outposts[max_index].second) {
            max_index = i;
        }
        if(outposts[i].first > outposts[max_index].first && outposts[i].second < outposts[max_index].second) {
            count += 1;
        }
    }

    std::cout << count << std::endl;
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-12-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Friends of Friends
    • Problem Description
      • Input
        • Output
          • Sample Input
            • Sample Output
              • Solution
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档