华中农业大学第四届程序设计大赛网络同步赛 C

本文介绍了一道算法题目,要求根据给定的两个整数序列构造第三个整数序列,使特定公式计算出的值最小。文章分析了题目的解决思路,并提供了实现这一算法的C++代码。



链接:戳这里


18: Array C
Time Limit: 1 Sec  Memory Limit: 128 MB
[Submit][Status][Web Board]
Description

   Giving two integers  and  and two arrays  and  both with length , you should construct an array  also with length  which satisfied:
1.0≤Ci≤Ai(1≤i≤n)
2.
and make the value S be minimum. The value S is defined as:

Input

   There are multiple test cases. In each test case, the first line contains two integers n(1≤n≤1000) and m(1≤m≤100000). Then two lines followed, each line contains n integers separated by spaces, indicating the array A and B in order. You can assume that 1≤Ai≤100 and 1≤Bi≤10000 for each i from 1 to n, and there must be at least one solution for array C. The input will end by EOF.
Output
    For each test case, output the minimum value S as the answer in one line.

Sample Input
3 4 
2 3 4
1 1 1
Sample Output
6
HINT

    In the sample, you can construct an array [1,1,2](of course [1,2,1] and [2,1,1] are also correct), and  is 6.


题意:

给定两个整数序列ai,bi 和一个整数m

要求满足条件的整数序列ci使得S最小

0<=ci<=ai

c1+c2+...+cn>=m

(Σci*ci*bi )Min


思路:

首先推出c1+c2+...+cn=m

对于所有的ci<=ai 则枚举每一个ci的大小去推答案  很容易发现

   1     2      3      ai

1*bi  4*bi  9*bi   ai*ai*bi

假设当前的j(0<=j<=ai)值是最优的,那么他等于前一项+( (j-1)*2+1 )*bi  ->1*bi 3*bi 5*bi = =

发现这层累加关系之后,我只需要求出所有的ai下的x*x*bi项  取出前m项即可


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int a[1010],b[1010];
int anw[1000100];
int n,m;
int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        for(int i=1;i<=n;i++) scanf("%d",&b[i]);
        int cnt=0;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=a[i];j++){
                anw[cnt++]=((j-1)*2+1)*b[i];
            }
        }
        sort(anw,anw+cnt);
        ll ans=0;
        for(int i=0;i<m;i++) ans+=(ll)anw[i];
        printf("%lld\n",ans);
    }
    return 0;
}



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值