寝癖頭の解法

学習中の覚え書きを投稿、更新していきます。

AtCoder Problems in C++ #C - 2D Plane 2N Points

AtCoder Beginner Contestの過去問から、その提出コードの解答例です。
AtCoderとは、コンテストを通じて、プログラミングやアルゴリズムを学習するサービスです。
atcoder.jp
プログラミングコンテストとは、「与えられた問題をいかに素早く、正確に」解くことができるかを競うものです。
競技プログラミング」を略して、「競プロ」などと呼ばれています。

#C - 2D Plane 2N Points
atcoder.jp

僕が作成、提出したコードは、以下のとおりです。

/*
AtCoder Problems in C++
#C - 2D Plane 2N Points
https://atcoder.jp/contests/abc091/tasks/arc092_a
提出コードの解答例
https://neguse-atama.hatenablog.com
*/
#include<bits/stdc++.h>
using namespace std;
using P=pair<int,int>;
int main(void){
  int n;
  cin>>n;
  vector<P> red,blue;
  for(int i=0;i<n;i++){
    int a,b;
    cin>>a>>b;
    red.push_back({a,b});
  }
  for(int i=0;i<n;i++){
    int c,d;
    cin>>c>>d;
    blue.push_back({c,d});
  }
  sort(blue.begin(),blue.end());
  vector<bool> tf(n,false);
  int cnt=0;
  for(int i=0;i<n;i++){
    int bx=blue[i].first;
    int by=blue[i].second;
    int y=-1000;
    int x=-1;
    for(int j=0;j<n;j++){
      if(tf[j]){
        continue;
      }
      int rx=red[j].first;
      int ry=red[j].second;
      if(bx<rx){
        continue;
      }
      if(by<ry){
        continue;
      }
      if(y<ry){
        x=j;
        y=ry;
      }
    }
    if(x!=-1){
      ++cnt;
      tf[x]=true;
    }
  }
  cout<<cnt<<endl;
  return 0;
}

AtCoder Beginner Contestは、オンラインジャッジによるプログラミングコンテストです。
日本語と英語に対応していて、週末ごとに実施されているみたいです。
https://practice.contest.atcoder.jp/tutorial
アカウントを登録すれば、誰でも参加できます。