写在前面:没AC,15分只拿了16分,有两个点wrong answer了
其实PAT题库的大部分题目直接看输入输出就好了(陈越姥姥总喜欢在题目描述部分说点有的没的,看了也没办法帮你解题)
【题目描述】
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.
在火星购物有点不一样。火星人用一串钻石付款。每颗钻石都有它的价值(火星货币火刀M$)。当需要付款时,链子可以从任意位置切断一次,且钻石从链子上一个接一个被取下来。一旦钻石被取下来,它就不能再被放回去。比如我们有串成一串的八个钻石,价值分别为3,2,1,5,4,6火刀,我们必须付15火刀,我们有三个选择:...(懒得翻译了总之就是必须连续取一段钻石来付款,这一段钻石总价必须大于给定数值)
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 (≤10^8), the amount that the customer has to pay. Then the next line contains N positive numbers D1⋯DN (Di≤10^3 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.
【思路】
双指针,每当指针移动,记录两个指针之间钻石总价。
【代码】
上代码!刚接触c++,有哪些可优化的地方还请各位佬指正。
#include<iostream>
#include<string.h>
#include<vector>
#include<math.h>
using namespace std;
typedef struct pair{
int beg;
int end;
}pairs;//定义了一个结构体,保存当前取钻石方案的起点和终点
int main(){
// freopen("1044input.txt","r",stdin);
vector<pairs> ans;
int num,pay;//读入钻石数和要付的钱
int i=0,j;//双指针
int len;
int nowval=0;
int min=1001;
cin>>num>>pay;
int val[num];
memset(val,0,sizeof(val));
while(i<num){//读入每颗钻石的价值
cin>>val[i++];
}
nowval+=val[0];
i=0;
while(i<num){//count all the probabilities
if(i>0) nowval-=val[i-1];//开始指针后移,从总价里减去开始指针前一位的钻石价值
else j=i;
while(nowval<pay){//当前取的钻石不够则再取下一个
j++;
nowval+=val[j];
}
if(nowval-pay<min){//如果发现损失更小的方案,清空之前的所有方案
min=nowval-pay;
ans.empty();
}if(nowval-pay==min){//如果有损失相同的方案,保存新方案
pairs tmp;
tmp.beg=i;
tmp.end=j;
ans.push_back(tmp);
}
i++;
}//counting done
len=ans.size();
i=0;
while(i<len){
cout<<ans[i].beg+1<<'-'<<ans[i].end+1;
i++;
if(i<len) cout<<endl;
}
return 0;
}
Wrong Answer真是最折磨人的错误...超时嘛剪个枝就好了,也不知道是哪里出了问题