Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the payment, the chain can be cut at any position for only once and some of the diamonds are taken off the chain one by one. Once a diamond is off the chain, it cannot be taken back. For example, if we have a chain of 8 diamonds with values M$3, 2, 1, 5, 4, 6, 8, 7, and we must pay M$15. We may have 3 options:
1. Cut the chain between 4 and 6, and take off the diamonds from the position 1 to 5 (with values 3+2+1+5+4=15).
2. Cut before 5 or after 6, and take off the diamonds from the position 4 to 6 (with values 5+4+6=15).
3. Cut before 8, and take off the diamonds from the position 7 to 8 (with values 8+7=15).
Now given the chain of diamond values and the amount that a customer has to pay, you are supposed to list all the paying options for the customer.
If it is impossible to pay the exact amount, you must suggest solutions with minimum lost.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 numbers: N (<=105), the total number of diamonds on the chain, and M (<=108), the amount that the customer has to pay. Then the next line contains N positive numbers D1 ... DN (Di<=103 for all i=1, ..., N) which are the values of the diamonds. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print "i-j" in a line for each pair of i <= j such that Di + ... + Dj = M. Note that if there are more than one solution, all the solutions must be printed in increasing order of i.
If there is no solution, output "i-j" for pairs of i <= j such that Di + ... + Dj > M with (Di + ... + Dj - M) minimized. Again all the solutions must be printed in increasing order of i.
It is guaranteed that the total value of diamonds is sufficient to pay the given amount.
Sample Input 1:16 15 3 2 1 5 4 6 8 7 16 10 15 11 9 12 14 13Sample Output 1:
1-5 4-6 7-8 11-11Sample Input 2:
5 13 2 4 5 7 9Sample Output 2:
2-4 4-5
火星人购物使用钻石链,每颗钻石都有一定的价值。付款是,要求从钻石链上找到所有刚好能匹配到商品价值的连续的钻石子链,如果没有, 则找到超过商品价值的最小的钻石子链。
比如钻石链为3 2 1 5 4 6 8 7,商品价值为15,则候选的方案有:第 4 到第 6 颗的子链5 4 6,第 7 到第 8 颗:8 7。
输入为一个钻石链以及商品的价值,要求找到所有满足条件的分割方案,按起始点从小到大排序输出。
分析:
题目对时间复杂度有很高要求。
传统思路是求出任意两个点之间的和,然后找最接近目标的;
可优化为 求从i往后相加,直到得到不小于目标值的最小和;
可再优化为 先求出任意点到零点的连续和,以此来求任两点间的和,再找最接近目标的;
可再优化为 采用二分法查找最接近目标值的和。
代码:
1.下面代码可行,但有两个Case超时。问题在于重复进行求和运算:比如算过[1,...,10]的和,仍要计算[2,....,10]的和。
#include <stdio.h>
const int MAX = 100000;
int arr[MAX],mim[MAX],des[MAX];
int main(){
int n,m;
scanf("%d %d",&n,&m);
//cin>>n>>m;
int i,j;
for(i=0;i<n;i++)
//cin>>arr[i];
scanf("%d",&arr[i]);
int t,mm;
mm = 0x7fffffff;
for(i=0;i<n;i++)
{
j = i+1;
t = arr[i];
while(t<m && j<n){
t = t + arr[j];
j++;
}
if(t >= m){
mim[i] = t;
des[i] = j-1;
if(mm > t)mm = t;
}
}
for(i=0;i<n;i++){
if(mim[i] == mm){ //mm为不小于目标m值的最小值
printf("%d-%d\n",i+1,des[i]+1);
//cout<<i+1<<'-'<<des[i]+1<<endl;
}
}
system("pause");
return 0;
}
①[x,....,y]的和可由arr[y]-arr[x-1]求出,其中arr[i]表示[1,...,i]的和。
②因为求和后的数组呈递增数列,采用二分法,进行查找最接近目标m的点。
#include <stdio.h> #include <vector> using namespace std; const int MAX = 100001; int arr[MAX],des[MAX]; int findBestSum(int i,int n,int m){ int left = i+1; int right = n; int mid; while(left < right){ //采用二分法查找 mid = (left + right)/2; if(arr[mid]-arr[i] >= m){ //arr[x]-arr[i] 表示[i+1,...,x]的数据和 right = mid; }else{ left = mid+1; } } des[i] = right; //记录下右边界 return arr[right]-arr[i]; } int main(){ int n,m; scanf("%d %d",&n,&m); int i,j; arr[0]=0; for(i=1;i<=n;i++){ scanf("%d",&arr[i]); arr[i]=arr[i]+arr[i-1]; } vector<int> vec; int res,mm; mm = 0x7fffffff; for(i=0;i<n;i++){ res = findBestSum(i,n,m); //寻找从i+1开始,最接近m的和 if(res >= m){ if(res == mm){ //和当前最小值相等,则加入vector vec.push_back(i); }else if(res < mm){ //比当前最小值还小,则清空vector,重新加入新值 mm = res; vec.clear(); vec.push_back(i); } }else{ //后面肯定更小,无必要再算 break; } } for(i=0;i<vec.size();i++){ printf("%d-%d\n",vec[i]+1,des[vec[i]]); } system("pause"); return 0; }