Hdoj 1300 Pearls Slope optimization DP

Source: Internet
Author: User

Original Topic Connection: http://acm.hdu.edu.cn/showproblem.php?pid=1300

Test instructions

The topic is too long. See for yourself.


Read the topic, you will find this is a silly DP problem, slope optimization is good

#include <iostream>#include<cstring>#include<vector>#include<cstdio>#defineMax_n 500using namespaceStd;typedefLong Longll;ll n;ll sum[max_n];ll dp[max_n];ll a[max_n];ll p[max_n];DoubleYintt) {    returndp[t];}DoubleXintt) {    returnsum[t];}DoubleSlope (intUintv) {return(Y (U)-y (v))/(X (U)-X (v));}intQue[max_n];intT;intMain () {Cin.sync_with_stdio (false); CIN>>u;  while(t--) {cin>>N; Memset (DP,0,sizeof(DP)); memset (que,0,sizeof(que)); memset (SUM,0,sizeof(sum));  for(inti =1; I <= N; i++) {cin>> A[i] >>P[i]; Sum[i]= Sum[i-1] +A[i]; }        intFront =0, rear =0; Que[rear++] =0;  for(inti =1; I <= N; i++) {             while(Rear-front >1&& Slope (Que[front], Que[front +1]) <= P[i]) front++; intj =Que[front]; Dp[i]= Dp[j] + (Sum[i]-sum[j] +Ten) *P[i];  while(Rear-front >1&& Slope (Que[rear-1], Que[rear-2]) >= Slope (Que[rear-1], i)) rear--; Que[rear++] =i; } cout<< Dp[n] <<Endl; }    return 0;}

Hdoj 1300 Pearls Slope optimization DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.