472B : Design Tutorial: Learn from Life

 

#Solutions

#include <bits/stdc++.h>
using namespace std;
int main() {
int b,c,tmp,n,k;
int sum=0;

cin>>n>>k;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
for(int i=n-1;i>=0;i-=k)
{
sum+=(a[i]-1)*2;     //1st floor to n-1 floor
}
cout<<sum<<endl;
}

Advertisements

1260: Sales

Problem Link

#Solution

#include <bits/stdc++.h>
using namespace std;
int main() {
int n,m,t;
cin>>t;
while(t–){
cin>>n;
vector<int>a,b;
vector<double>nw;

for(int i=0;i<n;i++)
{int x;
cin>>x;
a.push_back(x);
}
int m=a.size();
int co=0;
for(int i=0;i<m;i++)
{
for(int j=i+1;j<m;j++)
{
if(a[i]<=a[j])
co++;
}

}
cout<<co<<endl;
}return 0;

}

11565:Simple Equations

Problem Link

#Solution

#include<stdio.h>
#include<stdbool.h>
bool eqn(int a,int b,int c)
{
int i,j,k;
for(i=-100;i<=100;i++)
for(j=-100;j<=100;j++)
for(k=-100;k<=100;k++)
{
if(i!=j&&j!=k&&i!=k&&(i+j+k==a)&&(i*j*k==b)&&(i*i+j*j+k*k==c))
{printf(“%d %d %d\n”,i,j,k);
return true;}
} return false;
}

int main()
{
int a,b,c,t,flag=1;
scanf(“%d”,&t);
while(t–)
{scanf(“%d %d %d”,&a,&b,&c);
if(!eqn(a,b,c))
printf(“No solution.\n”);}
return 0;
}

 

1237:Expert Enough?

 

Problem Link

#Solution

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

int main() {

int t;
int n,m;
cin>>t;
while(t–)
{

cin>>n;
string s[n];
int lw[n],hi[n];
for(int i=0;i<n;i++)
{cin>>s[i]>>lw[i]>>hi[i];
}
cin>>m;int pos;
for(int i=0;i<m;i++)
{int x;
int flag=0;
cin>>x;
for(int i=0;i<n;i++)
{
if(lw[i]<=x &&hi[i]>=x)
{
flag++;pos=i; }
if(flag>1)
break;

}
if(flag==1)
cout<<s[pos]<<endl;
else
puts(“UNDETERMINED”);
flag=0;
}
if(t) cout<<endl;
}

}

3278: Catch That Cow

 

Problem Link

#Solution

#include<bits/stdc++.h>
using namespace std;
struct point{
int x;
int tym;
};

int vis[100010];
int n,k;
#include<cstdio>
int bfs()
{memset(vis,0,sizeof vis);
point val;

val.x=n;
val.tym=0;
vis[n]=1;

queue<point>q;
point a,b,c;
q.push(val);
while(!q.empty())
{
point val=q.front();
q.pop();

if(val.x==k) return val.tym;

a.x=val.x-1;
a.tym=val.tym+1;

if(a.x >=0 && a.x<=100010&& !vis[a.x])
{
vis[a.x]=1;
q.push(a);
}
b.x=val.x+1;
b.tym=val.tym+1;
if(b.x>=0 &&b.x<=100010 &&!vis[b.x])
{
vis[b.x]=1;
q.push(b);

}
c.x=val.x*2;
c.tym=val.tym+1;
if(c.x>=0 &&c.x<=100010 &&!vis[c.x])
{
vis[c.x]=1;
q.push(c);

}

}
}
int main()
{
while(cin>>n>>k )
{
if(n>=k)
cout<<n-k<<endl;
else cout<<bfs()<<endl;

}
return 0;
}