Print Article

博客围绕文章打印最小成本问题展开,给出输入输出要求及示例。还提及斜率优化,给出相关链接。同时展示了求解该问题的代码,包括初始化、求dp值、分子分母等函数及主函数逻辑,通过一系列操作计算并输出最小成本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description
Zero has an old printer that doesn't work well sometimes. As it is antique, he still like to use it to print articles. But it is too old to work for a long time and it will certainly wear and tear, so Zero use a cost to evaluate this degree. One day Zero want to print an article which has N words, and each word i has a cost Ci to be printed. Also, Zero know that print k words in one line will cost
M is a const number. Now Zero want to know the minimum cost in order to arrange the article perfectly.
 
Input
There are many test cases. For each test case, There are two numbers N and M in the first line (0 ≤ n ≤ 500000, 0 ≤ M ≤ 1000). Then, there are N numbers in the next 2 to N + 1 lines. Input are terminated by EOF.
 
Output
A single number, meaning the mininum cost to print the article.
 
Sample Input
5 5 5 9 5 7 5
 
Sample Output
230
 
Author
Xnozero
 
Source
 
Recommend
zhengfeng
***************************************************************************************
讲解的很清楚,有图又推导,不过看了很长时间才看懂,关键求斜率推导式
***************************************************************************************
 1 #include<iostream>
 2 #include<cstring>
 3 #include<string>
 4 #include<cstdio>
 5 #include<cmath>
 6 #define  L  long long
 7 using namespace std;
 8 L dp[500005];
 9 L sum[500005];
10 int  q[500005];
11 int n,m,i,j;
12 void  init()
13 {
14     for(i=1;i<=n;i++)
15      cin>>sum[i];
16     for(i=2;i<=n;i++)
17       sum[i]+=sum[i-1];
18 
19 }
20 L getdp(int i,int j)//求dp
21     {
22         return dp[j]+m+(sum[i]-sum[j])*(sum[i]-sum[j]);
23     }
24 L  getup(int j,int k)//分子
25  {
26      return   (dp[j]+sum[j]*sum[j])-(dp[k]+sum[k]*sum[k]);
27  }
28 L getdown(int j,int k)//分母
29   {
30       return  2*(sum[j]-sum[k]);
31   }
32 int main()
33 {
34    while(scanf("%d %d",&n,&m)!=EOF)
35     {
36         memset(dp,0,sizeof(dp));
37         memset(sum,0,sizeof(sum));
38         memset(q,0,sizeof(q));
39         init();
40       int head=0;
41     int tail=0;
42      q[0]=dp[0]=0;
43      tail++;
44      for(i=1;i<=n;i++)
45       {
46           while(head+1<tail&&getup(q[head+1],q[head])<=sum[i]*getdown(q[head+1],q[head]))
47             head++;
48             dp[i]=getdp(i,q[head]);
49           while(head+1<tail&&getup(i,q[tail-1])*getdown(q[tail-1],q[tail-2])<=getup(q[tail-1],q[tail-2])*getdown(i,q[tail-1]))//删去一定不满足条件的点
50             tail--;
51           q[tail++]=i;
52       }
53 
54   cout<<dp[n]<<endl;
55 }
56   return 0;
57 
58 
59 
60 }
View Code

 

转载于:https://www.cnblogs.com/sdau--codeants/p/3251238.html

在Flask中,使用`print`函数在控制台打印信息并不是一个常见的做法,一般推荐使用Flask的日志系统进行消息的记录和打印。 如果你想在控制台输出信息,你可以使用Flask的`app.logger`,它是Flask应用程序自带的日志系统。你可以使用`app.logger.info`方法来记录消息日志,使用`app.logger.debug`方法来进行调试日志的测试,使用`app.logger.warning`方法来记录警告日志,使用`app.logger.error`方法来记录错误日志。这样可以更好地追踪和查找WEB故障。 此外,Flask实际上是调用了Python自带的logging模块来实现日志功能。你可以直接引入logging模块,并使用`basicConfig`方法进行日志输出格式和方式的相关配置。通过设置日志级别为DEBUG,可以将所有的打印信息显示在控制台上。你可以使用`logger.debug`方法来记录调试日志。 在Flask应用程序的`__init__`文件中,你可以使用`print`函数来打印信息,这些信息将会在控制台上显示。 总结起来,如果你想在Flask应用程序中控制台打印信息,有以下几种方法: 1. 使用`app.logger`来记录不同级别的日志消息,推荐使用这种方式。 2. 使用Python自带的logging模块,配置日志的输出格式和方式,然后使用`logger.debug`方法来记录调试日志。 3. 在`__init__`文件中使用`print`函数打印信息。 请根据你的具体需求选择合适的方法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [flask-12 flask-script应用](https://blog.youkuaiyun.com/legend818/article/details/121948816)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [2021-10-31flask下的类似print功能-------- 日志系统的使用](https://blog.youkuaiyun.com/gaoke11240/article/details/121059783)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值