描述
Steven loves reading book on his phone. The book he reads now consists of N paragraphs and the i-th paragraph contains ai characters.
Steven wants to make the characters easier to read, so he decides to increase the font size of characters. But the size of Steven‘s phone screen is limited. Its width is W and height is H. As a result, if the font size of characters is S then it can only show ⌊W / S⌋ characters in a line and ⌊H / S⌋ lines in a page. (⌊x⌋ is the largest integer no more than x)
So here‘s the question, if Steven wants to control the number of pages no more than P, what‘s the maximum font size he can set? Note that paragraphs must start in a new line and there is no empty line between paragraphs.
输入
Input may contain multiple test cases.
The first line is an integer TASKS, representing the number of test cases.
For each test case, the first line contains four integers N, P, W and H, as described above.
The second line contains N integers a1, a2, ... aN, indicating the number of characters in each paragraph.
For all test cases,
1 <= N <= 103,
1 <= W, H, ai <= 103,
1 <= P <= 106,
There is always a way to control the number of pages no more than P.
输出
For each testcase, output a line with an integer Ans, indicating the maximum font size Steven can set.
这题我有点取巧,不太清楚别人怎么做的,我是用估算来做的,先按每段都不写满这种特殊情况赋值,毕竟恰好一段写满是小概率事件,这样来降低运算的时间复杂度,然后做调整,当过界时,即状态改变时即是所求临界点,下面是我的代码:
#include<iostream> #include<math.h> using namespace std; int inter(int a,int b){ if(a%b==0){ return a/b; }else{ return a/b+1; } } bool pan(int u,int W,int H,int a[],int n,int P){ int w = W/u; int h = H/u; int t = 0; int k = P*h; for(int i=0;i<n;i++){ t += inter(a[i],w); if(t>k){ return false; } } return true; } int main() { int Task; cin >> Task; int an[Task]; for(int j = 0;j<Task;j++){ int N,P,W,H; cin >>N>>P>>W>>H; int a[N]; int S = 0; for(int i = 0;i<N;i++){ cin >> a[i]; S +=a[i]; } S =(P*H*W)/S; S=sqrt(S); int flag1 = 0; if(pan(S,W,H,a,N,P)){ flag1--; while(flag1!=0){ if(pan(S,W,H,a,N,P)){ S++; }else{ flag1++; S--; } } }else{ flag1++; while(flag1!=0){ if(pan(S,W,H,a,N,P)){ flag1--; }else{ S--; } } } an[j]=S; } for(int j = 0;j<Task;j++){ cout << an[j]<<endl; } }