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 ;
}

最新文章

  1. EntityFramework.Extended 支持 MySql
  2. Map与Tuple
  3. hdu 1232, disjoint set, linked list vs. rooted tree, a minor but substantial optimization for path c 分类: hdoj 2015-07-16 17:13 116人阅读 评论(0) 收藏
  4. oledbdataadapter 读取excel数据时,有的单元格内容不能读出
  5. Python format格式化输出
  6. 50个令人惊奇的jQuery插件(对话框和表单篇)及免费的响应式bootstrap管理员后台界面主题 - Charisma
  7. easyui datagrid columns field 如何支持一个或多个子属性
  8. django 单独测试模块
  9. jquery easyui的layout
  10. js内置对象处理-打印学生成绩单
  11. 验证码 Demo
  12. delphi 使用superobject实现jsonrpc的http远程调用 good
  13. web开发性能优化---用户体验篇
  14. Linux Shell脚本攻略
  15. Entity Framework 6.x Code First 基础
  16. Postgres中的物化节点之sort节点
  17. 求链表倒数第n个元素
  18. 【翻译】使用Sencha Ext JS创建美丽的图画(1)
  19. Win8 HTML5与JS编程学习笔记(一)
  20. 从网卡发送数据再谈TCP/IP协议—网络传输速度计算-网卡构造

热门文章

  1. python函数 -- 作用域,异常处理
  2. Storm提交Topology报错:Found multiple defaults.yaml resources.
  3. hello2源代码分析
  4. P1828香甜的黄油
  5. RuntimeError: Model class user.models.User doesn&#39;t declare an explicit app_label and isn&#39;t in an application in INSTALLED_APPS.
  6. # Clion复制提示信息
  7. [BZOJ2594] [WC2006]水管局长(Kruskal+LCT)
  8. 怎么快速写好看的手机menu菜单
  9. java中如果删除导入的jar包,工程出现叹号解决方案
  10. SQL--合计函数(Aggregate functions):avg,count,first,last,max,min,sum