Codeforces Round #325 (Div. 2)

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.

題意:一天有n個課時,但是不是每個課時都有課,(1表示該課時有課,0表示該課時沒課),因爲Alena家離學校比較遠,所以如果這節有課直到下一節有課之間只有小於等於一個【沒課的】課時,那麼Alena就留在學校等待下一節課,否則就回家,求Alena在學校的時間【每個課時算1】

思路:題意相當於:兩個1之間如果少於等於一個0時間,則繼續留在學校【累加】,如果兩個1之間有2個0或以上那麼就回家【跳過】

#include<iostream>
using namespace std;
const int MAXN=105;
int a[MAXN];
int main()
{
	int n,total=0;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	for(int i=0;i<n;i++)
	{
		if(a[i])
		{
			total++;
		}
		else
		{
			if((i-1>=0)&&(i+1<n)&&(a[i-1]&&a[i+1]))
			{
				total++;
			}
		}
	}
	cout<<total<<endl;
	return 0;
}


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 it exactly 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
4
1 2 3
3 2 1
3 2 2 3
output
12
input
3
1 2
3 3
2 1 3
output
11
input
2
1
1
1 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.


題意:就和例圖的一樣,從下面的n個位置去到上面的第一個位置,然後再從上面的第一個位置回到下面的

第n個位置,要求在來回兩次的路徑不能一樣,並且中間的過道只能走一次,求最短的來回值。

思路:如果中間的過道沒限制可能會難一點,但是因爲中間的過道只能走一遍,所以就暴力找從哪個位置走過道就行了,

然後回來再從n-1個過道里面再選一個就行。

#include<iostream>
using namespace std;
const int MAXN=55;
#define INF 65535
int up[MAXN],down[MAXN],street[MAXN];
int main()
{
    int n,a,index,min_go=INF,min_break=INF;
    cin>>n;
    up[n-1]=down[0]=0;
    for(int i=n-2;i>=0;i--)
    {
        cin>>up[i];
    }
    for(int i=n-2;i>=0;i--)
    {
        up[i]+=up[i+1];
    }
    for(int i=n-1;i>=1;i--)
    {
        cin>>down[i];
    }
    for(int i=1;i<=n-1;i++)
    {
        down[i]+=down[i-1];
    }
    for(int i=n-1;i>=0;i--)
    {
        cin>>street[i];
    }
    for(int i=0;i<n;i++)
    {
        if(up[i]+street[i]+down[i]<min_go)
        {
            min_go=up[i]+street[i]+down[i];
            index=i;
        }
    }
    for(int i=0;i<n;i++)
    {
        if(i==index)
        {
            continue;
        }
        if(up[i]+street[i]+down[i]<min_break)
        {
            min_break=up[i]+street[i]+down[i];
        }
    }
    cout<<min_break+min_go<<endl;
    return 0;
}

C. Gennady the Dentist
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Gennady is one of the best child dentists in Berland. Today n children got an appointment with him, they lined up in front of his office.

All children love to cry loudly at the reception at the dentist. We enumerate the children with integers from 1 to n in the order they go in the line. Every child is associated with the value of his cofidence pi. The children take turns one after another to come into the office; each time the child that is the first in the line goes to the doctor.

While Gennady treats the teeth of the i-th child, the child is crying with the volume of vi. At that the confidence of the first child in the line is reduced by the amount of vi, the second one — by value vi - 1, and so on. The children in the queue after the vi-th child almost do not hear the crying, so their confidence remains unchanged.

If at any point in time the confidence of the j-th child is less than zero, he begins to cry with the volume of dj and leaves the line, running towards the exit, without going to the doctor's office. At this the confidence of all the children after the j-th one in the line is reduced by the amount of dj.

All these events occur immediately one after the other in some order. Some cries may lead to other cries, causing a chain reaction. Once in the hallway it is quiet, the child, who is first in the line, goes into the doctor's office.

Help Gennady the Dentist to determine the numbers of kids, whose teeth he will cure. Print their numbers in the chronological order.

Input

The first line of the input contains a positive integer n (1 ≤ n ≤ 4000) — the number of kids in the line.

Next n lines contain three integers each vi, di, pi (1 ≤ vi, di, pi ≤ 106) — the volume of the cry in the doctor's office, the volume of the cry in the hall and the confidence of the i-th child.

Output

In the first line print number k — the number of children whose teeth Gennady will cure.

In the second line print k integers — the numbers of the children who will make it to the end of the line in the increasing order.

Sample test(s)
input
5
4 2 2
4 1 2
5 2 4
3 3 5
5 1 2
output
2
1 3 
input
5
4 5 1
5 3 9
4 1 2
2 1 8
4 1 9
output
4
1 2 4 5 
Note

In the first example, Gennady first treats the teeth of the first child who will cry with volume 4. The confidences of the remaining children will get equal to  - 2, 1, 3, 1, respectively. Thus, the second child also cries at the volume of 1 and run to the exit. The confidence of the remaining children will be equal to 0, 2, 0. Then the third child will go to the office, and cry with volume 5. The other children won't bear this, and with a loud cry they will run to the exit.

In the second sample, first the first child goes into the office, he will cry with volume 4. The confidence of the remaining children will be equal to 5,  - 1, 6, 8. Thus, the third child will cry with the volume of 1 and run to the exit. The confidence of the remaining children will be equal to 5, 5, 7. After that, the second child goes to the office and cry with the volume of 5. The confidences of the remaining children will be equal to 0, 3. Then the fourth child will go into the office and cry with the volume of 2. Because of this the confidence of the fifth child will be 1, and he will go into the office last.


題意:有n個孩子在排隊看牙醫,每個孩子有3個值,分別爲:v:對他後面的孩子造成的傷害值,這個值每距離+1則傷害值-1。 

p:每個孩子的初始血量,d:當血量小於0時,則該孩子對他後面的孩子造成d點傷害,並且血量小於0時,該孩子就會跑  

問牙醫能幫幾個孩子看牙,分別是哪個孩子

思路:主要問題在於題目的理解,模擬一下即可。

有幾個坑點:①.因爲數據可能到10^6,如果第一個孩子進去之後後面n-1個血量都小於0的話,【如果n個人的d都是極限的10^的話,那麼會爆int,所以

要用long long 】 ②如果造成的傷害達到之前,該位置的孩子已經跑了的話,那麼傷害就不會跳過他,對後面孩子排隊的孩子造成傷害。如果造成傷害

後血量小於0,則還會對他後面的孩子造成d的傷害。

#include<iostream>
using namespace std;
const int MAXN = 4005;
long long int v[MAXN], p[MAXN], d[MAXN];
int main()
{
	int n, ans = 0;
	int inter[MAXN];
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> v[i] >> d[i] >> p[i];
	}
	for (int i = 1; i <= n; i++)
	{
		if (p[i]<0)//該孩子已經跑路
		{
			continue;
		}
		inter[ans++] = i;//該孩子進入門裏,看醫生
		long long int tempv = v[i], tempd=0;
		for (int j = i + 1; j <= n; j++)
		{
			if (p[j] < 0)//孩子已經跑路
			{
				continue;
			}
			if (p[j] >= 0)//孩子還在排隊
			{
				p[j] -= (tempv + tempd);
				tempv = (tempv - 1) > 0 ? (tempv - 1) : 0;
				if (p[j] < 0)
				{
					tempd += d[j];
				}
			}
		}
	}
	cout << ans << endl;
	for (int i = 0; i<ans; i++)
	{
		cout << inter[i];
		if (i != ans - 1)
		{
			cout << ' ';
		}
	}
	cout << endl;
	//system("pause");
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章