I had class so had to do the virtual
i don't understand how few high rated coders get 1 wrong try on this problem :/
will take 4-5 minutes
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(int)a;i<=(int)b;i++)
using namespace std ;
int main(int argc, char **argv){
ios::sync_with_stdio(false) ;
int n ,s ,maxi = -1 ,x,y;
cin>>n>>s ;
s *=100 ;
FOR(i,0,n-1){
cin>>x>>y ;
int cost =100*x+y ;
if(cost<=s){
int change = s-cost ;
maxi = max(maxi,change%100) ;
}
}
cout<<maxi<<"\n" ;
return 0;
}
10-15 minutes to code
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(int)a;i<=(int)b;i++)
using namespace std ;
#define MAXN 100000+7
int main(int argc, char **argv){
ios::sync_with_stdio(false) ;
int n ,h[MAXN] ;
cin>>n ;
FOR(i,0,n-1){
cin>>h[i] ;
}
int cur = 0 ,energy = 0 ,res = 0 ;
FOR(i,0,n-1){
if(cur+energy>h[i]){
energy =(cur-h[i]+energy) ;
}
else{
res +=(h[i]-cur-energy) ;
energy = 0 ;
}
cur = h[i] ;
}
cout<<res<<"\n" ;
return 0;
}
the problem is super nice . At first you may get into the trap thinking this is a standard lcs problem . But take paper and pencil and u will see this is actually a lis problem i wonder how brilliant the person's mind is who come up with the problem
The problem with standard lcs problem is that if there exists two lcs of same length which one should i keep for the next string the give away is that the max value in the string is with in K and it is a permutation of n :)
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(int)a;i<=(int)b;i++)
using namespace std ;
#define MAXN 1000+7
vector<int>pos[MAXN] ;
int dp[MAXN],n,k ;
int call(int u){
if(dp[u]!=-1) return dp[u] ;
int ret = 0 ;
FOR(i,1,n){
bool ok = 1 ;
FOR(j,0,k-1){
//cout<<pos[i][j]<<" "<<pos[u][j]<<"\n" ;
if(pos[i][j] <= pos[u][j]) ok = 0 ;
}
if(ok){
//cout<<"from "<<u<<" to "<<i<<"\n" ;
ret = max(ret,call(i)) ;
}
}
dp[u] = ret+1 ;
return dp[u] ;
}
int main(int argc, char **argv){
ios::sync_with_stdio(false) ;
int val ;
cin>>n>>k ;
FOR(i,0,k-1){
FOR(j,0,n-1){
cin>>val ;
pos[val].push_back(j) ;
}
}
/*
FOR(i,1,n){
FOR(j,0,k-1) cout<<pos[i][j]<<" " ;
cout<<"\n" ;
}
* */
memset(dp,-1,sizeof(dp)) ;
int maxi = 1 ;
FOR(i,1,n){
int Len = call(i) ;
maxi = max(Len,maxi) ;
}
cout<<maxi<<"\n" ;
return 0;
}
Problem A:
pretty simple . just convert the dollers + cents combination into cents and do 100i don't understand how few high rated coders get 1 wrong try on this problem :/
will take 4-5 minutes
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(int)a;i<=(int)b;i++)
using namespace std ;
int main(int argc, char **argv){
ios::sync_with_stdio(false) ;
int n ,s ,maxi = -1 ,x,y;
cin>>n>>s ;
s *=100 ;
FOR(i,0,n-1){
cin>>x>>y ;
int cost =100*x+y ;
if(cost<=s){
int change = s-cost ;
maxi = max(maxi,change%100) ;
}
}
cout<<maxi<<"\n" ;
return 0;
}
Problem B:
Greedy + implementation , nice problem I don't the concrete proof that why Greedy will work but it's almost intuitive10-15 minutes to code
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(int)a;i<=(int)b;i++)
using namespace std ;
#define MAXN 100000+7
int main(int argc, char **argv){
ios::sync_with_stdio(false) ;
int n ,h[MAXN] ;
cin>>n ;
FOR(i,0,n-1){
cin>>h[i] ;
}
int cur = 0 ,energy = 0 ,res = 0 ;
FOR(i,0,n-1){
if(cur+energy>h[i]){
energy =(cur-h[i]+energy) ;
}
else{
res +=(h[i]-cur-energy) ;
energy = 0 ;
}
cur = h[i] ;
}
cout<<res<<"\n" ;
return 0;
}
Problem D:
At long last i solve a problem D without looking the tutorial B|the problem is super nice . At first you may get into the trap thinking this is a standard lcs problem . But take paper and pencil and u will see this is actually a lis problem i wonder how brilliant the person's mind is who come up with the problem
The problem with standard lcs problem is that if there exists two lcs of same length which one should i keep for the next string the give away is that the max value in the string is with in K and it is a permutation of n :)
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(int)a;i<=(int)b;i++)
using namespace std ;
#define MAXN 1000+7
vector<int>pos[MAXN] ;
int dp[MAXN],n,k ;
int call(int u){
if(dp[u]!=-1) return dp[u] ;
int ret = 0 ;
FOR(i,1,n){
bool ok = 1 ;
FOR(j,0,k-1){
//cout<<pos[i][j]<<" "<<pos[u][j]<<"\n" ;
if(pos[i][j] <= pos[u][j]) ok = 0 ;
}
if(ok){
//cout<<"from "<<u<<" to "<<i<<"\n" ;
ret = max(ret,call(i)) ;
}
}
dp[u] = ret+1 ;
return dp[u] ;
}
int main(int argc, char **argv){
ios::sync_with_stdio(false) ;
int val ;
cin>>n>>k ;
FOR(i,0,k-1){
FOR(j,0,n-1){
cin>>val ;
pos[val].push_back(j) ;
}
}
/*
FOR(i,1,n){
FOR(j,0,k-1) cout<<pos[i][j]<<" " ;
cout<<"\n" ;
}
* */
memset(dp,-1,sizeof(dp)) ;
int maxi = 1 ;
FOR(i,1,n){
int Len = call(i) ;
maxi = max(Len,maxi) ;
}
cout<<maxi<<"\n" ;
return 0;
}