链接:戳这里
18: Array C
Time Limit: 1 Sec Memory Limit: 128 MB
[Submit][Status][Web Board]
Description
1.0≤Ci≤Ai(1≤i≤n)
2.
and make the value S be minimum. The value S is defined as:
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;
}

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

被折叠的 条评论
为什么被折叠?



