Codeforces Round #363 (Div. 2) C dp或贪心 两种方法
Description
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:
- on this day the gym is closed and the contest is not carried out;
- on this day the gym is closed and the contest is carried out;
- on this day the gym is open and the contest is not carried out;
- on this day the gym is open and the contest is carried out.
On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).
Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.
Input
The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations.
The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where:
- ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out;
- ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out;
- ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out;
- ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.
Output
Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:
- to do sport on any two consecutive days,
- to write the contest on any two consecutive days.
Sample Input
4
1 3 2 0
2
7
1 3 3 2 1 2 3
0
2
2 2
1 题意:给你n天的情况
0 代表休息
1 代表只能参加contest或休息
2 代表只能参加gym或休息
3 代表能参加contest或gym或休息
要求 不能连续参加contest 不能连续参加gym
问 如何安排使得休息日最少 输出休息日的数量 题解: dp处理 dp[i][j] 代表 第i天进行j活动的情况下的前i天的最多非休息日的数量
三个状态的转移方程
dp[i][1]=max(dp[i-1][2],dp[i-1][0])+1;
dp[i][2]=max(dp[i-1][1],dp[i-1][0])+1;
dp[i][0]=max(max(dp[i-1][0],dp[i-1][1]),dp[i-1][2]); 结果为n-max(max(dp[n][0],dp[n][1]),dp[n][2])
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int n;
int a[];
int dp[][];
int main()
{
scanf("%d",&n);
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
if(a[]==)
dp[][]=;
if(a[]==)
dp[][]=;
if(a[]==)
dp[][]=;
if(a[]==)
{
dp[][]=;
dp[][]=;
dp[][]=;
}
for(int i=;i<=n;i++)
{
if(a[i]==)
{
dp[i][]=max(dp[i-][],dp[i-][])+;
dp[i][]=max(max(dp[i-][],dp[i-][]),dp[i-][]);
}
if(a[i]==)
{
dp[i][]=max(dp[i-][],dp[i-][])+;
dp[i][]=max(max(dp[i-][],dp[i-][]),dp[i-][]);
}
if(a[i]==)
{
dp[i][]=max(max(dp[i-][],dp[i-][]),dp[i-][]);
}
if(a[i]==)
{
dp[i][]=max(dp[i-][],dp[i-][])+;
dp[i][]=max(dp[i-][],dp[i-][])+;
dp[i][]=max(max(dp[i-][],dp[i-][]),dp[i-][]);
}
}
int ans=;
for(int i=;i<=;i++)
ans=max(ans,dp[n][i]);
cout<<n-ans<<endl;
return ;
}
贪心处理 我gou代码
重点在3状态的处理上 若当前状态为3 则判断前一天状态
若前一天为1状态 则 将当前状态改为2 非休息日++
若前一天为2状态 则 将当前状态改为1 非休息日++
若前一天为0状态 则 将当前状态改为0 非休息日++ 当前状态改为0意味着 无论后一个状态为什么
当前这一天都有相应的状态可以更改 使得当前这一天为非休息日 即当前这一天 对后一天没有影响。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <sstream>
#include <vector>
#define PI acos(-1.0)
const int inf = (<<) - ;
using namespace std;
inline int get_int()
{
int r=;
char c;
while((c=getchar())!=' '&&c!='\n')
r=r*+c-'';
return r;
}
inline void out(int x)
{
if(x>)
{
out(x/);
}
putchar(x % + '');
putchar('\n');
}
/****************************************/
int a[];
int main()
{
int n,m;
cin>>n;
for(int i=;i<n;i++){
scanf("%d",&a[i]);
}
m=;
if(a[]>){
m++;
}
if(a[]==)
a[]=;
for(int i=;i<n;i++){
if(a[i]==){
if(a[i-]!=)
m++;
else a[i]=;
}
if(a[i]==){
if(a[i-]!=)
m++;
else a[i]=;
}
if(a[i]==){
if(a[i-]==){
a[i]=;
m++;
}
if(a[i-]==){
a[i]=;
m++;
}
if(a[i-]==){
a[i]=;
m++;
}
}
}
printf("%d\n",n-m);
return ;
}
另外附一种奇怪搞法 逻辑运算处理
http://blog.csdn.net/nare123/article/details/51966794
Codeforces Round #363 (Div. 2) C dp或贪心 两种方法的更多相关文章
- Codeforces Round 363 Div. 1 (A,B,C,D,E,F)
Codeforces Round 363 Div. 1 题目链接:## 点击打开链接 A. Vacations (1s, 256MB) 题目大意:给定连续 \(n\) 天,每天为如下四种状态之一: 不 ...
- Codeforces Round #363 (Div. 2) C. Vacations(DP)
C. Vacations time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- Codeforces Round #363 (Div. 2)
A题 http://codeforces.com/problemset/problem/699/A 非常的水,两个相向而行,且间距最小的点,搜一遍就是答案了. #include <cstdio& ...
- Codeforces Round #541 (Div. 2) G dp + 思维 + 单调栈 or 链表 (连锁反应)
https://codeforces.com/contest/1131/problem/G 题意 给你一排m个的骨牌(m<=1e7),每块之间相距1,每块高h[i],推倒代价c[i],假如\(a ...
- Codeforces Round #543 (Div. 2) F dp + 二分 + 字符串哈希
https://codeforces.com/contest/1121/problem/F 题意 给你一个有n(<=5000)个字符的串,有两种压缩字符的方法: 1. 压缩单一字符,代价为a 2 ...
- Codeforces Round #363 Div.2[111110]
好久没做手生了,不然前四道都是能A的,当然,正常发挥也是菜. A:Launch of Collider 题意:20万个点排在一条直线上,其坐标均为偶数.从某一时刻开始向左或向右运动,速度为每秒1个单位 ...
- Codeforces Round #652 (Div. 2) E. DeadLee(贪心)
题目链接:https://codeforces.com/contest/1369/problem/E 题意 Lee 有 $n$ 种不同种类的食物和 $m$ 个朋友,每种食物有 $w_i$ 个,每个朋友 ...
- Codeforces Round #363 (Div. 2) C. Vacations —— DP
题目链接:http://codeforces.com/contest/699/problem/C 题解: 1.可知每天有三个状态:1.contest ,2.gym,3.rest. 2.所以设dp[i] ...
- 严格递增类的dp Codeforces Round #371 (Div. 1) C dp
http://codeforces.com/contest/713 题目大意:给你一个长度为n的数组,每次有+1和-1操作,在该操作下把该数组变成严格递增所需要的最小修改值是多少 思路:遇到这类题型, ...
随机推荐
- theano 模块 MLP示例
theano 模块 MLP示例,有需要的朋友可以参考下. theano教程Example: MLP: 约定数组为列向量, 层级:将多层传感器定义为一连串的层级,每个层级定义为一个类.类属性包括:权重. ...
- display:inline-block解决文字有间隙问题
定义:display:inline-block是使元素以块级元素的形式呈现在行内.意思就是说,让这个元素显示在同一行不换行,但是又可以控制高度和宽度,这相当于内联元素的增强. 但是display:in ...
- 牛客NOIP普及组R1 C括号(dp)
题意 题目链接 Sol maya普及组的dp都要想很长时间,我真是越来越菜了qwq 设$f[i][j]$表示当前到第$i$个位置,剩下$j$个左括号没被匹配 转移的时候判断一下即可 /* */ #in ...
- BZOJ2287: 【POJ Challenge】消失之物(背包dp)
题意 ftiasch 有 N 个物品, 体积分别是 W1, W2, ..., WN. 由于她的疏忽, 第 i 个物品丢失了. “要使用剩下的 N - 1 物品装满容积为 x 的背包,有几种方法呢?” ...
- Atlas 配置高可用
keepalived安装 #下载keepalived ./configure --prefix=/usr/local Make && make install Atlas主安装keep ...
- jsp 生成验证码代码
调用方法:在jsp页面用图像标签便可以直接调用如下是标签代码<img border=0 src="image.jsp">,只需要把该代码发在验证码要显示的区域就可以了) ...
- Date.prototype.Format---对Date的扩展
// 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占 ...
- LeetCode954二倍数对数组
问题:二倍数对数组 给定一个长度为偶数的整数数组 A,只有对 A 进行重组后可以满足 “对于每个 0 <= i < len(A) / 2,都有 A[2 * i + 1] = 2 * A[2 ...
- jupyter notebook(三)——IOPub_data_rate_limit报错
一.问题 运行jupyter notebook,然后运行python代码,读取文件处理时,会报错.发现时IO读取时错误.应该是IO速率问题. 下面是问题报错: IOPub data rate exce ...
- 算法图解之大O表示法
什么是大O表示法 大O表示法可以告诉我们算法的快慢. 大O比较的是操作数,它指出了算法运行时间的增速. O(n) 括号里的是操作数. 举例 画一个16个格子的网格,下面分别列举几种不同的画法,并用大O ...