4D - Mysterious Present - CodeForces Solution


dp sortings *1700

Please click on ads to support us..

Python Code:

import sys






input = sys.stdin.readline
n, width, height = map(int, input().split())
arr = []
for i in range(n):
    w, h = map(int, input().split())
    if w > width and h > height:
        arr.append((w, h, i+1))

F = [1]*len(arr)
parent = [-1]*len(arr)
arr.sort(key=lambda x:x[0], reverse=True)

for i in range(len(arr)):
    for j in range(i):
        if arr[j][0] > arr[i][0] and arr[j][1] > arr[i][1] and F[j]+1 > F[i]:
            F[i] = F[j]+1
            parent[i] = j
ind = -1
m = 0
for i in range(len(arr)):
    if F[i] > m:
        m = F[i]
        ind = i

ans = []
print(m)
while ind != -1:
    ans.append(arr[ind][2])
    ind = parent[ind]
if len(ans):
    print(*(ans))

C++ Code:

// Hydro submission #6410a2f4cff12ea87106af21@1678811892987
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5005;

int f[N],g[N];
int n,x,y,ans;

struct node{
    int id;
    int x;
    int y;
    bool operator<(const node &aa) const{
        if(y == aa.y){
            return y < aa.y;
        }
        return x < aa.x;
    }
}a[N];

void Index(int ix){
    if(ix == -1)
    return;
    Index(g[ix]);
    cout<<a[ix].id<<" ";
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>x>>y;
    for(int i = 0;i < n;i++){
        cin>>a[i].x>>a[i].y;
        a[i].id = i+1;
    }
    sort(a,a+n);
    memset(f,0,sizeof(f));
    memset(g,-1,sizeof(g));

    for(int i = 0;i < n;i++){
        if(a[i].x > x && a[i].y > y){
            f[i] = 1;
            for(int j = 0;j < i;j++){
                if(a[j].x < a[i].x &&a[j].y < a[i].y && f[j] >= f[i]){
                    f[i] = f[j]+1;
                    g[i] = j;
                }
            }
            if(f[ans] < f[i]){
                ans = i;
            }
        }
    }
    cout<<f[ans]<<endl;
    if(f[ans]){
        Index(ans);
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

1472B - Fair Division
1281C - Cut and Paste
141A - Amusing Joke
112A - Petya and Strings
677A - Vanya and Fence
1621A - Stable Arrangement of Rooks
472A - Design Tutorial Learn from Math
1368A - C+=
450A - Jzzhu and Children
546A - Soldier and Bananas
32B - Borze
1651B - Prove Him Wrong
381A - Sereja and Dima
41A - Translation
1559A - Mocha and Math
832A - Sasha and Sticks
292B - Network Topology
1339A - Filling Diamonds
910A - The Way to Home
617A - Elephant
48A - Rock-paper-scissors
294A - Shaass and Oskols
1213A - Chips Moving
490A - Team Olympiad
233A - Perfect Permutation
1360A - Minimal Square
467A - George and Accommodation
893C - Rumor
227B - Effective Approach
1534B - Histogram Ugliness