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操作,在该操作下把该数组变成严格递增所需要的最小修改值是多少 思路:遇到这类题型, ...
随机推荐
- WPF实现ListView大小图标和分组
XAML: <ListView Grid.Row="3" Name="t_lvw_time" HorizontalAlignment="Stre ...
- Mybatis generator(复制粘贴完成)
命令行模式 1.java -jar mybatis-generator-core-x.x.x.jar -configfile generatorConfig.xml 2.Maven plugin(my ...
- [C++]#if !defined 的作用
当你用VC的菜单新增一个类,你会发现自动生成的代码总是类似下面的样子: #if !defined(AFX_XXXX__INCLUDED_) #define AFX_XXXX__INCLUDED_ 具 ...
- AJAX进行分页
新建数据集:PagingDataSet.xsd SELECT * from ( select id, areaID, area, father,Row_Number() over (order by ...
- Linux命令安装vnc服务端与vnc的客户端
第一歩:运行命令 yum install tigervnc-server -y 第二歩:安装telnet 第三歩:运行vncserver,创建桌面 vncserver -kill :1 删除桌面1的 ...
- Linux分享笔记:查看帮助命令 & 常用系统工作命令
在执行命令时,为了防止出现权限不足的问题,在登陆Linux系统时,要点击普通用户名下的 “Not listed?” 用root管理员身份登陆. [1] 执行查看帮助命令 man 这条命令用来查看帮助文 ...
- ajaxfileuplod 上传文件 essyui laoding 效果,防止重复上传文件
//放于上传前 function ajaxLoading(){ $("<div class=\"datagrid-mask\"></div>&qu ...
- MySQL自学笔记_联结(join)
1. 背景及原因 关系型数据库的一个基本原则是将不同细分数据放在单独的表中存储.这样做的好处是: 1).避免重复数据的出现 2).方便数据更新 3).避免创建重复数据时出错 例子: 有供应商信息和产 ...
- [Wolfgang Mauerer] 深入linux 内核架构 第二章 进程管理与调度【未完】
作为Linux开发爱好者,从事linux 开发有三年多时间.做过bsp移植,熟悉u-boot代码执行流程:看过几遍<linux 设备驱动程序开发>,分析过kernel启动流程,写过驱动, ...
- JZOJ 5456. 【NOIP2017提高A组冲刺11.6】奇怪的队列
5456. [NOIP2017提高A组冲刺11.6]奇怪的队列 (File IO): input:queue.in output:queue.out Time Limits: 1000 ms Mem ...