Codeforces Round #359 (Div. 2)

本文解析了Codeforces平台上的两道编程题:免费冰淇淋问题探讨如何处理顾客与送货者队列,确保服务流程顺利进行;小强盗女孩的动物园则通过一系列操作实现动物按身高排序的目标。文章提供了详细的算法思路与实现代码。
A. Free Ice Cream
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The first line contains two space-separated integers n and x (1 ≤ n ≤ 10000 ≤ 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.

Output

Print two space-separated integers — number of ice cream packs left after all operations, and number of kids that left the house in distress.

Examples
input
5 7
+ 5
- 10
- 20
+ 40
- 20
output
22 1
input
5 17
- 16
- 2
- 98
+ 100
- 98
output
3 2
Note

Consider the first sample.

  1. Initially Kay and Gerda have 7 packs of ice cream.
  2. Carrier brings 5 more, so now they have 12 packs.
  3. A kid asks for 10 packs and receives them. There are only 2 packs remaining.
  4. Another kid asks for 20 packs. Kay and Gerda do not have them, so the kid goes away distressed.
  5. Carrier bring 40 packs, now Kay and Gerda have 42 packs.
  6. Kid asks for 20 packs and receives them. There are 22 packs remaining.

#include <iostream>

using namespace std;

int main()
{
    long long ans,n,kid,num;
    char aim;
    while(cin>>n>>ans){
        kid=0;
        while(n--){
            cin>>aim>>num;
            if(aim=='+') ans+=num;
            else{
                if(ans<num)
                    kid++;
                else
                    ans-=num;
            }
        }
        cout<<ans<<' '<<kid<<endl;
    }
}

B. Little Robber Girl's Zoo
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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.

Output

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.

Examples
input
4
2 1 4 3
output
1 4
input
7
36 28 57 39 66 69 68
output
1 4
6 7
input
5
1 2 1 2 1
output
2 5
3 4
1 4
1 4
Note

Note that you don't have to minimize the number of operations. Any solution that performs at most 20 000 operations is allowed.


题目对排序的方法没有限制,只限制步数在20000内。故直接暴力即可。
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int a[105];
int b[105];
int n;

void solve(int s,int k){
    cout<<s<<' '<<k<<endl;
    for(;s<=k;s+=2)
        swap(a[s],a[s+1]);
    for(int i=1;i<=n;i++)
        cout<<a[i]<<' ';
    cout<<endl;

}
int main()
{
    cin>>n;
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    for(int i=1;i<=n;i++){
        cin>>a[i];
        b[i]=a[i];
    }
    sort(b+1,b+n+1);
    for(int i=1;i<=n;i++)
        cout<<b[i]<<' ';
    cout<<endl;
    for(int i=1;i<=n;i++){
        if(a[i]!=b[i]){
            int s=i,k=0,e;
            for(k=s+1;k<=n;k+=2){
                if(a[k]==b[k]){
                    swap()
                }
            }
            solve(s,e);
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值