Sir_Kay

Kaysman Official Website


  • 首页

  • 分类

  • 标签

  • 归档

  • 搜索

Codeforces 1238C Standard Free2play 题解

发表于 2019-10-21 更新于 2019-10-26 分类于 算法 , Codeforces
本文字数: 6.9k 阅读时长 ≈ 6 分钟

Codeforces 1238C Standard Free2play 题解

题意

有一个高为$h (1\le h\le10^9)$的悬崖,在高度为$x(1\le x\le h)$的位置上,有一个可移动的平台。

每个平台有两种状态:隐藏在悬崖里 或 在悬崖外。一开始,有$n (1\le n\le\min(h,2\cdot10^5))$个在悬崖外的平台,分别为$p_1,p_2,\dots,p_n$,其中$p_1=h$。

你一开始站在$p_1$上,即在悬崖最高点$h$。若你站在高度为$x$的地方(高度为$x$的平台必须在悬崖外),你可以按下一个按钮。如果你按下了按钮,那么高度为$x$和$x-1$的平台的状态会改变,即高度为$x$的平台隐藏到悬崖里,若高度为$x-1$的平台在悬崖里,则它移动到悬崖外;若它在悬崖外,则隐藏到悬崖里。如果按下按钮后,高度为$x-1$的平台在悬崖外,那么你安全地落在高度为$x-1$的平台上(否则你会掉落在下一个在悬崖外的平台上)。这是唯一一种方式从一个平台移动到另外一个平台。

你可以安全地从$x$掉落到$x-2$,但是如果从$x$掉落在$x-3$或更低处,你就死了。

你可以使用魔法。一次魔法只能使在任何高度的平台改变状态(除了高度为$h$的平台)。

求你使用魔法的最小次数,使得你安全地落在地上(即高度为$0$的地方)。

题解

首先,若你在高度为$x$的平台上,下一个在悬崖外平台的高度为$y$且$x>y+1$,那么你可以安全地、不使用任何魔法,从$x$到达$y+1$。

现在要考虑的是,你在高度为$x$的平台上,有多个连续的、在悬崖外的平台,设它们的高度为$q_1,q_2,\dots,q_n$且$q_1=x,\forall 1\le i<n,q_i=q_{i+1}+1$。

  • 若$n$为奇数,则可以安全地、不使用任何魔法,从$q_1$到达$q_n$(因为每次按下按钮,都只会掉下$2$格)。
  • 若$n$为偶数,则需要一次魔法(如果不使用魔法,在$q_{n-1}$按下按钮时,会掉下$2$格以上),把高度为$q_n+1$的平台移动到悬崖外。

注意,当$q_n=1$时,可以不需要魔法,直接掉落$2$格到地面。

程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// #pragma GCC optimize(2)
// #pragma G++ optimize(2)
// #pragma comment(linker,"/STACK:102400000,102400000")

// #include <bits/stdc++.h>
#include <map>
#include <set>
#include <list>
#include <array>
#include <cfenv>
#include <cmath>
#include <ctime>
#include <deque>
#include <mutex>
#include <queue>
#include <ratio>
#include <regex>
#include <stack>
#include <tuple>
#include <atomic>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <chrono>
#include <cstdio>
#include <cwchar>
#include <future>
#include <limits>
#include <locale>
#include <memory>
#include <random>
#include <string>
#include <thread>
#include <vector>
#include <cassert>
#include <climits>
#include <clocale>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <ctgmath>
#include <cwctype>
#include <fstream>
#include <iomanip>
#include <numeric>
#include <sstream>
#include <ccomplex>
#include <cstdbool>
#include <iostream>
#include <typeinfo>
#include <valarray>
#include <algorithm>
#include <cinttypes>
#include <cstdalign>
#include <stdexcept>
#include <typeindex>
#include <functional>
#include <forward_list>
#include <system_error>
#include <unordered_map>
#include <unordered_set>
#include <scoped_allocator>
#include <condition_variable>
// #include <conio.h>
// #include <windows.h>
using namespace std;

typedef long long LL;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef float fl;
typedef double ld;
typedef long double LD;
typedef pair<int,int> pii;
#if (WIN32) || (WIN64) || (__WIN32) || (__WIN64) || (_WIN32) || (_WIN64) || (WINDOWS)
#define lld "%I64d"
#define llu "%I64u"
#else
#define lld "%lld"
#define llu "%llu"
#endif
#define ui(n) ((unsigned int)(n))
#define LL(n) ((long long)(n))
#define ull(n) ((unsigned long long)(n))
#define fl(n) ((float)(n))
#define ld(n) ((double)(n))
#define LD(n) ((long double)(n))
#define char(n) ((char)(n))
#define Bool(n) ((bool)(n))
#define fixpoint(n) fixed<<setprecision(n)

const int INF=1061109567;
const int NINF=-1044266559;
const LL LINF=4557430888798830399;
const ld eps=1e-15;
#define MOD (1000000007)
#define PI (3.1415926535897932384626433832795028841971)

/*
#define MB_LEN_MAX 5
#define SHRT_MIN (-32768)
#define SHRT_MAX 32767
#define USHRT_MAX 0xffffU
#define INT_MIN (-2147483647 - 1)
#define INT_MAX 2147483647
#define UINT_MAX 0xffffffffU
#define LONG_MIN (-2147483647L - 1)
#define LONG_MAX 2147483647L
#define ULONG_MAX 0xffffffffUL
#define LLONG_MAX 9223372036854775807ll
#define LLONG_MIN (-9223372036854775807ll - 1)
#define ULLONG_MAX 0xffffffffffffffffull
*/

#define MP make_pair
#define MT make_tuple
#define All(a) (a).begin(),(a).end()
#define pall(a) (a).rbegin(),(a).rend()
#define Log(x,y) log(x)/log(y)
#define SZ(a) ((int)(a).size())
#define rep(i,n) for(int i=0;i<((int)(n));i++)
#define rep1(i,n) for(int i=1;i<=((int)(n));i++)
#define repa(i,a,n) for(int i=((int)(a));i<((int)(n));i++)
#define repa1(i,a,n) for(int i=((int)(a));i<=((int)(n));i++)
#define repd(i,n) for(int i=((int)(n))-1;i>=0;i--)
#define repd1(i,n) for(int i=((int)(n));i>=1;i--)
#define repda(i,n,a) for(int i=((int)(n));i>((int)(a));i--)
#define repda1(i,n,a) for(int i=((int)(n));i>=((int)(a));i--)
#define FOR(i,a,n,step) for(int i=((int)(a));i<((int)(n));i+=((int)(step)))
#define repv(itr,v) for(__typeof((v).begin()) itr=(v).begin();itr!=(v).end();itr++)
#define repV(i,v) for(auto i:v)
#define repE(i,v) for(auto &i:v)
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x) MS(x,0)
#define MINF(x) MS(x,63)
#define MCP(x,y) memcpy(x,y,sizeof(y))
#define sqr(x) ((x)*(x))
#define UN(v) sort(All(v)),v.erase(unique(All(v)),v.end())
#define filein(x) freopen(x,"r",stdin)
#define fileout(x) freopen(x,"w",stdout)
#define fileio(x)\
freopen(x".in","r",stdin);\
freopen(x".out","w",stdout)
#define filein2(filename,name) ifstream name(filename,ios::in)
#define fileout2(filename,name) ofstream name(filename,ios::out)
#define file(filename,name) fstream name(filename,ios::in|ios::out)
#define Pause system("pause")
#define Cls system("cls")
#define fs first
#define sc second
#define PC(x) putchar(x)
#define GC(x) x=getchar()
#define Endl PC('\n')
#define SF scanf
#define PF printf

#define j0 J0
#define j1 J1
#define jn Jn
#define y0 Y0
#define y1 Y1
#define yn Yn

inline int Read()
{
int X=0,w=0;char ch=0;while(!isdigit(ch)){w|=ch=='-';ch=getchar();}while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
return w?-X:X;
}
inline void Write(int x){if(x<0)putchar('-'),x=-x;if(x>9)Write(x/10);putchar(x%10+'0');}

inline LL powmod(LL a,LL b){LL RES=1;a%=MOD;assert(b>=0);for(;b;b>>=1){if(b&1)RES=RES*a%MOD;a=a*a%MOD;}return RES%MOD;}
inline LL gcdll(LL a,LL b){return b?gcdll(b,a%b):a;}
const int dx[]={0,1,0,-1,1,-1,-1,1};
const int dy[]={1,0,-1,0,-1,-1,1,1};
/************************************************************BEGIN************************************************************/
const int maxn=200010;

int q,h,n,p[maxn],ans;

int main()
{
SF("%d",&q);
while(q--)
{
ans=0;

SF("%d%d",&h,&n);
rep(i,n) SF("%d",&p[i]);

rep(i,n)
{
int cur=p[i],cnt=1;

while(p[i]==p[i+1]+1&&i+1<n)
{
cnt++;
i++;
}

if((cur==h&&(cnt%2==0)||cur!=h&&(cnt%2==1))&&p[i]!=1) ans++;
}

PF("%d\n",ans);
}

return 0;
}
/*************************************************************END**************************************************************/
__EOF__
DP 数学 贪心
Codeforces 1238D AB-string 题解
Codeforces 587C Duff in the Army 题解
  • 文章目录
  • 站点概览
Sir_Kay

Sir_Kay

Kaysman #1 Sir_Kay
33 日志
6 分类
34 标签
RSS
Main site Wikipedia GitHub GitLab
Creative Commons
  1. 1. Codeforces 1238C Standard Free2play 题解
    1. 1.1. 题意
    2. 1.2. 题解
    3. 1.3. 程序
0%
© 2019 – 2020 Sir_Kay