Educational Codeforces Round 74 (Rated for Div. 2) C. Standard Free2play ###K ###K //K
题目链接:https://codeforces.ml/contest/1238/problem/C
题意:1~n 都有一个平面,有些平面隐藏,有些平面显露, 每次走的时候 会将当前位置和下一个位置的平面取反,人只能站在显露的平面上,且一次跨越的高度不能超过2,用一个水晶能将任意平面取反
问最少多少水晶能从n 走到0
思路:贪心, 每次走到p[i] +1 的位置, 然后看能不能跳两步,可以就跳 否则就跳一步ans++
贪心的做法
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=2e5+10; 4 const int mod=998244353; 5 #define ll long long 6 #define pi pair<ll,ll> 7 #define fi first 8 #define sc second 9 #define pb push_back 10 int p[maxn]; 11 12 13 int main() 14 { 15 ios::sync_with_stdio(0); 16 cin.tie(0); 17 int t; 18 cin>>t; 19 while(t--) 20 { 21 int h,n; 22 cin>>h>>n; 23 for(int i=1;i<=n;i++) cin>>p[i]; 24 int ans=0; 25 int now=h; 26 for(int i=2;i<=n;i++) 27 { 28 now=p[i]+1; 29 if(now<=2) break; 30 if(i!=n&&p[i]==p[i+1]+1) 31 { 32 i++; 33 } 34 else 35 ans++; 36 } 37 cout<<ans<<'\n'; 38 } 39 40 41 42 43 44 45 }
dp比贪心更好写
dp的做法 最后处理的是 能到2的就肯定能到0
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 #define pb push_back 5 const int maxn =2e5+10; 6 const int mod=998244353; 7 int a[maxn]; 8 int dp[maxn]; 9 10 11 12 13 int main() 14 { 15 ios::sync_with_stdio(false); 16 cin.tie(0); 17 int q; 18 cin>>q; 19 while(q--) 20 { 21 int h,n; 22 cin>>h>>n; 23 memset(dp,0,sizeof(dp)); 24 for(int i=1;i<=n;i++) 25 { 26 cin>>a[i]; 27 } 28 dp[1]=0; 29 dp[2]=1; 30 for(int i=3;i<=n;i++) 31 { 32 if(a[i-1]-a[i]==1) 33 { 34 dp[i]=min(dp[i-2],dp[i-1]+1); 35 } 36 else 37 { 38 dp[i]=dp[i-1]+1; 39 } 40 } 41 int ans=1e9; 42 43 for(int i=1;i<=n;i++) 44 { 45 if(a[i]>=2) 46 { 47 ans=dp[i]; 48 } 49 } 50 ans=min(ans,dp[n]); 51 cout<<ans<<'\n'; 52 } 53 54 55 56 57 58 59 60 61 62 }

浙公网安备 33010602011771号