It was a mixed round for me . I manged to solved problem A without any hiccup . But for problem B i accepted the greedy solution that came into my head . not realising it contains a serious bug which a got at the end of the contest . Unfortunately It passed the pretests and no one hacked me :'( . As usual it failed the system test .
On the bright side I manged to solve problem C . which was easy . You just need to get the maths right . Power function may take O(lgn) time.
Problem A:
#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 ;
cin>>n ;
int d = 1 ;
while(d<=n){
int s = n-d ;
FOR(i,0,(s/2)-1) cout<<"*" ;
FOR(i,0,d-1) cout<<"D" ;
FOR(i,0,(s/2)-1) cout<<"*" ;
cout<<"\n" ;
d+=2 ;
}
d-=4 ;
while(d>0){
int s = n-d ;
FOR(i,0,(s/2)-1) cout<<"*" ;
FOR(i,0,d-1) cout<<"D" ;
FOR(i,0,(s/2)-1) cout<<"*" ;
cout<<"\n" ;
d-=2 ;
}
return 0;
}
Problem B :
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(int)a;i<=(int)b;i++)
using namespace std ;
#define MAXN 1000000
int a[MAXN] ;
int main(int argc, char **argv){
ios::sync_with_stdio(false) ;
int n ;
int res ;
cin>>n ;
FOR(i,0,n-1){
cin>>a[i] ;
}
int seq = 0 ;
//cout<<idx<<"\n" ;
FOR(i,1,n-1){
if(a[i]<a[i-1]){
seq++ ;
res = n-i ;
}
}
if(seq==0) cout<<"0\n" ;
else if(seq==1) {
if(a[n-1]>a[0]) {
cout<<"-1\n" ;
return 0 ;
}
cout<<res<<"\n" ;
}
else cout<<"-1\n" ;
return 0;
}
Problem C:
#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) ;
double n,m ;
cin>>n>>m ;
double res = n ;
for(int i=n-1;i>0;i--){
res -= pow((i/n),m) ;
}
printf("%.13lf",res) ;
return 0;
}
On the bright side I manged to solve problem C . which was easy . You just need to get the maths right . Power function may take O(lgn) time.
Problem A:
#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 ;
cin>>n ;
int d = 1 ;
while(d<=n){
int s = n-d ;
FOR(i,0,(s/2)-1) cout<<"*" ;
FOR(i,0,d-1) cout<<"D" ;
FOR(i,0,(s/2)-1) cout<<"*" ;
cout<<"\n" ;
d+=2 ;
}
d-=4 ;
while(d>0){
int s = n-d ;
FOR(i,0,(s/2)-1) cout<<"*" ;
FOR(i,0,d-1) cout<<"D" ;
FOR(i,0,(s/2)-1) cout<<"*" ;
cout<<"\n" ;
d-=2 ;
}
return 0;
}
Problem B :
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(int)a;i<=(int)b;i++)
using namespace std ;
#define MAXN 1000000
int a[MAXN] ;
int main(int argc, char **argv){
ios::sync_with_stdio(false) ;
int n ;
int res ;
cin>>n ;
FOR(i,0,n-1){
cin>>a[i] ;
}
int seq = 0 ;
//cout<<idx<<"\n" ;
FOR(i,1,n-1){
if(a[i]<a[i-1]){
seq++ ;
res = n-i ;
}
}
if(seq==0) cout<<"0\n" ;
else if(seq==1) {
if(a[n-1]>a[0]) {
cout<<"-1\n" ;
return 0 ;
}
cout<<res<<"\n" ;
}
else cout<<"-1\n" ;
return 0;
}
Problem C:
#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) ;
double n,m ;
cin>>n>>m ;
double res = n ;
for(int i=n-1;i>0;i--){
res -= pow((i/n),m) ;
}
printf("%.13lf",res) ;
return 0;
}
No comments:
Post a Comment