首页 > 其他分享 >P4655 [CEOI2017] Building Bridges

P4655 [CEOI2017] Building Bridges

时间:2024-08-28 09:15:11浏览次数:7  
标签:Building P4655 Bridges int sum tr CEOI2017 ih id

题意

image

思路

设 \(sum_i = \sum\limits_{j = 1}^i w_j\)。

可以得到转移方程 \(f_i = f_j + (h_i - h_j) ^ 2 + sum_i - sum_j\)。

转化为 \(y = kx + b\) 的形式:

\(f_i = f_j + (h_i - h_j) ^ 2 + sum_i - sum_j = f_j + h_i^2 + h_j^2 - 2 h_ih_j + sum_i - sum_j = (-2h_ih_j) + (f_j + h_j ^ 2 - sum_j) + (h_i ^ 2 + sum_i)\)。

转化完成,\(k = -2h_i, b = f_j + h_j ^ 2 - sum_j\)。

然后我们边转移,边把这条线段放入即可。

代码

#include <bits/stdc++.h>

#define int long long

using namespace std;

const int N = 1000010;

int n, h[N], w[N], f[N];

struct node {
    int id;
} tr[N << 2];

double gety(double k, int x, double b) {
    return k * x + b;
}

double k[N], b[N];

bool compare(int v1, int v2, int x) {
    double y1 = gety(k[v1], x, b[v1]);
    double y2 = gety(k[v2], x, b[v2]);
    return y1 < y2;
}


void update(int u, int l, int r, int pl, int pr, int x) {
    int mid = l + r >> 1;
    if (pl <= l && r <= pr) {
        if (!tr[u].id) {
            tr[u].id = x;
            return;
        }
        if (compare(x, tr[u].id, mid)) swap(tr[u].id, x);
        if (compare(x, tr[u].id, l)) update(u << 1, l, mid, pl, pr, x);
        if (compare(x, tr[u].id, r)) update(u << 1 | 1, mid + 1, r, pl, pr, x);
        return;
    }
    if (pl <= mid) update(u << 1, l, mid, pl, pr, x);
    if (pr > mid) update(u << 1 | 1, mid + 1, r, pl, pr, x);
}

double query(int u, int l, int r, int x) {
    if (l == r) return gety(k[tr[u].id], x, b[tr[u].id]);
    int mid = l + r >> 1;
    double ans = gety(k[tr[u].id], x, b[tr[u].id]), ans2 = 0;
    if (x <= mid) ans2 = query(u << 1, l, mid, x);
    else ans2 = query(u << 1 | 1, mid + 1, r, x);
    return min(ans, ans2);
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n;
    for (int i = 1; i <= n; i++ )cin >> h[i];
    for (int i = 1; i <= n; i++) cin >> w[i], w[i] += w[i - 1];
    b[0] = 1e18;
    k[1] = -2 * h[1], b[1] = h[1] * h[1] - w[1];
    update(1, 1, 1000000, 1, 1000000, 1);
    for (int i = 2; i <= n; i++) {
        f[i] = h[i] * h[i] + w[i - 1] + query(1, 1, 1000000, h[i]);
        k[i] = -2 * h[i], b[i] = f[i] + h[i] * h[i] - w[i];
        update(1, 1, 1000000, 1, 1000000, i);
    }
    cout << f[n] << '\n';
    return 0;
}

标签:Building,P4655,Bridges,int,sum,tr,CEOI2017,ih,id
From: https://www.cnblogs.com/Yuan-Jiawei/p/18383933

相关文章

  • [ARC062E] Building Cubes with AtCoDeer
    题意给定\(n\)个正方形,每个正方形的四个角有颜色,有方向。你需要选出\(6\)个正方形,拼出一个正方体,使得角上的颜色相同。问有多少种方案。\(n\le400,m\le1000\)Sol注意到若确定了对着的两个面,就可以确定正方体上所有面的颜色。因此考虑枚举两个面,计算出\(8\)个角......
  • SSUD71-101 Understanding Buildings 241
    SSUD71-101Understanding Buildings241:Assignment2Topic: Assignment2:Group project requiring application ofconstructionprocesses/methods.Format:Part 1:Siteand HouseSummaryPart2: Detailed RoomConstructionSchedulePart3:Construction......
  • P4653 [CEOI2017] Sure Bet
    上链接[CEOI2017]SureBet题目描述现在有$n$个A类灯泡和$n$个B类灯泡,每个灯泡都有各自的权值。我们将这些灯泡分为$n$组,每组包含一个来自A类的灯泡和一个来自B类的灯泡。你可以从中选取任意个灯泡,每选取一个灯泡需要花费$1$的代价。在你选取完之后,系统会随机在A类......
  • Building a Car Rental Website
    WebApplicationDevelopment:BuildingaCarRentalWebsiteSummer2024AllocationThiscourseworkisworth100%ofthemarksformoduleYouwillalsoneedskillsfromthefollowingmodules:(PythonProgramming)(Designreporting)(Databasedesign)(Secu......
  • 无法在 python 中安装 pip install expliot - bluepy 的 Building Wheel (pyproject.t
    在此处输入图像描述当我尝试在Windows计算机中通过cmd安装pipinstallexpliot包时,我收到2个错误名称×Buildingwheelforbluepy(pyproject.toml)didnotrunsuccessfully.│exitcode:1**AND**opt=self.warn_dash_deprecation......
  • Derect local .aar file dependencies are not supported when building an AAR
    背景项目中需要将部分功能业务打包成aar包供其它项目调用,我引入了fataar(具体使用方式见github)。实际打包过程中抛出错误Derectlocal.aarfiledependenciesarenotsupportedwhenbuildinganAAR原因分析我们项目中lib文件夹下导入了很多jar包、aar包,在之前没有引入aar包......
  • CF1316E Team Building
    CF1316ETeamBuilding状压dp观察:假如选出了\(p\)个队员,那么\(k\)个观众一定是剩下的人中的前\(k\)大。可以考虑将\(a_i\)从大到小排序,那么观众就是剩下的人中的一段前缀。然后就可以考虑怎么选队员了,发现\(p\)很小,考虑直接状压队员。设\(f_{i,s}\)表示考虑完前......
  • C121 李超树+DP P4655 [CEOI2017] Building Bridges
    视频链接:C121李超树+DPP4655[CEOI2017]BuildingBridges_哔哩哔哩_bilibili   LuoguP4655[CEOI2017]BuildingBridges#include<iostream>#include<cstring>#include<algorithm>usingnamespacestd;#definelllonglong#definelsu<<......
  • pyqt5报错记录:ERROR: Failed building wheel for PyQt5-sip
    问题:pipinstallpyqt5Collectingpyqt5UsingcachedPyQt5-5.15.10-cp37-abi3-win_amd64.whl.metadata(2.2kB)CollectingPyQt5-sip<13,>=12.13(frompyqt5)UsingcachedPyQt5_sip-12.13.0.tar.gz(123kB)Installingbuilddependencies...doneGettingr......
  • microsoft全球GlobalMLBuildingFootprints下载方法
    website:https://github.com/microsoft/GlobalMLBuildingFootprints?tab=readme-ov-filePython代码Start"""Thissnippetdemonstrateshowtoaccessandconvertthebuildingsdatafrom.csv.gztogeojsonforuseincommonGIStools.Youwillneedtoi......