Codeforces Round #325 (Div. 2) A && B

来源:互联网 发布:番茄鸡蛋打卤面 知乎 编辑:程序博客网 时间:2024/06/07 08:23

【题目链接】:click here~~

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
50 1 0 1 1
output
4
input
71 0 1 0 0 1 0
output
4
input
10
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.


【题意】:去掉前导零,后导零,中间有连续超过2个以上的0忽略,求最后的个数

代码:
#include <bits/stdc++.h>using namespace std;int main(){    //freopen("1.txt","r",stdin);    int a[100000]={0};    int b[100000]={0};    int n;    cin>>n;    for(int i=1; i<=n; ++i) cin>>a[i];    int s=0;    for(int i=1; i<=n; ++i)    {        if(a[i]==0&&(a[i-1]==0||a[i+1]==0)) b[i]=0;        else b[i]=1;        s+=b[i];    }    cout<<s<<endl;}

B. Laurenty and Shop
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A little boy Laurenty has been playing his favourite game Nota for quite a while and is now very hungry. The boy wants to make sausage and cheese sandwiches, but first, he needs to buy a sausage and some cheese.

The town where Laurenty lives in is not large. The houses in it are located in two rows, n houses in each row. Laurenty lives in the very last house of the second row. The only shop in town is placed in the first house of the first row.

The first and second rows are separated with the main avenue of the city. The adjacent houses of one row are separated by streets.

Each crosswalk of a street or an avenue has some traffic lights. In order to cross the street, you need to press a button on the traffic light, wait for a while for the green light and cross the street. Different traffic lights can have different waiting time.

The traffic light on the crosswalk from the j-th house of the i-th row to the (j + 1)-th house of the same row has waiting time equal to aij(1 ≤ i ≤ 2, 1 ≤ j ≤ n - 1). For the traffic light on the crossing from the j-th house of one row to the j-th house of another row the waiting time equals bj (1 ≤ j ≤ n). The city doesn't have any other crossings.

The boy wants to get to the store, buy the products and go back. The main avenue of the city is wide enough, so the boy wants to cross itexactly once on the way to the store and exactly once on the way back home. The boy would get bored if he had to walk the same way again, so he wants the way home to be different from the way to the store in at least one crossing.

Figure to the first sample.

Help Laurenty determine the minimum total time he needs to wait at the crossroads.

Input

The first line of the input contains integer n (2 ≤ n ≤ 50) — the number of houses in each row.

Each of the next two lines contains n - 1 space-separated integer — values aij (1 ≤ aij ≤ 100).

The last line contains n space-separated integers bj (1 ≤ bj ≤ 100).

Output

Print a single integer — the least total time Laurenty needs to wait at the crossroads, given that he crosses the avenue only once both on his way to the store and on his way back home.

Sample test(s)
input
41 2 33 2 13 2 2 3
output
12
input
31 23 32 1 3
output
11
input
2111 1
output
4
Note

The first sample is shown on the figure above.

In the second sample, Laurenty's path can look as follows:

  • Laurenty crosses the avenue, the waiting time is 3;
  • Laurenty uses the second crossing in the first row, the waiting time is 2;
  • Laurenty uses the first crossing in the first row, the waiting time is 1;
  • Laurenty uses the first crossing in the first row, the waiting time is 1;
  • Laurenty crosses the avenue, the waiting time is 1;
  • Laurenty uses the second crossing in the second row, the waiting time is 3.
In total we get that the answer equals 11.

In the last sample Laurenty visits all the crossings, so the answer is 4.

【题意】:固定一个起点,一个终点,从起点走到终点然后又回到起点的最短路径,注意每次只能穿越竖直道路一次,且不能重复,其他两条水平路径可以重复走,看到题目,直接想到最短路径问题,然后就陷入思维的胡同了,然后比赛快结束了,看到数据很小,过的人很多,应该不是这么想,然后就先想到直接暴力三种可能,两条水平的,一条竖直的,这里用前缀和记录一下走过的路径,也就是记录第一行路的前缀,第二行从后往前记录,然后枚举竖着的列,最后取一个最小值就是答案。
代码:
#include <stdio.h>#include <math.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;const int maxn=1e5+10;int a[maxn],b[maxn],c[maxn];int sa[maxn]= {0},sb[maxn]= {0};int get(int i){    return sa[i-1]+sb[i]+c[i];}int main(){    int n;    cin>>n;    for(int i=1; i<=n-1; ++i)    {        cin>>a[i];        sa[i]=sa[i-1]+a[i];    }    for(int i=1; i<=n-1; ++i) cin>>b[i];    for(int i=n-1; i>=1; --i)    {        sb[i]=sb[i+1]+b[i];    }    for(int i=1; i<=n; ++i) cin>>c[i];    int ans=99999999999;    for(int i=1; i<=n; ++i)        for(int j=1; j<=n; ++j)        {            if(i==j) continue;            ans=min(ans,get(i)+get(j));        }    cout<<ans<<endl;    return 0;}

ps:#325正好是寝室号,本来想攒攒rp,结果比赛最后10分钟系统提示A题被别人HACK,囧~~


0 1
原创粉丝点击