CF1941A题解

A.Rudolf and the Ticket

题目描述

Rudolf is going to visit Bernard, and he decided to take the metro to get to him. The ticket can be purchased at a machine that accepts exactly two coins, the sum of which does not exceed kk.

Rudolf has two pockets with coins. In the left pocket, there are nn coins with denominations b1,b2,…,bn. In the right pocket, there are mm coins with denominations c1,c2,…,cm. He wants to choose exactly one coin from the left pocket and exactly one coin from the right pocket (two coins in total).

Help Rudolf determine how many ways there are to select indices ff and ss such that bf+cs≤k.

Input:

The first line contains an integer tt (1≤t≤100) — the number of test cases. Then follows the description of each test case.

The first line of each test case contains three natural numbers nn, mm, and kk (1≤n,m≤100,1≤k≤2000) — the number of coins in the left and right pockets, and the maximum sum of two coins for the ticket payment at the counter, respectively.

The second line of each test case contains nn integers bibi (1≤bi≤1000) — the denominations of coins in the left pocket.

The third line of each test case contains mm integers cici (1≤ci≤1000) — the denominations of coins in the right pocket.

Output:

For each testcase, output a single integer — the number of ways Rudolf can select two coins, taking one from each pocket, so that the sum of the coins does not exceed k.

题意概述

左边口袋有n个硬币,右边口袋有m个硬币,现在让你从左边拿一个硬币,右边拿一个硬币,使得它们的和小于等于k,问有多少种组合?

解题思路

观察到n和m的数据范围都不大,因此我们可以直接采用暴力枚举所有情况,遇见满足条件的情况时计数加1,输出最终计数即可。

AC Code


#include <bits/stdc++.h>



#define endl '\n'

#define int long long



void solve(){

  int n,m,k;

  std::cin >> n >> m >> k;

  std::vector<int> b(n);

  for (int i = 0; i < n;++i){

    std::cin >> b[i];

  }

  std::vector<int> c(m);

  for (int i = 0; i < m;++i){

    std::cin >> c[i];

  }



  int ans = 0;

  for (int i = 0; i < n;++i){

    for (int j = 0; j < m;++j){

      if(b[i]+c[j]<=k)

        ans++;

    }

  }



  std::cout << ans << endl;

}



signed main()

{

  std::ios_base::sync_with_stdio(0);

  std::cin.tie(nullptr);

  std::cout.tie(nullptr);



  int _;

  //_=1;

  std::cin>>_;

  while(_--){

    solve();

  }



  return 0;

}

posted @ 2025-05-19 08:18  渝州炒鸡舞帝神兽大王  阅读(60)  评论(0)    收藏  举报