U454752巧克力问题(@wsgly986280)
  • 板块灌水区
  • 楼主Sharmy
  • 当前回复2
  • 已保存回复2
  • 发布时间2024/8/14 10:50
  • 上次更新2024/8/14 13:05:22
查看原帖
U454752巧克力问题(@wsgly986280)
1401143
Sharmy楼主2024/8/14 10:50

巧克力

题目背景

小圆和小周是好朋友。有一天,他们去超市买巧克力。

题目描述

超市里有n种巧克力,编号分别为0,1,2,3…n-1,它们的价格分别为a0,a1,a2…an-1元。其中,小圆喜欢吃第i1~p1种,小周喜欢吃第i2~p2种。小圆带了x元钱,小周带了y元钱。当然,超市里也可能没有他们喜欢吃的巧克力。请编写一个程序,帮小圆和小周判断他们能不能把自己喜欢的巧克力都买一遍。

输入格式

第一行一个整数n,代表超市里的巧克力种数和输入的价格数。

第二行四个整数i1,p1,i2,p2,代表小圆和小周的喜好。

第三行t个整数ai,代表巧克力的价格。若t<n,将价格循环使用。如输入"12 13 15",则巧克力的价格为12,13,15,12,13,15,12……

第四行两个整数x,y,代表两人带的钱。

输出格式

一行三个字符串,用逗号隔开,分别代表小圆是否能用自己的钱买到所有自己喜欢的巧克力;小周是否能用自己的钱买到所有自己喜欢的巧克力;两人合力是否能买到两人所有喜欢的巧克力。如果是,输出"true";如果否,输出"false"。

样例 #1

样例输入 #1

3
0 1 1 2
6 5 8
11 17

样例输出 #1

true,true,true

样例 #2

样例输入 #2

3
0 2 1 3
6
20 18

样例输出 #2

true,false,false

提示

样例二中小周想要的3号巧克力超市没有,所以输出false。

对于所有测试点:

0<=n<=100000000

0<=min(i1,p1),min(i2,p2)<=100

0<=ai<=10000000

0<=|i1-p1|,|i2-p2|<=10000

0<=x,y<=1000000000

0<=t<=n

对于测试点1~2:

0<=n<=10000000

0<=min(i1,p1),min(i2,p2)<=100

0<=ai<=10000

0<=|i1-p1|,|i2-p2|<=10000

0<=x,y<=100000000

t=n

对于测试点3:

0<=n<=10000000

0<=min(i1,p1),min(i2,p2)<=100

0<=ai<=10000

0<=|i1-p1|,|i2-p2|<=10000

0<=x,y<=10000000

0<t<n

对于测试点4~5:

0<=n<=100000000

0<=min(i1,p1),min(i2,p2)<=100

0<=ai<=10000000

0<=|i1-p1|,|i2-p2|<=10000

0<=x,y<=100000000

0<t<n

我的代码如下:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

bool can_buy(int start, int end, const vector<int>& prices, int money) {
    long long total_cost = 0;
    int size = prices.size();

    for (int idx = start; idx <= end; ++idx) {
        total_cost += prices[idx % size];
        if (total_cost > money) return false; 
    }
    return total_cost <= money;
}

int main() {
    int n;
    cin >> n;

    int i1, p1, i2, p2;
    cin >> i1 >> p1 >> i2 >> p2;

   
    vector<int> prices;
    int price;
    while (cin >> price) {
        prices.push_back(price);
        if (cin.peek() == '\n') break; 
    }

    int x, y;
    cin >> x >> y;


    vector<int> full_prices(n);
    int price_count = prices.size();
    for (int i = 0; i < n; ++i) {
        full_prices[i] = prices[i % price_count];
    }


    if (i1 < 0 || p1 >= n || i2 < 0 || p2 >= n || x < 0 || y < 0) {
        cout << "NO" << endl;
        return 0;
    }


    bool round_can_buy = can_buy(i1, p1, prices, x);

    bool zhou_can_buy = can_buy(i2, p2, prices, y);

  
    bool together_can_buy = can_buy(i1, p1, prices, x + y) && can_buy(i2, p2, prices, x + y);

    cout << (round_can_buy ? "true" : "false") << ","
         << (zhou_can_buy ? "true" : "false") << ","
         << (together_can_buy ? "true" : "false") << endl;

    return 0;
}
2024/8/14 10:50
加载中...