codeforces #359 前三题题解

本文介绍三道编程竞赛题目,包括免费冰淇淋分配算法、动物园动物排序问题及强盗手表的时间计算。通过详细解析题意与提供AC代码,帮助读者理解并解决这些问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

 

题目大意:看Note直接就可以看懂,看题干太长,大概意识就是说,输入一个n表示有n个操作,输入一个m表示初始的时候有m个点数,然后n个操作对应有+或者-,如果+直接加上点数,如果-,如果点数够减就减,如果不够减就不减,并且计数器加一,表示有一次不够减,然后输出最终的点数和不够减的次数。


AC代码:

#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
    int n;
    long long int m;
    while(~scanf("%d%I64d",&n,&m))
    {
        int cnt=0;
        for(int i=0;i<n;i++)
        {
            char s[5];
            scanf("%s",s);
            if(s[0]=='+')
            {
                long long int  x;
                scanf("%I64d",&x);
                m+=x;
            }
            else
            {
                long long int  x;
                scanf("%I64d",&x);
                if(m-x>=0)m-=x;
                else cnt++;
            }
        }
        printf("%I64d %d\n",m,cnt);
    }
}

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

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.

 

题目大意:给你一个序列,让你经过一些操作,使得其变成递增序列,也就是让你排序,每一次操作选择一个偶数长度的区间,操作内容:区间内第一个元素和第二个元素互换,然后第二个元素和第三个元素互换,第三个元素和第四个元素互换.......................

用不多于2w个操作,完成任务就算正确输出。


思路:
初学C语言的时候,大家还记得冒泡排序吗?直接交换相邻元素,那就是偶数区间的交换,因为n也比较小,所以我们直接冒泡排序输出冒泡排序的过程就好。


AC代码:


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[150];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<n;j++)
            {
                if(a[j]>a[j+1])
                {
                    swap(a[j],a[j+1]);
                    printf("%d %d\n",j,j+1);
                }
            }
        }
    }
}


C. Robbers' watch

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Input

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.

Output

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.

Examples

input

2 3

output

4

input

8 2

output

5

Note

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).

 

题目大意:设定一天为n小时,每个小时m分钟,对应的每个时间的表达式都是7进制数,表示时间的数不能有重复,求一共一天有多少个时间表达式。并且小时对应的表达式长度为n的长度,不够的位用0来补,分钟也是同理


思路:

1、关键点在于不能有重复的数,而且时间表达为7进制数,那么表示,一个时间的表达式中,最多也就7个数。


2、那么我们就可以暴力Dfs来求小时和分钟两个数。这里注意前导0也要记录长度。


3、注意要求对应小时和分钟求长度的时候要求n-1的长度和m-1的长度,如果您wa在test11,那么无疑就是这个问题了!


AC代码:(写的着急有点挫,多担待哈!)


#include<stdio.h>
#include<string.h>
#include<cmath>
using namespace std;
int n,m;
int lenn,lenm,tmpn,tmpm;
int vis[100];
int output;
int pow(int cont)
{
    int ans=1;
    for(int i=1;i<=cont;i++)
    {
        ans*=7;
    }
    return ans;
}
int judge(int a,int b,int flaga,int flagb)
{
    int dd=0;
    int dd2=0;
    int cont=0;
    while(a)
    {
        int tmp=a%10;
        dd+=tmp*pow(cont);
        cont++;
        a/=10;
    }
    cont=0;
    while(b)
    {
        int tmp=b%10;
        dd2+=tmp*pow(cont);
        cont++;
        b/=10;
    }
    if(dd<n&&dd2<m)return 1;
    else return 0;
}
void Dfs(int a,int b,int cura,int curb,int flaga,int flagb)
{
    int nexa,nexb;
    int ff=0;
    if(cura<lenn)
    {
        for(int i=0;i<=6;i++)
        {
            if(vis[i]==0)
            {
                vis[i]=1;
                if(a==-1)
                {
                    nexa=i;
                    if(i==0)ff=1;
                }
                else nexa=a*10+i;
                if(ff==0)
                Dfs(nexa,b,cura+1,curb,flaga,flagb);
                if(ff==1)
                Dfs(nexa,b,cura+1,curb,ff,flagb);
                vis[i]=0;
            }
        }
    }
    if(cura==lenn)
    {
        if(curb==lenm)
        {
            if(judge(a,b,flaga,flagb)==1&&a!=-1&&b!=-1)
            {
                output++;
            }
            return ;
        }
        for(int i=0;i<=6;i++)
        {
            if(vis[i]==0)
            {
                vis[i]=1;
                if(b==-1)
                {
                    nexb=i;
                    if(i==0)ff=1;
                }
                else nexb=b*10+i;
                if(ff==0)
                Dfs(a,nexb,cura,curb+1,flaga,flagb);
                if(ff==1)
                Dfs(a,nexb,cura,curb+1,flaga,ff);
                vis[i]=0;
            }
        }
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    output=0;
    lenn=0;lenm=0;
    int tmpp=n-1;
    while(tmpp)
    {
        lenn++;
        tmpp/=7;
    }
    tmpp=m-1;
    while(tmpp)
    {
        lenm++;
        tmpp/=7;
    }
    if(lenn==0)lenn=1;
    if(lenm==0)lenm=1;
    memset(vis,0,sizeof(vis));
    Dfs(-1,-1,0,0,0,0);
    printf("%d\n",output);
    return 0;
}




### Codeforces 目解答思路与方法 #### Monsters and Spells 的解答思路 对于目 *Monsters And Spells* ,其核心在于模拟怪物受到伤害的过程并判断最终能否击败所有怪物。此过程涉及到贪心算法的应用,具体来说是在每一轮攻击中尽可能多地减少怪物的生命值。 为了实现这一目标,可以先按照怪物初始生命值降序排列,然后依次处理每一个怪物,在每次施放技能时优先选择能造成最大伤害的方式。通过这种方式能够确保在有限的能量下最大化总伤害输出[^1]。 ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while(t--) { long long n, h, a, b, k; cin >> n >> h >> a >> b >> k; vector<pair<long long,int>> monsters(n); for(int i = 0; i < n; ++i){ cin >> monsters[i].first; // 生命值 monsters[i].second = i; } sort(monsters.rbegin(), monsters.rend()); // 按照生命值从高到低排序 bool canDefeatAll = true; for(auto& m : monsters) { if(m.first > someFunctionToCalculateDamage(h,a,b,k)){ canDefeatAll = false; break; } } cout << (canDefeatAll ? "YES\n" : "NO\n"); } } ``` 上述代码片段展示了如何读取输入数据并对怪物按生命值进行排序,之后遍历这些已排序的数据来决定是否有可能战胜所有的敌人。 #### Sequence 数字序列生成逻辑分析 针对 *Sequence* 这一问,则采取了一种完全不同的策略。考虑到直接计算会遇到性能瓶颈以及难以预测的结果模式,转而探索是否存在周期性的特性成为了解决方案的关键所在。经过观察发现随着数值的增长确实出现了重复现象,这意味着一旦找到了这样的循环节就可以快速定位任意位置上的元素而不必逐项构建整个列表[^3]。 ```python def find_nth_number(n): sequence = [] current_num = 1 seen = {} while True: str_form = &#39;&#39;.join(sorted(str(current_num))) if str_form in seen: loop_start_index = seen[str_form] non_loop_part_length = len(sequence[:loop_start_index]) relative_position_within_cycle = (n - non_loop_part_length - 1) % \ (len(sequence) - non_loop_part_length) return int(&#39;&#39;.join(sorted(str(sequence[relative_position_within_cycle])))) seen[str_form] = len(sequence) sequence.append(current_num) next_value_options = set([current_num * 2, int(&#39;&#39;.join(sorted(str(current_num))))]) current_num = min(next_value_options.difference(set(sequence)), default=current_num + 1) print(find_nth_number(15)) # 输出应为1156 ``` 这段 Python 实现首先尝试建立直到检测到第一个重复项为止的部分序列;接着利用模运算找到给定索引 `n` 对应在环内的确切位置,并据此返回相应的整数值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值