hihocoer Front size

描述

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;
    }
} 
时间: 2024-10-18 08:37:02

hihocoer Front size的相关文章

leetcode Implement Stack using Queues

Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. empty() -- Return whether the stack is empty. Notes: You must use only s

Apache Spark-1.0.0浅析(十一):Shuffle过程

一.Shuffle的产生 Shuffle Dependency是划分stages的依据,由此判断是ShuffleMapStage或ResultStage,正如下所述 * A Spark job consists of one or more stages. The very last stage in a job consists of multiple * ResultTasks, while earlier stages consist of ShuffleMapTasks. A Resul

LeetCode225:Implement Stack using Queues

Implement the following operations of a stack using queues. push(x) – Push element x onto stack. pop() – Removes the element on top of the stack. top() – Get the top element. empty() – Return whether the stack is empty. Notes: You must use only stand

Implement Stack using Queues

Description: Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. empty() -- Return whether the stack is empty. Notes: You mu

[考研系列之数据结构]线性表之队列

基本概念 队列的定义 队列是一种只能在表的一头插入,另一头删除的线性表,简而言之具有FIFO的特性 组成 队头 队尾 扩展 双端队列 只能在两端进行删除插入操作的线性表 实现 链队列 顺序队列 循环队列 循环队列 循环队列是将顺序队列臆造成一个环,如图 循环队列有以下参数 front 指向队头的指针 rear 指向队尾的指针 SIZE 循环最大队列长度 对于循环队列,初始状态的时候 front=rear=0; 每次insert的时候 Insert((front++)%SIZE); 那么,当循环队

LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)

翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用一个仅仅有标准操作的队列. 也就是说,仅仅有push/pop/size/empty等操作是有效的. 队列可能不被原生支持.这取决于你所用的语言. 仅仅要你仅仅是用queue的标准操作,你能够用list或者deque(double-ended queue)来模拟队列. 你能够如果全部的操作都是有效的(

[leetcode] 225. Implement Stack using Queues

Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. empty() -- Return whether the stack is empty. Notes: You must use only s

数据结构与算法(1)支线任务8——Find Median from Data Stream

题目如下:(https://leetcode.com/problems/find-median-from-data-stream/) Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value. Examples: [2,3,4] 

[LeetCode][JavaScript]Implement Stack using Queues

Implement Stack using Queues Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. empty() -- Return whether the stack is empt