Problem:
Activity Selection Problem
There are $n$ acitivities with start times $\{s_i\}$ and finish times $\{t_i\}$. Assuming that a person can only work on a single activity at a time, find the maximum number of activities that can be performed by a single person.
Input
$n$
$s_1$ $t_1$
$s_2$ $t_2$
:
$s_n$ $t_n$
The first line consists of the integer $n$. In the following $n$ lines, the start time $s_i$ and the finish time $t_i$ of the activity $i$ are given.
出力
Print the maximum number of activities in a line.
Constraints
$1 \le n \le 10^5$
$1 \le s_i \lt t_i \le 10^9 (1 \le i \le n)$
Sample Input 1
5
1 2
3 9
3 5
5 9
6 8
Sample Output 1
3
Sample Input 2
3
1 5
3 4
2 5
Sample Output 2
1
Sample Input 3
3
1 2
2 3
3 4
Sample Output 3
2

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
using namespace std;

typedef struct time{
    int s;
    int f;
}tym;

int main(){
    int n, c=1;
    tym t[100000];
    cin >> n;

    for(int i=0; i<n; i++){
        cin >> t[i].s >> t[i].f;
    }

    sort(t, t + n, [](const tym &a,const tym &b){
        return a.f < b.f;
    });

    int lf= t[0].f;
    for(int i=1; i<n; i++){
        if(t[i].s>lf){
            c++;
            lf=t[i].f;
        }
    }

    cout << c << endl;
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 4200
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;

struct Activity {
    int start;
    int finish;
};


bool compare(Activity a, Activity b) {
    return a.finish < b.finish;
}

int main() {
    int n;
    cin >> n;

    Activity activities[100000];
    for (int i = 0; i < n; i++) {
        cin >> activities[i].start >> activities[i].finish;
    }


    sort(activities, activities + n, compare);

    int count = 1;
    int j = 0;

    for (int i = 0; i < n; i++) {

        if (activities[i].start > activities[j].finish) {
            count++;
            j=i;
        }
    }

    cout << count << "\n";

    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 4136
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;

struct activity {
    string name;
    float s, t;
};

bool comp(activity act1, activity act2) {
    return (act1.t < act2.t);
}

int main() {
    int n;

    cin >> n;

    activity ar[n + 5];

    for (int i = 0; i < n; i++) {
        cin >>  ar[i].s >> ar[i].t;
    }

    sort(ar, ar + n, comp);

    int j = 0, cnt = 1;


    for (int i = 1; i < n; i++) {
        if (ar[i].s > ar[j].t) {
            cnt++;
            j = i;

        }
    }


    cout << cnt << endl;

    return 0;
}
accuracy: 0.68
time_ms: 80
memory_kb: 7224
final_rank: 18

code_snippet: #include<bits/stdc++.h>
using namespace std;
struct act
    {
        int s;
        int e;
    };
bool esot (act a,act b)
{
    if(a.e<b.e)
    return true;
    else
    return false;
}
int main()
{
    
    int t;
    cin>>t;
    act a[t+1];
    for (int i = 0; i < t; i++)
    {
        cin>>a[i].s>>a[i].e;
    }
    sort(a,a+t,esot);
    int select=1;
    int tim = a[0].e;
    int j=0;
    for (int i = 1; i < t; i++)
    {
        if(a[j].e<a[i].s)
        {
            select++;
            j = i;
        }
    }
    cout<< select<< endl;
    
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 4156
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;

struct Activity {
    double start;
    double end;
};

bool custom(Activity a, Activity b) {
    return a.end < b.end;
}

int main() {
    int n;
    cin >> n;

    Activity act[n];
    for (int i = 0; i < n; i++) {
        cin >> act[i].start >> act[i].end;
    }

    sort(act, act + n, custom);

    int count = 1;
    double last_end = act[0].end;

    for (int i = 1; i < n; i++){
        if (act[i].start > last_end){
            count++;
            last_end = act[i].end;
        }
    }

    cout << count << endl;

    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 5060
final_rank: 17

code_snippet: #include <stdio.h>
#include <stdlib.h>

// 活動の開始時刻と終了時刻を保持する構造体
typedef struct {
    int s; // 開始時刻
    int t; // 終了時刻
} Activity;

// qsortライブラリ関数で使うための比較関数
// 活動を終了時刻 `t` の昇順でソートする
int compare_activities(const void* a, const void* b) {
    Activity* act1 = (Activity*)a;
    Activity* act2 = (Activity*)b;
    
    if (act1->t < act2->t) {
        return -1;
    } else if (act1->t > act2->t) {
        return 1;
    }
    // 終了時刻が同じ場合は順序を問わない
    return 0;
}

int main(void) {
    int n;
    scanf("%d", &n);

    // 活動が0個の場合は0を出力して終了
    if (n == 0) {
        printf("0\n");
        return 0;
    }

    // n個の活動を格納するためのメモリを動的に確保
    Activity* activities = (Activity*)malloc(n * sizeof(Activity));
    if (activities == NULL) {
        // メモリ確保に失敗した場合
        return 1;
    }

    // n個の活動の開始時刻と終了時刻を読み込む
    for (int i = 0; i < n; i++) {
        scanf("%d %d", &activities[i].s, &activities[i].t);
    }

    // 活動を終了時刻の早い順にソートする
    qsort(activities, n, sizeof(Activity), compare_activities);

    int selected_count = 1; // 最初の活動は必ず選択する
    int last_finish_time = activities[0].t; // 最後に選択した活動の終了時刻

    // 2番目以降の活動を順にチェックする
    for (int i = 1; i < n; i++) {
        // 現在チェックしている活動の開始時刻が、
        // 最後に選んだ活動の終了時刻より後であれば、重ならないので選択できる
        if (activities[i].s > last_finish_time) {
            selected_count++;
            last_finish_time = activities[i].t; // 最後に選んだ活動の情報を更新
        }
    }

    // 選択できた活動の最大数を出力
    printf("%d\n", selected_count);

    // 確保したメモリを解放
    free(activities);

    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4212
final_rank: 4

code_snippet: #include<bits/stdc++.h>
using namespace std;
bool cmp(pair<int,int>a,pair<int,int>b)
{
    return a.second<b.second;
}
int main()
{
    int n;
    cin>>n;
    vector<pair<int,int>>v;
    for(int i=0;i<n;i++)
    {
        int start,end;
        cin>>start>>end;
        v.push_back(make_pair(start,end));
    }
    sort(v.begin(),v.end(),cmp);
    int count=1,finish=v[0].second;
    for(int i=1;i<n;i++)
    {
        if(finish<v[i].first)
        {
            count++;
            finish=v[i].second;
        }
    }
    cout<<count<<endl;
}
accuracy: 1
time_ms: 50
memory_kb: 4132
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;

    vector<pair<int, int>> activities(n);

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

    sort(activities.begin(), activities.end());

    int count = 0;
    int lastEnd = 0;

    for (auto act : activities) {
        int end = act.first;
        int start = act.second;

        if (start > lastEnd) { 
            count++;
            lastEnd = end;
        }
    }

    cout << count << endl;
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3908
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;

    vector<pair<int, int>> activities(n);

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

    sort(activities.begin(), activities.end());

    int count = 0;
    int lastEnd = 0;

    for (auto act : activities) {
        int end = act.first;
        int start = act.second;

        if (start > lastEnd) { // changed >= to >
            count++;
            lastEnd = end;
        }
    }

    cout << count << endl;
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3876
final_rank: 6

code_snippet: #include<bits/stdc++.h>
using namespace std;
bool cmp(pair<int,int>a,pair<int,int>b)
{
    return a.second<b.second;
}
int main()
{
    int n;
    cin>>n;
    vector<pair<int,int>>v;
    for(int i=0;i<n;i++)
    {
        int start,end;
        cin>>start>>end;
        v.push_back(make_pair(start,end));
    }
    sort(v.begin(),v.end(),cmp);
    int count=1,finish=v[0].second;
    for(int i=1;i<n;i++)
    {
        if(finish<v[i].first)
        {
            count++;
            finish=v[i].second;
        }
    }
    cout<<count<<endl;
}
accuracy: 1
time_ms: 50
memory_kb: 4132
final_rank: 12

code_snippet: #include<bits/stdc++.h>
using namespace std;
struct Act {
   double a, b;
};
bool comp(Act a1, Act a2){
   return(a1.b<a2.b);
}
 int main(){

     int n;
     cin>> n;
     Act arr[n] ;
     for(int i=0; i<n; i++){
        cin>>arr[i].a>> arr[i].b;

     }

     sort(arr, arr+n, comp);
     int j=0, cnt=1;
     for(int i=1; i<n; i++){
        if(arr[j].b<arr[i].a){
            cnt++;
            j=i;
        }
     }
         cout<<cnt <<endl;
 }
accuracy: 1
time_ms: 70
memory_kb: 4984
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    int n, count = 0, last_end = -1;
    cin >> n;

    vector<pair<int, int>> activities(n);
    for (int i = 0; i < n; ++i)
        cin >> activities[i].second >> activities[i].first;

    sort(activities.begin(), activities.end());

    for (auto& [end, start] : activities) if (start > last_end) {
        count++;
        last_end = end;
    }

    cout << count << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3868
final_rank: 1

code_snippet: #include <bits/stdc++.h>

using namespace std;

using ii = pair<int,int>;
using vii = vector<ii>  ;
using vi = vector<int>  ;
#define ln  '\n'
#define ll long long
#define pb push_back
#define fi first
#define se second
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define f(i, a, b)  for(ll i = (ll)a; i < (ll)b; i++)
#define fer(i, b, a)  for(ll i = (ll)a - 1; i >= (ll)b; i--)

void so(int test){
        int n;
        cin >> n;
        vii v(n+1);
        f(i,1,n+1){
                cin >> v[i].fi >> v[i].se;
        }

        sort(all(v));

        vi dp(n+2,0);

        auto nex = [&](int x){
                auto [a,b] = v[x];
                int lo = x+1, lf = n,mid;
                while(lo < lf){
                        mid = (lo+lf)/2;
                        if(v[mid].fi > b){
                                lf = mid ;
                        }else{
                                lo = mid+1;
                        }
                }
                if(v[lo].fi > b)return lo;
                return -1;
        };

        for(int i = 1; i <= n ; i++){
                if(nex(i) != -1)dp[nex(i)] = max(dp[i] + 1,dp[nex(i)]);
                else dp[n+1] = max(dp[n+1],dp[i] + 1);
                dp[i+1] = max(dp[i+1], dp[i]);
        }
        cout << dp[n+1] << ln;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	int tt = 1;
	int test = 1;
	while (tt--){
		so(test++);
	}
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 4392
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
	int n;
	cin >> n;
	vector<pair<int, int>> v(n);
	for (auto& [l, r] : v) cin >> l >> r;
	sort(v.begin(), v.end(), [&] (auto a, auto b) {
		return a.second < b.second;	
	});
	int last = -1, cnt = 0;
	for (auto [l, r] : v) {
		if (last >= l) continue;
		last = r;
		cnt += 1;
	}
	cout << cnt << "\n";
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3824
final_rank: 5

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>

struct Act {
    int start;
    int end;
};

bool comp(const Act& a, const Act& b) {
    if (a.end == b.end)
        return a.start < b.start;
    else
        return a.end < b.end;
}

int main()
{
    int n;
    std::cin >> n;
    std::vector<Act> act(n);
    for (int i = 0; i < n; i++)
        std::cin >> act[i].start >> act[i].end;

    std::sort(act.begin(), act.end(), comp);

    int count = 1;
    int et = act[0].end;
    for (int i = 1; i < n; i++) {
        if (et < act[i].start) {
            count++;
            et = act[i].end;
        }
    }

    std::cout << count << std::endl;

    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3868
final_rank: 10

code_snippet: #include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;

int main() { _
    int N;
    cin >> N;

    vector<pair<int, int>> activities;

    for (int i = 0; i < N; ++i) {
        int s, t;
        cin >> s >> t;
        activities.push_back({t, s});
    }

    sort(activities.begin(), activities.end());

    int maxActivities = 0, lastFinish = 0;

    for (int i = 0; i < N; ++i) {
        int start = activities[i].second;
        int finish = activities[i].first;

        if (start > lastFinish) {
            ++maxActivities;
            lastFinish = finish;
        }
    }

    cout << maxActivities << '\n';

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4124
final_rank: 2

code_snippet: #include <bits/stdc++.h>

using namespace std;

int main(){
    int N;
    cin >> N;
    int numAtivs = 0,  fim_geral = 0;
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> minHeap;
    for(int i = 0; i <N; i++){
        int s, t;
        cin >> s >> t;
        minHeap.push({t,s});
    }

    while(!minHeap.empty()){
        pair<int,int> aux = minHeap.top();
        minHeap.pop();
        int inicio = aux.second;
        int fim = aux.first;
        if(inicio > fim_geral){
            numAtivs++;
            fim_geral = fim;
        }
    }
    cout << numAtivs << endl;
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 4128
final_rank: 11

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    vector<pair<int,int>> activities;
    for (int i = 0; i < n; i++) {
        int s, t;
        cin >> s >> t;
        activities.emplace_back(t, s);
    }
    sort(activities.begin(), activities.end());
    int count = 0;
    int last_end = -1;
    for (auto &act : activities) {
        int finish = act.first;
        int start  = act.second;
        if (start > last_end) {
            count++;
            last_end = finish;
        }
    }
    cout << count << "\n";
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4156
final_rank: 3