VK Cup 2018 Round 2: D. Riverside Curio(思维)

本文介绍了一种解决河流观测问题的方法,通过记录每日水位变化并标记水位线,求解每天水线下标记线总数的最小值。文章提供了一种简单有效的方法,包括确保标记线数量非严格递增及调整相邻两天标记线数量差值不超过1。

D. Riverside Curio
time limit per test 1 second
memory limit per test 256 megabytes
input standard input
output standard output

Arkady decides to observe a river for n consecutive days. The river’s water level on each day is equal to some real value.

Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to mi.

Define di as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of di over all days. There are no marks on the channel before the first day.
Input

The first line contains a single positive integer n (1 ≤ n ≤ 105) — the number of days.

The second line contains n space-separated integers m1, m2, …, mn (0 ≤ mi < i) — the number of marks strictly above the water on each day.
Output

Output one single integer — the minimum possible sum of the number of marks strictly below the water level among all days.
Examples
input

6
0 1 0 3 0 2

output

6

input

5
0 1 2 1 2

output

1

input

5
0 1 1 2 2

output

0

题意:你用n天时间去观察一条河,这条河的水位每天都有可能改变,所有你每天都会在当前水位上画一条线来标注(这个线永远都不会消失),当然啦,如果这个水位刚好和之前某天水位一致,也就是已经有线和水面平齐,那么当然就不需要再标记了。现在告诉你每天可以看到的水位线数量m[i],求出∑d[i]的最小值,其中d[i]表示第i天水底下的标记线的个数
解析:
设c[i]表示第i次到河边后总共画了多少条线,那么有c[i] = m[i]+d[i]+1
由题意c[i]必须非严格递增,并且相邻两个数差距不超过1
方法很简单,只要先把它变成非严格递增,如果m[i]< m[i-1],就让m[i] = m[i-1],然后再从后往前遍历如果m[i]+1< m[i+1],那么m[i] = m[i+1]-1满足第二个条件就行了

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define pb push_back
#define mod 1000000007
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rep1(i,b,a) for(int i=b;i>=a;i--)
using namespace std;
const int N=1e5+100;
int a[N];
int main()
{
    ios::sync_with_stdio(false);
    ll ans;
    int n,i;
    cin>>n;
    for(i=1;i<=n;i++)
        cin>>a[i];
    ans = 0;
    for(i=1;i<=n;i++)
    {
        if(a[i]<a[i-1])
        {
            ans+=a[i-1]-a[i];
            a[i]=a[i-1];
        }
    }
    for(i=n-1;i>=1;i--)
    {
        if(a[i]+1<a[i+1])
        {
            ans+=a[i+1]-a[i]-1;
            a[i]=a[i+1]-1;
        }
    }
    cout<<ans<<endl;
    return 0;
}
响应内容如下:{ "code": 0, "msg": "Success", "data": { "pageNum": 1, "pageSize": 20, "total": 142, "list": [ { "dsmCode": "345607", "dsmTitle": "EXPLORE 900儿童山地自行车-橘红色", "modelCode": "8858200", "randomItemCode": "5133630", "brandDisplayName": "BTWIN", "label": { "backgroundColor": "#E20C18", "foregroundColor": "#ffffff" }, "modelCount": 2, "price": { "listPrice": 241.00, "activePrice": 128.00, "discountRate": "5.0", "displayDiscountRate": true }, "mainImage": "https://easymerch-oss-pp.decathlon.com.cn/emh-ppd-tf/2025-05-23/8858200_主图_1_2fa101ec-6a1f-4e5e-a145-ad213fdd44f9.jpg", "images": [ "https://easymerch-oss-pp.decathlon.com.cn/emh-ppd-tf/2025-05-23/8858200_主图_1_2fa101ec-6a1f-4e5e-a145-ad213fdd44f9.jpg", "https://easymerch-oss-pp.decathlon.com.cn/emh-ppd-tf/2025-05-23/8796133_主图_1_e3aa42ab-46da-43da-b810-474b9bbee793.jpg" ] }, { "dsmCode": "313167", "dsmTitle": "RC 120铝合金弯把公路车", "modelCode": "8576492", "randomItemCode": "2966505", "brandDisplayName": "TRIBAN", "label": { "backgroundColor": "#8126A3", "foregroundColor": "#ffffff", "text": "新品" }, "modelCount": 2, "price": { "listPrice": 227.00, "activePrice": 125.00, "discountRate": "6.0", "displayDiscountRate": true }, "mainImage": "https://pixl.decathlon.com.cn/p1752594/k$3f269157b70d616094545db8e539ca8d/content.jpg", "images": [ "https://pixl.decathlon.com.cn/p1752594/k$3f269157b70d616094545db8e539ca8d/content.jpg", "https://easymerch-oss-pp.decathlon.com.cn/emh-ppd-tf/2025-05-23/8651416_主图_1_08e469ae-2b60-4b5e-a92e-d498573563d6.jpg" ] }, { "dsmCode": "347930", "dsmTitle": "山地自行车 ST 100 黄色 V2 27,5\" CN", "modelCode": "8808808", "randomItemCode": "4894181", "brandDisplayName": "ROCKRIDER", "label": { "backgroundColor": "#E20C18", "foregroundColor": "#ffffff" }, "modelCount": 2, "price": { "listPrice": 268.00, "activePrice": 69.00, "discountRate": "3.0", "displayDiscountRate": true }, "mainImage": "https://pixl.decathlon.com.cn/p2534286/k$13eb50c021d526da7820213b3202f924/content.jpg", "images": [ "https://pixl.decathlon.com.cn/p2534286/k$13eb50c021d526da7820213b3202f924/content.jpg", "https://pixl.decathlon.com.cn/p2534277/k$e67b20927105664772f2ba785facb395/content.jpg" ] }, { "dsmCode": "331029", "dsmTitle": "26寸EXPLORE 500 儿童运动自行车-普鲁士蓝", "modelCode": "8647409", "randomItemCode": "4319808", "brandDisplayName": "BTWIN", "label": { "backgroundColor": "#E20C18", "foregroundColor": "#ffffff", "text": "尾货商品" }, "modelCount": 3, "price": { "listPrice": 255.00, "activePrice": 187.00, "discountRate": "7.0", "displayDiscountRate": true }, "mainImage": "https://pixl.decathlon.com.cn/p2521747/k$dae4e3b4ad2c5df571cff55aff122d18/content.jpg", "images": [ "https://pixl.decathlon.com.cn/p2521747/k$dae4e3b4ad2c5df571cff55aff122d18/content.jpg", "https://easymerch-oss-pp.decathlon.com.cn/emh-ppd-tf/2025-05-23/8619513_主图_1_b7594758-4629-4593-af34-ede932126ee1.jpg", "https://easymerch-oss-pp.decathlon.com.cn/emh-ppd-tf/2025-05-23/8647410_主图_1_9c6ab1ce-3cd5-4b65-83b1-8ade99e592b8.jpg" ] }, { "dsmCode": "323802", "dsmTitle": "山地车运动头盔100 -蓝色", "modelCode": "8618918", "randomItemCode": "4238470", "brandDisplayName": "ROCKRIDER", "label": { "backgroundColor": "#E20C18", "foregroundColor": "#ffffff" }, "modelCount": 2, "price": { "listPrice": 203.00, "activePrice": 90.00, "discountRate": "4.0", "displayDiscountRate": true }, "mainImage": "https://pixl.decathlon.com.cn/p2496016/k$266ee37127a69ec77d5ed29bb04d5e71/content.jpg", "images": [ "https://pixl.decathlon.com.cn/p2496016/k$266ee37127a69ec77d5ed29bb04d5e71/content.jpg", "https://pixl.decathlon.com.cn/p2496026/k$2703b7f53ea2d36f8816fcc3acf9577b/content.jpg" ] }, { "dsmCode": "12114", "dsmTitle": "轻松刹车刹把(适用于20寸和24寸自行车)", "modelCode": "8299213", "randomItemCode": "1796687", "brandDisplayName": "DECATHLON", "label": { "backgroundColor": "#E20C18", "foregroundColor": "#ffffff", "text": "尾货商品" }, "modelCount": 1, "price": { "listPrice": 246.00, "activePrice": 26.00, "discountRate": "1.0", "displayDiscountRate": true }, "mainImage": "https://pixl.decathlon.com.cn/p2712199/k$050c149e4ac5ffaf1f1e8d442663d086/content.jpg", "images": [ "https://pixl.decathlon.com.cn/p2712199/k$050c149e4ac5ffaf1f1e8d442663d086/content.jpg" ] }, { "dsmCode": "323978", "dsmTitle": "自行车防水鞍座包6 -15升 ", "modelCode": "8579464", "randomItemCode": "2988338", "brandDisplayName": "RIVERSIDE", "label": { "backgroundColor": "#E20C18", "foregroundColor": "#ffffff" }, "modelCount": 1, "price": { "listPrice": 220.00, "activePrice": 127.00, "discountRate": "6.0", "displayDiscountRate": true }, "mainImage": "https://pixl.decathlon.com.cn/p2095854/k$e57b3cc761e99887fd8ef050304994f7/content.jpg", "images": [ "https://pixl.decathlon.com.cn/p2095854/k$e57b3cc761e99887fd8ef050304994f7/content.jpg" ] }, { "dsmCode": "120528", "dsmTitle": "自行车线材和软管切割钳", "modelCode": "8352340", "randomItemCode": "2008175", "brandDisplayName": "DECATHLON", "label": { "backgroundColor": "#E20C18", "foregroundColor": "#ffffff" }, "modelCount": 1, "price": { "listPrice": 220.00, "activePrice": 92.00, "discountRate": "4.0", "displayDiscountRate": true }, "mainImage": "https://pixl.decathlon.com.cn/p732787/k$cc711353cf9a7f3cff2c2d964bc11a17/content.jpg", "images": [ "https://pixl.decathlon.com.cn/p732787/k$cc711353cf9a7f3cff2c2d964bc11a17/content.jpg" ] }, { "dsmCode": "303590", "dsmTitle": "山地自行车定向越野和户外比赛旋转地图夹", "modelCode": "8518787", "randomItemCode": "2611323", "brandDisplayName": "GEONAUTE", "label": { "backgroundColor": "#E20C18", "foregroundColor": "#ffffff" }, "modelCount": 1, "price": { "listPrice": 274.00, "activePrice": 8.00, "discountRate": "0.0", "displayDiscountRate": true }, "mainImage": "https://pixl.decathlon.com.cn/p1882778/k$e27fa121573248a1d14a84bad3b61236/content.jpg", "images": [ "https://pixl.decathlon.com.cn/p1882778/k$e27fa121573248a1d14a84bad3b61236/content.jpg" ] }, { "dsmCode": "349490", "dsmTitle": "26寸-27.5寸-29寸山地自行车前挡泥板V2", "modelCode": "8827450", "randomItemCode": "4966139", "brandDisplayName": "ROCKRIDER", "label": { "backgroundColor": "#E20C18", "foregroundColor": "#ffffff" }, "modelCount": 1, "price": { "listPrice": 214.00, "activePrice": 101.00, "discountRate": "5.0", "displayDiscountRate": true }, "mainImage": "https://pixl.decathlon.com.cn/p2142896/k$41d2006f75963cbc5f59b71c5cc8fe3b/content.jpg", "images": [ "https://pixl.decathlon.com.cn/p2142896/k$41d2006f75963cbc5f59b71c5cc8fe3b/content.jpg" ] }, { "dsmCode": "329622", "dsmTitle": "自行车清洗刷", "modelCode": "8608977", "randomItemCode": "4174576", "brandDisplayName": "DECATHLON", "label": { "backgroundColor": "#E20C18", "foregroundColor": "#ffffff", "text": "尾货商品" }, "modelCount": 1, "price": { "listPrice": 239.00, "activePrice": 47.00, "discountRate": "2.0", "displayDiscountRate": true }, "mainImage": "https://pixl.decathlon.com.cn/p2034992/k$9d6edf43045e26e19c5d8d8d1d2efd80/content.jpg", "images": [ "https://pixl.decathlon.com.cn/p2034992/k$9d6edf43045e26e19c5d8d8d1d2efd80/content.jpg" ] }, { "dsmCode": "10967", "dsmTitle": "ST 500 女士山地车运动头盔-蓝色/粉色", "modelCode": "8540109", "randomItemCode": "2836884", "brandDisplayName": "ROCKRIDER", "label": { "backgroundColor": "#E20C18", "foregroundColor": "#ffffff" }, "modelCount": 2, "price": { "listPrice": 296.00, "activePrice": 121.00, "discountRate": "4.0", "displayDiscountRate": true }, "mainImage": "https://pixl.decathlon.com.cn/p1545905/k$7d024cb6a9dbc7d77f9e4307a55fa207/content.jpg", "images": [ "https://pixl.decathlon.com.cn/p1545905/k$7d024cb6a9dbc7d77f9e4307a55fa207/content.jpg", "https://pixl.decathlon.com.cn/p1759243/k$7c6b503432056698291c703dc9590c06/content.jpg" ] }, { "dsmCode": "346986", "dsmTitle": "RCR FORCE AXS碳纤维竞技公路车", "modelCode": "8802734", "randomItemCode": "4865478", "brandDisplayName": "VAN RYSEL", "label": { "backgroundColor": "#0082C3", "foregroundColor": "#0082C3", "text": "热卖单品" }, "modelCount": 1, "price": { "listPrice": 299.00, "activePrice": 299.00, "discountRate": "10.0", "displayDiscountRate": true }, "mainImage": "https://pixl.decathlon.com.cn/p2558769/k$806a69f4a44d9f11993ba437a08048f9/content.jpg", "images": [ "https://pixl.decathlon.com.cn/p2558769/k$806a69f4a44d9f11993ba437a08048f9/content.jpg" ] }, { "dsmCode": "306941", "dsmTitle": "RC 500铝合金弯把公路车-黑色", "modelCode": "8576490", "randomItemCode": "2966493", "brandDisplayName": "TRIBAN", "label": { "backgroundColor": "#E20C18", "foregroundColor": "#ffffff" }, "modelCount": 1, "price": { "listPrice": 283.00, "activePrice": 144.00, "discountRate": "5.0", "displayDiscountRate": true }, "mainImage": "https://pixl.decathlon.com.cn/p1711608/k$58a4d8d13a8e837c387b14f2ce06e010/content.jpg", "images": [ "https://pixl.decathlon.com.cn/p1711608/k$58a4d8d13a8e837c387b14f2ce06e010/content.jpg" ] }, { "dsmCode": "306293", "dsmTitle": "SPEED500 城市通勤自行车-苍穹绿", "modelCode": "8645977", "randomItemCode": "4306426", "brandDisplayName": "ELOPS", "label": { "backgroundColor": "#E20C18", "foregroundColor": "#ffffff" }, "modelCount": 2, "price": { "listPrice": 201.00, "activePrice": 114.00, "discountRate": "6.0", "displayDiscountRate": true }, "mainImage": "https://pixl.decathlon.com.cn/p2061897/k$0e37d195921338d9943b021068fe7f88/content.jpg", "images": [ "https://pixl.decathlon.com.cn/p2061897/k$0e37d195921338d9943b021068fe7f88/content.jpg", "https://pixl.decathlon.com.cn/p2067039/k$427a104c7b3d8c2609c6fd6c393222fe/content.jpg" ] }, { "dsmCode": "324844", "dsmTitle": "城市自行车Elops 120 C1", "modelCode": "8583722", "randomItemCode": "4016230", "brandDisplayName": "ELOPS", "label": { "backgroundColor": "#E20C18", "foregroundColor": "#ffffff" }, "modelCount": 3, "price": { "listPrice": 298.00, "activePrice": 174.00, "discountRate": "6.0", "displayDiscountRate": true }, "mainImage": "https://easymerch-oss-pp.decathlon.com.cn/emh-ppd-tf/2025-05-23/8583722_主图_1_d58f9176-da6b-4b69-8d00-acbfecfe4380.jpg", "images": [ "https://easymerch-oss-pp.decathlon.com.cn/emh-ppd-tf/2025-05-23/8583722_主图_1_d58f9176-da6b-4b69-8d00-acbfecfe4380.jpg", "https://pixl.decathlon.com.cn/p1863322/k$1fc6e89217c6f4c28de8bc2474588a6a/content.jpg", "https://pixl.decathlon.com.cn/p1863325/k$393cad43ee1680ac845b78a825f0eee8/content.jpg" ] }, { "dsmCode": "325484", "dsmTitle": "旅行自行车TOURING 900", "modelCode": "8587126", "randomItemCode": "4031554", "brandDisplayName": "RIVERSIDE", "label": { "backgroundColor": "#E20C18", "foregroundColor": "#ffffff" }, "modelCount": 1, "price": { "listPrice": 292.00, "activePrice": 96.00, "discountRate": "3.0", "displayDiscountRate": true }, "mainImage": "https://easymerch-oss-pp.decathlon.com.cn/emh-ppd-tf/2025-05-23/8587126_主图_1_cdb0f7ec-9a09-410e-885c-8aa4cba8ce4d.jpg", "images": [ "https://easymerch-oss-pp.decathlon.com.cn/emh-ppd-tf/2025-05-23/8587126_主图_1_cdb0f7ec-9a09-410e-885c-8aa4cba8ce4d.jpg" ] }, { "dsmCode": "301136", "dsmTitle": "山地车运动头盔EXPL500 -米色", "modelCode": "8801809", "randomItemCode": "4856358", "brandDisplayName": "ROCKRIDER", "label": { "backgroundColor": "#E20C18", "foregroundColor": "#ffffff" }, "modelCount": 8, "price": { "listPrice": 209.00, "activePrice": 90.00, "discountRate": "4.0", "displayDiscountRate": true }, "mainImage": "https://pixl.decathlon.com.cn/p2419831/k$97a8ed9b65a4526d710e6bc844162777/content.jpg", "images": [ "https://pixl.decathlon.com.cn/p2419831/k$97a8ed9b65a4526d710e6bc844162777/content.jpg", "https://pixl.decathlon.com.cn/p1767760/k$c7eb2da2036d853e9f90395bc70004c1/content.jpg", "https://pixl.decathlon.com.cn/p1767777/k$0f02aee0520d7afe6c01bc4aa3d7d6ba/content.jpg", "https://pixl.decathlon.com.cn/p2838941/k$422d1b326b534a55dd84533d4c378512/content.jpg", "https://pixl.decathlon.com.cn/p2722452/k$d72de994ecbc38e53fbdb81929b3d1f2/content.jpg", "https://pixl.decathlon.com.cn/p2315383/k$65b37d44d6e4c3a6be60f23725136d0e/content.jpg", "https://pixl.decathlon.com.cn/p2616183/k$48569adbeae81a20f46e6da7cb75d533/content.jpg", "https://pixl.decathlon.com.cn/p2408859/k$221cb0a9b7b14ec43fad4f3f78387473/content.jpg" ] }, { "dsmCode": "337645", "dsmTitle": "EXPLORE 500 大轮径山地自行车-碳灰色", "modelCode": "8733078", "randomItemCode": "4473411", "brandDisplayName": "ROCKRIDER", "label": { "backgroundColor": "#E20C18", "foregroundColor": "#ffffff" }, "modelCount": 2, "price": { "listPrice": 279.00, "activePrice": 114.00, "discountRate": "4.0", "displayDiscountRate": true }, "mainImage": "https://easymerch-oss-pp.decathlon.com.cn/emh-ppd-tf/2025-05-23/8733078_主图_1_5b3014ea-0369-4652-9d94-af79d3e5e1a1.jpg", "images": [ "https://easymerch-oss-pp.decathlon.com.cn/emh-ppd-tf/2025-05-23/8733078_主图_1_5b3014ea-0369-4652-9d94-af79d3e5e1a1.jpg", "https://pixl.decathlon.com.cn/p2502994/k$e08c721f0c5a51f67893e3617a8a2eec/content.jpg" ] }, { "dsmCode": "308639", "dsmTitle": "ST 540 RR 山地自行车 红色 CN", "modelCode": "8549410", "randomItemCode": "2764831", "brandDisplayName": "ROCKRIDER", "label": { "backgroundColor": "#E20C18", "foregroundColor": "#ffffff" }, "modelCount": 2, "price": { "listPrice": 2599.90, "activePrice": 2599.90, "discountRate": "10.0", "displayDiscountRate": true }, "mainImage": "https://pixl.decathlon.com.cn/p2555830/k$2f960fd5b46eeb2bf2e8a1f7977f6141/content.jpg", "images": [ "https://pixl.decathlon.com.cn/p2555830/k$2f960fd5b46eeb2bf2e8a1f7977f6141/content.jpg", "https://pixl.decathlon.com.cn/p2199022/k$7c4f872832319813657355fd262c498a/content.jpg" ] } ], "recommendList": [ ] } }怎么设定断言规则判断seach结果是自行车
最新发布
07-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值