Vacations(Codeforces Round #363 (Div1 A)
2016-07-22 23:12
393 查看
http://codeforces.com/problemset/problem/698/A
A. Vacations
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Examples
input
output
input
output
input
output
Note
In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.
In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.
In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
另外分享两个在Codeforces里看到的大牛的代码,愣是没看懂。。。
#include<stdio.h>
#include<string.h>
int main(){
int n,i,a,now=3,ans=0;
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%d",&a);
if((now&a)==0){
now=3;
ans++;
}else{
now&=a;
if(now==1)now=2;
else if(now==2)now=1;
}
}
printf("%d\n",ans);
return 0;
}
#include <bits/stdc++.h>
using namespace std ;
int main ()
{
int n,out=0,now; cin>>n;
int a[n+1];a[0]=3;
for (int i=1;i<=n;i++)
{
cin>>a[i];
if (a[i]==a[i-1]&&a[i]!=3) a[i]=0;
else if (a[i]==3&&a[i-1]!=3) a[i]-=a[i-1];
if (a[i]==0)out++;
}
A. Vacations
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Examples
input
4 1 3 2 0
output
2
input
7 1 3 3 2 1 2 3
output
0
input
22 2
output
1
Note
In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.
In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.
In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
题目的大致意思是:一个人有n天时间,每一天他可以休息、可以比赛、也可以去体育馆锻炼(当然去体育馆要求体育馆必须是开着的,比赛也必须在比赛进行的时候),分别记为活动0、活动1、活动2,要求是不能连续两天都比赛或者都锻炼,求休息天数的最小值。
记dp[i][j]为共有i天,第i天做活动j时休息天数的最小值,则有
dp[i][0]=min(dp[i-1][0],dp[i-1][1],dp[i-1][2])+1;
dp[i][1]=min(dp[i-1][0],dp[i-1][2]);
dp[i][2]=min(dp[i-1][0],dp[i-1][1]).
AC代码如下:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int dp[105][3]; int num[105]; int main() { int n; scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&num[i]); } memset(dp,0x3f,sizeof(dp));//别问我为什么,大神就是这样写的,意思就是把dp都赋尽可能大的值。下面注释掉的也能AC /* for(int i=1;i<105;i++){ for(int j=0;j<3;j++){ dp[i][j]=105; } } */ memset(dp[0],0,sizeof(dp[0])); for(int i=1;i<=n;i++){ dp[i][0]=min(dp[i-1][0],min(dp[i-1][1],dp[i-1][2]))+1; if(num[i]==1||num[i]==3){ dp[i][1]=min(dp[i-1][0],dp[i-1][2]); } if(num[i]==2||num[i]==3){ dp[i][2]=min(dp[i-1][0],dp[i-1][1]); } } printf("%d",min(dp [0],min(dp [1],dp [2]))); return 0; }
另外分享两个在Codeforces里看到的大牛的代码,愣是没看懂。。。
#include<stdio.h>
#include<string.h>
int main(){
int n,i,a,now=3,ans=0;
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%d",&a);
if((now&a)==0){
now=3;
ans++;
}else{
now&=a;
if(now==1)now=2;
else if(now==2)now=1;
}
}
printf("%d\n",ans);
return 0;
}
#include <bits/stdc++.h>
using namespace std ;
int main ()
{
int n,out=0,now; cin>>n;
int a[n+1];a[0]=3;
for (int i=1;i<=n;i++)
{
cin>>a[i];
if (a[i]==a[i-1]&&a[i]!=3) a[i]=0;
else if (a[i]==3&&a[i-1]!=3) a[i]-=a[i-1];
if (a[i]==0)out++;
}
相关文章推荐
- asp Fix、Int、Round、CInt函数使用说明
- PHP四舍五入、取整、round函数使用示例
- sqlserver四舍五入使用round函数及cast和convert函数
- PHP中round()函数对浮点数进行四舍五入的方法
- php取整函数ceil,floo,round的用法及介绍
- C#使用round函数四舍五入的方法
- JavaScript里四舍五入函数round用法实例
- round robin权重轮循算法php实现代码
- python学之round()函数
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)
- Codeforces 405E Codeforces Round #238 (Div. 2)E
- Codeforces 407C Codeforces Round #239 (Div. 1)C
- CodeForces 449A - Jzzhu and Chocolate
- CodeForces 449 B. Jzzhu and Cities
- codeforces 618C. Constellation
- Codeforces Round #349 (Div. 2) - C
- Codeforces Round #361 (Div. 2)
- 7.13Codeforces Round #360 (Div. 2)
- php 数字格式化保留两位有效小数