PAT 1044 Shopping in Mars(经常碰见,占坑)
题目
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:
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).
Cut before 5 or after 6, and take off the diamonds from the position 4 to 6 (with values 5+4+6=15).
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.
题目大意
类似于求子序列和,csp-ccf碰到过很多次,但是每次都忘记,用的是双重for循坏,于是每次都超时
正确思路
应该是在输入序列的时候就求出0-i的sum和,如何求子序列就只需要sum数组相减就好了
代码
超时不能AC代码
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
int A[n];
int z;
for(int i=0;i<n;i++){
scanf("%d",&A[i]);
}
int result[n][2];
int k=0;
int min=999999;
for(int i=0;i<n;i++){
int sum=0;
for(int j=i;j<n;j++){
sum+=A[j];
if(sum==m){
result[k][0]=i;
result[k++][1]=j;
break;
}
}
}
if(k==0){
for(int i=0;i<n;i++){
int sum=0;
for(int j=i;j<n;j++){
sum+=A[j];
if(sum>m){
if(sum<min){
min=sum;
}
break;
}
}
}
for(int i=0;i<n;i++){
int sum=0;
for(int j=i;j<n;j++){
sum+=A[j];
if(sum==min){
result[k][0]=i;
result[k++][1]=j;
break;
}
}
}
}
for(int i=0;i<k;i++){
printf("%d-%d\n",result[i][0]+1,result[i][1]+1);
}
return 0;
}
能正常AC代码
#include<iostream>
using namespace std;
int main(){
int n, d[111111] = {0}, count[111111] = {0};
int m;
cin >> n >> m;
int sum = 0;
for( int i = 1; i <= n; i++ ){
cin >> d[i];
sum += d[i];
count[i] = sum;
}
int i = 1, j = 1, cnt = 0, mmin = count[n];
while( i <= n && j <= n ){
int ans = count[j] - count[i-1];
if( ans < m ) j++;
else if( ans > m ) {
if ( mmin > ans ) mmin = ans;
i++;
}
else if ( i <= j ) {
cout << i << "-" << j << endl;
cnt++;
j++;
}
}
if( cnt == 0 ){
int i = 1, j = 1;
while( i <= n && j <= n ){
int ans = count[j] - count[i-1];
if( ans < mmin ) j++;
else if( ans > mmin ) i++;
else if ( i <= j ) {
cout << i << "-" << j << endl;
j++;
}
}
}
return 0;
}