Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Name: Mayank Kumar
- Roll No:1901Cb24
- Assignment no 4
- */
- #include <stdio.h>
- #include <stdlib.h>
- int abs(int x) //helper function for absolute value
- {
- return (x>0)?x:-1*x;
- }
- int min(int a,int b) // helper function for minimum of 2 values
- {
- return (a<b)?a:b;
- }
- int dist(int a,int b) //helper function for minimum distance
- {
- int d=abs(a-b);
- d=min(d,100-d);
- return d;
- }
- int main()
- {
- int n,m,k;
- scanf("%d",&n);
- scanf("%d",&m);
- scanf("%d",&k);
- int x[n],y[m],r[m],total=0,u[n],charged=0;
- for(int i=0;i<n;i++)
- {
- scanf("%d",&x[i]);
- }
- for(int i=0;i<m;i++)
- {
- scanf("%d",&y[i]);
- }
- for(int i=0;i<m;i++)
- {
- scanf("%d",&r[i]);
- }
- for(int i=0;i<n;i++)
- {
- u[i]=0;
- }
- //do something
- for(int i=0;i<m;i++)
- {
- //for each vehicle find nearest station
- int c_s;
- int d=1000;
- for(int j=0;j<n;j++)
- {
- if(dist(y[i],x[j])<d && u[j]<k)
- {
- c_s=j;
- d=dist(y[i],x[j]);
- }
- }
- //nearest station found with index c_s(charging station)
- if(u[c_s]<k)
- {
- if(r[i]>=d)
- {
- u[c_s]++;
- total+=d;
- charged++;
- }
- }
- }
- printf("%d ",total);
- printf("%d ",m-charged);
- for(int i=0;i<n;i++)
- {
- float op=(float)u[i]/k;
- op*=100;
- int sd=op;
- printf("%d ",sd);
- }
- return 0;
- }
- /*
- test 0:
- i/p:2 7 3 7 99 3 90 60 40 50 11 3 50 50 50 40 40 40 40
- o/p: 93 1 100 100
- test 1:
- i/p:2 3 3 5 9 3 9 32 81 50 50 50
- o/p:25 0 33 66
- test 2:
- i/p:1 5 3 95 3 35 49 7 11 15 15 15 30 30
- o/p:36 2 100
- */
Add Comment
Please, Sign In to add comment