Code Force 789 A/B

本文介绍两个有趣的算法问题:一是帮助Anastasia制定最优策略收集公园里的不同类型的石子;二是协助Masha计算在特定条件下,合法的几何级数项的数量。通过分析问题特点并提供解决方案。

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

A.Anastasia and pebbles

Anastasia loves going for a walk in Central Uzhlyandian Park. But she became uninterested in simple walking, so she began to collect Uzhlyandian pebbles. At first, she decided to collect all the pebbles she could find in the park.

She has only two pockets. She can put at most k pebbles in each pocket at the same time. There are n different pebble types in the park, and there are wi pebbles of the i-th type. Anastasia is very responsible, so she never mixes pebbles of different types in same pocket. However, she can put different kinds of pebbles in different pockets at the same time. Unfortunately, she can’t spend all her time collecting pebbles, so she can collect pebbles from the park only once a day.

Help her to find the minimum number of days needed to collect all the pebbles of Uzhlyandian Central Park, taking into consideration that Anastasia can’t place pebbles of different types in same pocket.

Input
The first line contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ 109) — the number of different pebble types and number of pebbles Anastasia can place in one pocket.

The second line contains n integers w1, w2, …, wn (1 ≤ wi ≤ 104) — number of pebbles of each type.

Output
The only line of output contains one integer — the minimum number of days Anastasia needs to collect all the pebbles.

Examples
input
3 2
2 3 4
output
3
input
5 4
3 1 8 9 7
output
5

题目大意:

Anastasia有两个口袋,一个口袋可以装k个石子,一个口袋里只能装一种石子。
一共n种石子,第二行是1-n种石子的具体个数。每天只能装两口袋,问最少需要几天才能把石子全部装完。

大致思路:

就贪心,每天运尽量多的石子。

代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    int n,day=0;
    long k,number;
    cin>>n>>k;
    for(int i=0;i<n;++i){
        cin>>number;
        day+=number/k;
        if(number%k)
            day++;//看这一种石子需要多少天才能运完(一个口袋的情况)
    }

    cout<<(day+1)/2<<endl;//最后将结果除以2向上取整
    return 0;
}

B.Masha and geometric depression

Masha really loves algebra. On the last lesson, her strict teacher Dvastan gave she new exercise.

You are given geometric progression b defined by two integers b1 and q. Remind that a geometric progression is a sequence of integers b1, b2, b3, …, where for each i > 1 the respective term satisfies the condition bi = bi - 1·q, where q is called the common ratio of the progression. Progressions in Uzhlyandia are unusual: both b1 and q can equal 0. Also, Dvastan gave Masha m “bad” integers a1, a2, …, am, and an integer l.

Masha writes all progression terms one by one onto the board (including repetitive) while condition |bi| ≤ l is satisfied (|x| means absolute value of x). There is an exception: if a term equals one of the “bad” integers, Masha skips it (doesn’t write onto the board) and moves forward to the next term.

But the lesson is going to end soon, so Masha has to calculate how many integers will be written on the board. In order not to get into depression, Masha asked you for help: help her calculate how many numbers she will write, or print “inf” in case she needs to write infinitely many integers.

Input
The first line of input contains four integers b1, q, l, m (-109 ≤ b1, q ≤ 109, 1 ≤ l ≤ 109, 1 ≤ m ≤ 105) — the initial term and the common ratio of progression, absolute value of maximal number that can be written on the board and the number of “bad” integers, respectively.

The second line contains m distinct integers a1, a2, …, am (-109 ≤ ai ≤ 109) — numbers that will never be written on the board.

Output
Print the only integer, meaning the number of progression terms that will be written on the board if it is finite, or “inf” (without quotes) otherwise.

Examples
input
3 2 30 4
6 14 25 48
output
3
input
123 1 2143435 4
123 11 -5453 141245
output
0
input
123 1 2143435 4
54343 -13 6 124
output
inf

题目大意:

给b1,q,l,m
b[i]=b[i-1]*q
b[i]<=l并且不在m个数里即为合法,要抄写在黑板上。
当b[i]>l停止向后计算。

大致思路:

其实不算难,就是各种情况判断的相当恶心。
要注意的细节有:
b=0时,如果a[i]里有,答案为0。没有,答案就是inf
q=0,判断b<=l,b和0是否在a[i]里
q=1,判断b<=l,b是否在a[i]里
q=-1,判断b<=l,b和-b是否在a[i]
还是半夜写的,想情况想到大脑宕机·····

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cmath>
using namespace std;
const long maxn=1e5+10;
long a[maxn];
int main()
{
    ios::sync_with_stdio(false);
    bool flag=true;
    long long b,q,l;
    int m,cnt=0;
    cin>>b>>q>>l>>m;
    for(int i=0;i<m;++i)
        cin>>a[i];
    sort(a,a+m);
    if(b==0||q==0||q==1||q==-1){
        if(b==0){
            if(binary_search(a,a+m,b))
                cnt=0;
            else
                flag=false;
        }else if(q==0){
            if(abs(b)<=l){
               if(!binary_search(a,a+m,b))
                    cnt=1;

                if(!binary_search(a,a+m,0))
                    flag=false;
            }

        }else {
            if(abs(b)<=l){
                if(q==1){
                    if(!binary_search(a,a+m,b))
                        flag=false;
                }else{
                    if(binary_search(a,a+m,b)&&binary_search(a,a+m,-b))
                        cnt=0;
                    else
                        flag=false;
                }
            }
        }
    }else{
        while(abs(b)<=l)
        {
            if(!binary_search(a,a+m,b))
                cnt++;
            b=b*q;
        }
    }
    if(flag)
        cout<<cnt<<endl;
    else
        cout<<"inf"<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值