Meaning of this statement

for this question:

Tomorrow Peter has a Biology exam. He does not like this subject much, but d days ago he learnt that he would have to take this exam. Peter's strict parents made him prepare for the exam immediately, for this purpose he has to study not less than minTimei and not more than maxTimei hours per each i-th day. Moreover, they warned Peter that a day before the exam they would check how he has followed their instructions.

So, today is the day when Peter's parents ask him to show the timetable of his preparatory studies. But the boy has counted only the sum of hours sumTime spent him on preparation, and now he wants to know if he can show his parents a timetable sсhedule with d numbers, where each number sсhedulei stands for the time in hours spent by Peter each i-th day on biology studies, and satisfying the limitations imposed by his parents, and at the same time the sum total of all schedulei should equal to sumTime.

Input
The first input line contains two integer numbers d, sumTime (1 ≤ d ≤ 30, 0 ≤ sumTime ≤ 240) — the amount of days, during which Peter studied, and the total amount of hours, spent on preparation. Each of the following d lines contains two integer numbers minTimei, maxTimei (0 ≤ minTimei ≤ maxTimei ≤ 8), separated by a space — minimum and maximum amount of hours that Peter could spent in the i-th day.

Output
In the first line print YES, and in the second line print d numbers (separated by a space), each of the numbers — amount of hours, spent by Peter on preparation in the corresponding day, if he followed his parents' instructions; or print NO in the unique line. If there are many solutions, print any of them.

Examples
input
1 48
5 7
output
NO
input
2 5
0 1
3 5
output
YES
1 4



Solution is that its the min(maxtimes[right side of input] , sumtime - sum of all minimum times[left side of input]+minimum time on that day)

I understand the reason behind getting the minimum in general it's because he'd want to study the least amount of time but I don't get the idea behind
sumtime - sum of all minimum times[left side of input]+minimum time on that day



Here is a sample of what i said:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<iostream>
using namespace std;
int main()
{
	int n,i,sumtime,mintime=0,maxtime=0;
    cin>>n>>sumtime;
    int a[n],b[n];
    for(i=0;i<n;i++)
    {
    	cin>>a[i]>>b[i];
    	mintime+=a[i];
    	maxtime+=b[i];
    }
	if(sumtime<mintime or sumtime>maxtime)
	{
		cout<<"NO\n";
		return 0;
	}
	cout<<"YES\n";
	for(i=0;i<n;i++)
	{
		cout<<min(b[i],a[i]+sumtime-mintime)<<" ";
		sumtime-=min(b[i],a[i]+sumtime-mintime);
		mintime-=a[i];
	}
}
Last edited on
anyone? :(
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
   int *minTime, *maxTime;

   int d, sumTime;
   cin >> d >> sumTime;
   minTime = new int[d];
   maxTime = new int[d];

   int sumMin = 0, sumMax = 0;
   for ( int i = 0; i < d; i++ )
   {
      cin >> minTime[i] >> maxTime[i];
      sumMin += minTime[i];
      sumMax += maxTime[i];
   }
 
   if ( sumTime < sumMin || sumTime > sumMax )                      // Test non-feasibility
   {
      cout << "NO\n";
   }
   else
   {
      cout << "YES\n";
      int deficit = sumTime - sumMin;                               // Extra hours above minimum
      for ( int i = 0; i < d; i++ )
      {
         if ( deficit > 0 )                                         // The path to glory is paved with good intentions ...
         {                                                          // ... put as many extra hours in at the start as possible
            int extra = min( deficit, maxTime[i] - minTime[i] );    // As many extra hours as possible or needed today
            deficit -= extra;                                       // Reduce the deficit
            cout << minTime[i] + extra << " ";
         }
         else
         {
            cout << minTime[i] << " ";
         }
      }
   }

   delete [] minTime;
   delete [] maxTime;
}




BTW, the lines you cite
1
2
    cin>>n>>sumtime;
    int a[n],b[n];

are strictly illegal. You can't declare dimensions of arrays like this at run time (whatever your non-standard-conforming compiler allows). Also, min() won't/shouldn't be available by default.

Either:
- preset the dimensions of a[] and b[] as maximal for the problem (legitimate, but wasteful);
- use new (and subsequently delete) to acquire memory for them on the free store (as in my example)
- use vectors (safer way of doing the above).
Last edited on
Topic archived. No new replies allowed.