编程介的小学生 2019-09-22 22:59 采纳率: 0.4%
浏览 76

Gourmet Grazers 的程序怎么来编写

Description

Like so many others, the cows have developed very haughty tastes and will no longer graze on just any grass. Instead, Farmer John must purchase gourmet organic grass at the Green Grass Grocers store for each of his N (1 ≤ N ≤ 100,000) cows.

Each cow i demands grass of price at least Ai (1 ≤ Ai ≤ 1,000,000,000) and with a greenness score at least Bi (1 ≤ Bi ≤ 1,000,000,000). The GGG store has M (1 ≤ M ≤ 100,000) different types of grass available, each with a price Ci (1 ≤ Ci ≤ 1,000,000,000) and a greenness score of Di (1 ≤ Di ≤ 1,000,000,000). Of course, no cow would sacrifice her individuality, so no two cows can have the same kind of grass.

Help Farmer John satisfy the cows' expensive gourmet tastes while spending as little money as is necessary.

Input

  • Line 1: Two space-separated integers: N and M.
  • Lines 2..N+1: Line i+1 contains two space-separated integers: Ai and Bi
  • Lines N+2..N+M+1: Line i+N+1 contains two space-separated integers: Ci and Di

Output

  • Line 1: A single integer which is the minimum cost to satisfy all the cows. If that is not possible, output -1.

Sample Input

4 7
1 1
2 3
1 4
4 2
3 2
2 1
4 3
5 2
5 4
2 6
4 4
Sample Output

12

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-30 07:54
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    # 读取数据
    n <- readLine()
    m <- readLine()
    
    # 将输入的数字转换为整数并存储在数组中
    cows <- as.integer(strsplit(n, " ")[[1]])
    grass <- as.integer(strsplit(m, " ")[[1]])
    
    # 初始化变量
    total_cost <- 0
    best_solution <- 0
    
    # 遍历所有可能的组合
    for(i in 1:(length(cows)-1)) {
      for(j in i+1:length(cows)) {
        # 计算总成本
        total_cost <- cows[i] * grass[j]
        
        # 检查当前组合是否更优
        if(total_cost < best_solution) {
          best_solution <- total_cost
        }
      }
    }
    
    # 输出结果
    print(best_solution)
    

    这段代码使用了R语言,并通过读取用户提供的输入(格式为“N M”),计算每个可能的草料分配方案所需的最小费用。最后输出最优方案的总费用。

    评论

报告相同问题?