题目链接


菜爆了啊QAQ

记起点为\(S\),终点为\(T\),走过的最靠左的点是\(L\),最靠右的点是\(R\)。

那么坐标轴被分成了五段:

\(0\sim L-1\):经过\(0\)次;

\(L\sim S-1\):经过偶数次;

\(S\sim T\):经过奇数次;

\(T+1\sim R\):经过偶数次;

\(R+1\sim\infty\):经过\(0\)次。

第二段和第四段可以经过\(0\)次(也就是可以没有第二段或第四段)。注意如果是第二/三/四段\(A_i=0\)的话会有花费。

\(f[i][j]\)表示到\(i\)点,处在第\(j\)段,的最小花费。转移很简单。

复杂度\(O(n)\)。


//4ms	640KB
#include <cstdio>
#include <cctype>
#include <algorithm>
//#define gc() getchar()
#define MAXIN 500000
#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
typedef long long LL;
const int N=2e5+5; char IN[MAXIN],*SS=IN,*TT=IN; inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-48,c=gc());
return now;
} int main()
{
int n=read(); LL f[5]={0};
for(int i=1; i<=n; ++i)
{
int ai=read();
LL mn=f[0]; f[0]=f[0]+ai;
mn=std::min(mn,f[1]), f[1]=mn+(ai?ai&1:2);
mn=std::min(mn,f[2]), f[2]=mn+(ai?ai+1&1:1);
mn=std::min(mn,f[3]), f[3]=mn+(ai?ai&1:2);
mn=std::min(mn,f[4]), f[4]=mn+ai;
}
printf("%lld\n",std::min(f[2],std::min(f[3],f[4]))); return 0;
}

Yahoo Programming Contest 2019.D.Ears(DP)的更多相关文章

  1. Yahoo Programming Contest 2019 D - Ears

    D - Ears 思路: s:起点           t:终点           l:左端点           r:右端点 以上称为关键点 dp[i][j]表示到位置 i 为止,已经经过前 j ...

  2. Yahoo Programming Contest 2019.F.Pass(DP)

    题目链接 惊了这是什么F题...怎么我都能做出来...以后atcoder的比赛也不能走神了万一有个这样的F呢(CF已有多次了= =) \(f[i][j]\)表示Takahashi现在一共有\(i\)个 ...

  3. [AtCoder] Yahoo Programming Contest 2019

    [AtCoder] Yahoo Programming Contest 2019   很遗憾错过了一场 AtCoder .听说这场是涨分场呢,于是特意来补一下题. A - Anti-Adjacency ...

  4. Yahoo Programming Contest 2019 补题记录(DEF)

    D - Ears 题目链接:D - Ears 大意:你在一个\(0-L\)的数轴上行走,从整数格出发,在整数格结束,可以在整数格转弯.每当你经过坐标为\(i-0.5\)的位置时(\(i\)是整数),在 ...

  5. 【AtCoder】Yahoo Programming Contest 2019

    A - Anti-Adjacency K <= (N + 1) / 2 #include <bits/stdc++.h> #define fi first #define se se ...

  6. Atcoder Yahoo Programming Contest 2019 简要题解

    A-C 直接放代码吧. A int n,k; int main() { n=read();k=read(); puts(k<=(n+1)/2?"YES":"NO&q ...

  7. Yahoo Programming Contest 2019

    A - Anti-Adjacency 签. #include <bits/stdc++.h> using namespace std; int main() { int n, k; whi ...

  8. Yahoo Programming Contest 2019 自闭记

    A:签到. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> ...

  9. Yahoo Programming Contest 2019 F - Pass

    F - Pass 思路: dp[i][j] 表示到第 i 个球为止放了 j 个蓝球的方案数 第 i 个球来自的位置的最右边是min(i, n) 转移方程看代码 代码: #pragma GCC opti ...

随机推荐

  1. UEFI rootkit 工具LoJax可以感染电脑主板(mainboard)

    1.UEFI(Unified Extensible Firmware Interface)统一扩展接口,UEFI rootkit是以在UEFI中植入rootkit ,18年9月份ESET首次公开了境外 ...

  2. Tmk吃汤饭

    问题 : Tmk吃汤饭 时间限制: 1 Sec  内存限制: 128 MB 题目描述 Tmk喜欢吃汤饭! Tmk喜欢吃汤饭!! Tmk喜欢吃汤饭!!! 汤饭窗口分点餐和取餐两个窗口,一位蜀黍负责点餐窗 ...

  3. ajax-简单参数方法实现阴影效果

    注: 简单参数 (按照参数的数量和位置传递参数) 使用时按照位置.数量传递 shadow.js函数 //简单参数实现方式/** slices:阴影* opacity:透明度* zIndex:层级* * ...

  4. servlet获取多个同名参数

    String[] item = request.getParameterValues("参数名");

  5. Python函数系列之eval()

    1.作用:将字符串str当成有效的表达式来求值并返回计算结果. 2.语法:eval(source[, globals[, locals]])  3.说明:参数:source:一个Python表达式或函 ...

  6. [转] Webpack的devtool和source maps

    source maps Webpack打包生成的.map后缀文件,使得我们的开发调试更加方便,它能帮助我们链接到断点对应的源代码的位置进行调试(//# souceURL),而devtool就是用来指定 ...

  7. Centos6.5下通过shell脚本快速安装samba服务器

    使用方法如下: 上传脚本到linux服务器授权

  8. PUTTY工具的使用

    Putty工具包简单使用 一.Putty简介 Putty是一款远程登录工具,用它可以非常方便的登录到Linux服务器上进行各种操作(命令行方式).Putty完全免费,而且无需安 装(双击即可运行),支 ...

  9. sparkStreaming消费kafka-1.0.1方式:direct方式(存储offset到zookeeper)

    版本声明: kafka:1.0.1 spark:2.1.0 注意:在使用过程中可能会出现servlet版本不兼容的问题,因此在导入maven的pom文件的时候,需要做适当的排除操作 <?xml ...

  10. vue中的provide/inject的学习使用

    irst:定义一个parent component ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <template>  <div> ...