[ PAT甲级 / PA ]1004. Shopping in Mars

3 篇文章 0 订阅

写在前面:没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:

  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.

在火星购物有点不一样。火星人用一串钻石付款。每颗钻石都有它的价值(火星货币火刀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真是最折磨人的错误...超时嘛剪个枝就好了,也不知道是哪里出了问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenSSH_9.2p1 Debian-2, OpenSSL 3.0.9 30 May 2023 debug1: Reading configuration data /etc/ssh/ssh_config debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files debug1: /etc/ssh/ssh_config line 21: Applying options for * debug1: Connecting to lxslc702.ihep.ac.cn [2401:de00:2:332::186] port 22. debug1: Connection established. debug1: identity file /home/fyf/.ssh/id_rsa type -1 debug1: identity file /home/fyf/.ssh/id_rsa-cert type -1 debug1: identity file /home/fyf/.ssh/id_ecdsa type -1 debug1: identity file /home/fyf/.ssh/id_ecdsa-cert type -1 debug1: identity file /home/fyf/.ssh/id_ecdsa_sk type -1 debug1: identity file /home/fyf/.ssh/id_ecdsa_sk-cert type -1 debug1: identity file /home/fyf/.ssh/id_ed25519 type -1 debug1: identity file /home/fyf/.ssh/id_ed25519-cert type -1 debug1: identity file /home/fyf/.ssh/id_ed25519_sk type -1 debug1: identity file /home/fyf/.ssh/id_ed25519_sk-cert type -1 debug1: identity file /home/fyf/.ssh/id_xmss type -1 debug1: identity file /home/fyf/.ssh/id_xmss-cert type -1 debug1: identity file /home/fyf/.ssh/id_dsa type -1 debug1: identity file /home/fyf/.ssh/id_dsa-cert type -1 debug1: Local version string SSH-2.0-OpenSSH_9.2p1 Debian-2 debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4 debug1: compat_banner: match: OpenSSH_7.4 pat OpenSSH_7.4* compat 0x04000006 debug1: Authenticating to lxslc702.ihep.ac.cn:22 as 'fanyufan' debug1: load_hostkeys: fopen /home/fyf/.ssh/known_hosts: No such file or directory debug1: load_hostkeys: fopen /home/fyf/.ssh/known_hosts2: No such file or directory debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory debug1: SSH2_MSG_KEXINIT sent debug1: SSH2_MSG_KEXINIT received debug1: kex: algorithm: curve25519-sha256 debug1: kex: host key algorithm: ssh-ed25519 debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none debug1: expecting SSH2_MSG_KEX_ECDH_REPLY Connection closed by 2401:de00:2:332::186 port 22
07-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值