After their adventure with the magic mirror Kay and Gerda have returned home and sometimes give free ice cream to kids in the summer.
At the start of the day they have x ice cream packs. Since the ice cream is free, people start standing in the queue before Kay and Gerda's house even in the night. Each person in the queue wants either to take several ice cream packs for himself and his friends or to give several ice cream packs to Kay and Gerda (carriers that bring ice cream have to stand in the same queue).
If a carrier with d ice cream packs comes to the house, then Kay and Gerda take all his packs. If a child who wants to take d ice cream packs comes to the house, then Kay and Gerda will give him d packs if they have enough ice cream, otherwise the child will get no ice cream at all and will leave in distress.
Kay wants to find the amount of ice cream they will have after all people will leave from the queue, and Gerda wants to find the number of distressed kids.
The first line contains two space-separated integers n and x (1 ≤ n ≤ 1000, 0 ≤ x ≤ 109).
Each of the next n lines contains a character '+' or '-', and an integer di, separated by a space (1 ≤ di ≤ 109). Record "+ di" in i-th line means that a carrier with di ice cream packs occupies i-th place from the start of the queue, and record "- di" means that a child who wants to take di packs stands in i-th place.
Print two space-separated integers — number of ice cream packs left after all operations, and number of kids that left the house in distress.
5 7 + 5 - 10 - 20 + 40 - 20
22 1
5 17 - 16 - 2 - 98 + 100 - 98
3 2
Consider the first sample.
- Initially Kay and Gerda have 7 packs of ice cream.
- Carrier brings 5 more, so now they have 12 packs.
- A kid asks for 10 packs and receives them. There are only 2 packs remaining.
- Another kid asks for 20 packs. Kay and Gerda do not have them, so the kid goes away distressed.
- Carrier bring 40 packs, now Kay and Gerda have 42 packs.
- Kid asks for 20 packs and receives them. There are 22 packs remaining.
AC代码:
#include<iostream>
#include<cstdio>
using namespace std;
long long sum;
int main()
{
int n,x,d;
char c;
while(~scanf("%d%d",&n,&x))
{
sum=x;
int num1=0,num2=0;
for(int i=0;i<n;i++)
{
scanf(" %c%d",&c,&d);
if(c=='-')
d=-d;
if(sum+d>=0)
{
sum+=d;
//num1++;
}
else
num2++;
}
printf("%lld %d\n",sum,num2);
}
}
Little Robber Girl likes to scare animals in her zoo for fun. She decided to arrange the animals in a row in the order of non-decreasing height. However, the animals were so scared that they couldn't stay in the right places.
The robber girl was angry at first, but then she decided to arrange the animals herself. She repeatedly names numbers l and r such thatr - l + 1 is even. After that animals that occupy positions between l and r inclusively are rearranged as follows: the animal at position lswaps places with the animal at position l + 1, the animal l + 2 swaps with the animal l + 3, ..., finally, the animal at position r - 1 swaps with the animal r.
Help the robber girl to arrange the animals in the order of non-decreasing height. You should name at most 20 000 segments, since otherwise the robber girl will become bored and will start scaring the animals again.
The first line contains a single integer n (1 ≤ n ≤ 100) — number of animals in the robber girl's zoo.
The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the height of the animal occupying the i-th place.
Print the sequence of operations that will rearrange the animals by non-decreasing height.
The output should contain several lines, i-th of the lines should contain two space-separated integers li and ri (1 ≤ li < ri ≤ n) — descriptions of segments the robber girl should name. The segments should be described in the order the operations are performed.
The number of operations should not exceed 20 000.
If the animals are arranged correctly from the start, you are allowed to output nothing.
4 2 1 4 3
1 4
7 36 28 57 39 66 69 68
1 4 6 7
5 1 2 1 2 1
2 5 3 4 1 4 1 4
Note that you don't have to minimize the number of operations. Any solution that performs at most 20 000 operations is allowed.
/*
题意:不解释了,注意最后的Note,你不需要将操作的次数规划到最小(重要),
思路:so,可以每次比较相邻的两个,将最小的值不断向前移,
如果可以前移,就输出下标
*/
#include<iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
int a[1005];
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; ++ i)
scanf("%d",&a[i]);
bool flag = false;
do{
flag = true;
for (int i = 1; i < n; i++)
{
if (a[i] > a[i + 1])
{
swap(a[i], a[i + 1]);
flag = false;
printf("%d %d\n",i, i+1);
}
}
}while(flag == false);//条件是 还未是非递减数列
return 0;
}
ps:除了这种循环条件做法,还可以用每次找出最小值,将最小值移到当前循环首部的遍历方法
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches.
First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation.
Note that to display number 0 section of the watches is required to have at least one place.
Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number.
The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 109) — the number of hours in one day and the number of minutes in one hour, respectively.
Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct.
2 3
4
8 2
5
In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2).
In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).