[LightOJ] [1275] Internet Service Providers

Problem Description

Loading...

Problem Analysis

I feel the hard part of this problem is… it’s an English-language problem
-.-
And then there’s the fact that I can’t understand what the problem is asking

Actually, the solution to this problem is very simple
It is just to find the integer value of T that makes this quadratic equation reach its extremum
…uh
Or rather
find an integer T such that when it is chosen as the value, the function result is maximized
(because N>0)

The equation should actually be written like this

$$T*(C - T*N)$$

which is equivalent to

$$-N*T^2+C*T$$

Replacing it with x might make it easier to understand…

$$y=-N*x^2+C*x$$

So…
It’s very simple

There is also something to note: here we need the integer value of T
So we need to check whether the result obtained from the formula is an integer
If it is not
we also need to compare the function values of the two integers on its left and right
(Also, when N==0, the output should be 0)

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>
#include<math.h>
int main() {
	int T;
	int i;
	long long N,C,x;
	scanf("%d",&T);
	for(i=1; i<=T; i++) {
		scanf("%lld %lld",&N,&C);
		if(!N)
			x=0;
		else {
			x=(C/N)/2;
			if(C%(2*N))
				if((C*x-N*x*x)<(C*(x+1)-N*(x+1)*(x+1)))
					x++;
		}
		printf("Case %d: %lld\n",i,x);
	}
	return 0;
}

Problem URL: Light [1275] Internet Service Providers

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...