A. Alena's Schedule
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Alena has successfully passed the entrance exams to the university and is now looking forward to start studying.

One two-hour lesson at the Russian university is traditionally called a pair, it lasts for two academic hours (an academic hour is equal to 45 minutes).

The University works in such a way that every day it holds exactly n lessons. Depending on the schedule of a particular group of students, on a given day, some pairs may actually contain classes, but some may be empty (such pairs are called breaks).

The official website of the university has already published the schedule for tomorrow for Alena's group. Thus, for each of the n pairs she knows if there will be a class at that time or not.

Alena's House is far from the university, so if there are breaks, she doesn't always go home. Alena has time to go home only if the break consists of at least two free pairs in a row, otherwise she waits for the next pair at the university.

Of course, Alena does not want to be sleepy during pairs, so she will sleep as long as possible, and will only come to the first pair that is presented in her schedule. Similarly, if there are no more pairs, then Alena immediately goes home.

Alena appreciates the time spent at home, so she always goes home when it is possible, and returns to the university only at the beginning of the next pair. Help Alena determine for how many pairs she will stay at the university. Note that during some pairs Alena may be at the university waiting for the upcoming pair.

Input

The first line of the input contains a positive integer n (1 ≤ n ≤ 100) — the number of lessons at the university.

The second line contains n numbers ai (0 ≤ ai ≤ 1). Number ai equals 0, if Alena doesn't have the i-th pairs, otherwise it is equal to 1. Numbers a1, a2, ..., an are separated by spaces.

Output

Print a single number — the number of pairs during which Alena stays at the university.

Sample test(s)
input
5
0 1 0 1 1
output
4
input
7
1 0 1 0 0 1 0
output
4
input
1
0
output
0
Note

In the first sample Alena stays at the university from the second to the fifth pair, inclusive, during the third pair she will be it the university waiting for the next pair.

In the last sample Alena doesn't have a single pair, so she spends all the time at home.

题目分析:原始题意见题目,转化后:转化后给你一连串的01字符串,求其中单独的1和处于101状态的0出现的次数,,转化忽的题意是看了别人的才想到的,自己的又是非常笨拙的暴力

先贴上好的代码

 #include<cstdio>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long LL;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
int a[105];
int main()
{
int n;
while(~scanf("%d",&n))
{
int cnt=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]==1)
cnt++;
}
for(int i=1;i<=n-2;i++)
if(a[i]==1&&a[i+1]==0&&a[i+2]==1)
cnt++;
printf("%d\n",cnt);
}
return 0;
}

  再贴上自己的挫的代码,水题也要得进步

#include<cstdio>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long LL;
typedef unsigned long long ULL;
const int mod = ;
const double eps = 1e-;
const int inf = 0x3f3f3f3f;
int a[];
int main()
{
int n;
while(~scanf("%d",&n))
{
int s=,e=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]&&i<s)
s=i;
if(a[i]&&i>e)
e=i;
}
if(s==)
{
printf("0\n");
continue;
}
int cnt=e-s+;
for(int i=s;i<=e;)
{
int j=i;
if(a[i]==)
{
while(a[i]==)
i++;
if(i-j>=)
cnt-=(i-j);
}
else i++;
}
printf("%d\n",cnt);
}
return ;
}

Codeforces Round #325 (Div. 2) A. Alena's Schedule 暴力枚举 字符串的更多相关文章

  1. Codeforces Round #325 (Div. 2) A. Alena's Schedule 水题

    A. Alena's Schedule Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/586/pr ...

  2. Codeforces Round #325 (Div. 2) C. Gennady the Dentist 暴力

    C. Gennady the Dentist Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/586 ...

  3. Codeforces Round #166 (Div. 2) A. Beautiful Year【暴力枚举/逆向思维/大于当前数且每个位数不同】

    A. Beautiful Year time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  4. Codeforces Round #447 (Div. 2) A. QAQ【三重暴力枚举】

    A. QAQ time limit per test 1 second memory limit per test 256 megabytes input standard input output ...

  5. Codeforces Round #297 (Div. 2)D. Arthur and Walls 暴力搜索

    Codeforces Round #297 (Div. 2)D. Arthur and Walls Time Limit: 2 Sec  Memory Limit: 512 MBSubmit: xxx ...

  6. Codeforces Round #325 (Div. 2) B. Laurenty and Shop 前缀和

    B. Laurenty and Shop Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/586/p ...

  7. Codeforces Round #325 (Div. 2) A

    A. Alena's Schedule time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. Codeforces Round #325 (Div. 2)

    水 A - Alena's Schedule /************************************************ * Author :Running_Time * Cr ...

  9. Codeforces Round #306 (Div. 2) C. Divisibility by Eight 暴力

    C. Divisibility by Eight Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...

随机推荐

  1. ETL初探

    初识ETL 概念 ETL即Extract-Transform-Load.目的是将分散.凌乱.异质的数据整合在一起,为决策提供分析数据,是BI项目(Business Intellifence)项目中重要 ...

  2. 2019中山纪念中学夏令营-Day1[JZOJ]

    T1 题目描述: 1999. Wexley接苹果(apple) (File IO): input:apple.in output:apple.out 时间限制: 1000 ms  空间限制: 1280 ...

  3. k8s 1.9安装

    关闭所有节点的selinux.iptables.firewalld systemctl stop iptables systemctl stop firewalld systemctl disable ...

  4. Alibaba开源组件-分布式流量控制框架sentinel初探

    Alibaba开源组件-分布式流量控制框架sentinel初探 2018年12月09日 18:23:11 SuperPurse 阅读数 1965更多 分类专栏: J2EE   版权声明:本文为博主原创 ...

  5. 1.bash总体介绍

    1.总体介绍1.1 什么是Bash?Bash(Borune-Again SHell)是一个用于Linux操作系统的shell,即命令解释器Bash与sh兼容,并从ksh和csh引进了一些有用的功能,在 ...

  6. html/css中map和area的应用

    一.使用方法: 因为map标签是与img标签绑定使用的,所以我们需要给map标签添加ID和name属性,让img标签中的usemap属性引用map标签中的id或者name属性(由于浏览器的不同,use ...

  7. python、mysql三-3:完整性约束

    一 介绍 约束条件与数据类型的宽度一样,都是可选参数 作用:用于保证数据的完整性和一致性主要分为: PRIMARY KEY (PK) 标识该字段为该表的主键,可以唯一的标识记录 FOREIGN KEY ...

  8. C语言字符串操作小结

    1)字符串操作strcpy(p, p1) 复制字符串strncpy(p, p1, n) 复制指定长度字符串strcat(p, p1) 附加字符串strncat(p, p1, n) 附加指定长度字符串s ...

  9. 【未知来源】Happy

    题意 给出一个 \(n\) 个节点的树,两点之间有且仅有一条路径相连. 给出 \(m\) 个点对 \(x_i,y_i\),如果添加一条双向边 \((u,v)\) 后 \(x_i\) 和 \(y_i\) ...

  10. crt0.o

    crt1.o, crti.o, crtbegin.o, crtend.o, crtn.o 等目标文件和daemon.o(由我们自己的C程序文件产生)链接成一个执行文件.前面这5个目标文件的作用分别是启 ...