B. Balanced Diet

文章描述了一个编程问题,要求帮助Taylor在有多种类型糖果的商店中找到能最大化S/C值的甜食组合。其中S是甜食总价值,C是出现次数最多的糖果类型的数量。程序需要处理多个测试用例,考虑每种糖果的最大购买限制,并保证结果的S和C的最大公约数为1。给出的代码示例展示了如何处理这个问题,包括读取输入,对数据进行排序和计算最大值。

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

Taylor is wandering in a milk candy store. The store has m types of sweets and there are n sweets in the store. The i-th sweet has the value of ai, and it is of type bi.

Taylor is planning to buy some sweets in the store, each sweet can be bought at most once. He will buy at least one sweet. Taylor knows that a balanced diet is important, the value of a sweet set is measured as S/C, where S denotes the sum of ai and C denotes the maximum number of occurrences among all types of sweets.

Assume Taylor selects pi sweets of type i, it is not welcomed if 1≤pi<li. Note that pi can also be 0 and pi can be everything when li=1.

Please write a program to help Taylor find the sweet set with maximum value.

Input
The first line of the input contains an integer T(1≤T≤1000), denoting the number of test cases.

In each test case, there are two integers n,m(1≤n,m≤100000) in the first line, denoting the number of sweets and types.

In the second line, there are m integers l1,l2,…,lm(1≤li≤n).

For the next n lines, each line contains two integers ai,bi(1≤ai≤108,1≤bi≤m), denoting each sweet.

It is guaranteed that ∑n≤106 and ∑m≤106, and there always exists a valid sweet set.

Output
For each test case, print a single line of format u/v, denoting the maximum value uv. Note that you should guarantee that gcd(u,v)=1.

Example
Input

2
2 1
2
7 1
2 1
3 2
1 2
2 1
5 2
3 2

Output

9/2
5/1
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
typedef long long ll;//注意,都要开longlong 要不然报错
vector<ll>ve[N],vec[N];

int a[N];

bool cmp(int a,int b)
{
    return a>b;
}
int main()
{
    ios::sync_with_stdio(0);
    int t;
    cin>>t;
    while(t--)
    {
        int n,m;
        cin>>n>>m;
        for(int i=1;i<=m;i++) cin>>a[i];
        for(int i=0;i<n;i++)
        {
            int x,y;
            cin>>x>>y;
            ve[y].push_back(x);
        }
        for(int i=1;i<=m;i++)//将ve转化为fvec,非常关键
        {
            sort(ve[i].begin(),ve[i].end(),cmp);
            long long sum=0;
            for(int j=0;j<ve[i].size();j++)//存入vec中,vec的开头是:最少的个数
            {
                if(j<a[i]) sum+=ve[i][j];
                if(j>=a[i]) vec[j+1].push_back(ve[i][j]);
                if(j+1==a[i]) vec[j+1].push_back(sum);
            }
            ve[i].clear();
        }
        long long mc=0,ms=1,ns,nc=0;
        for(int i=1;i<=n;i++)//前后相互比较,非常关键
        {
            ns=i;
            for(int j=0;j<vec[i].size();j++)
                {nc+=vec[i][j];}
            if(mc*ns<nc*ms) mc=nc,ms=ns;
            vec[i].clear();
        }
        long long k=__gcd(mc,ms);
        cout<<mc/k<<'/'<<ms/k<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值