Algorithm
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <cstdio>
#include <vector>
int main(){
int n(0); scanf("%d", &n);
std::vector<std::vector<int> > laptop;
for(int k = 0; k < n; k++){
laptop.push_back(std::vector<int>(4,0));
int speed(0), ram(0), hdd(0), cost(0);
scanf("%d %d %d %d", &speed, &ram, &hdd, &cost);
laptop[k][0] = speed;
laptop[k][1] = ram;
laptop[k][2] = hdd;
laptop[k][3] = cost;
}
bool *candidate = new bool[n]; for(int k = 0; k < n; k++){candidate[k] = 1;}
for(int a = 0; a < n; a++){
for(int b = 0; b < n; b++){
if(a == b){continue;}
if(laptop[a][0] < laptop[b][0] && laptop[a][1] < laptop[b][1] && laptop[a][2] < laptop[b][2] && laptop[a][3] < laptop[b][3]){candidate[a] = 0; break;}
}
}
int bestLaptop(-1), bestPrice(1001);
for(int k = 0; k < n; k++){
if(!candidate[k]){continue;}
if(laptop[k][3] < bestPrice){bestPrice = laptop[k][3]; bestLaptop = k;}
}
printf("%d\n", bestLaptop + 1);
return 0;
}
Copy The Code &
Try With Live Editor
Input
5
2100 512 150 200
2000 2048 240 350
2300 1024 200 320
2500 2048 80 300
2000 512 180 150
2100 512 150 200
2000 2048 240 350
2300 1024 200 320
2500 2048 80 300
2000 512 180 150
Output
4
Demonstration
Codeforces Solution-B. Choosing Laptop-Solution in C, C++, Java, Python