PTA(Advanced Level)1048.Find Coins
Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the payment: for each bill, she could only use exactly two coins to pay the exact amount. Since she has as many as 105 coins with her, she definitely needs your help. You are supposed to tell her, for any given amount of money, whether or not she can find two coins to pay for it.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 positive numbers: N (≤105, the total number of coins) and M (≤103, the amount of money Eva has to pay). The second line contains N face values of the coins, which are all positive numbers no more than 500. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the two face values V1 and V2 (separated by a space) such that V1+V2=M and V1≤V2. If such a solution is not unique, output the one with the smallest V1. If there is no solution, output No Solution instead.
Sample Input 1:
8 15
1 2 8 7 2 4 11 15
Sample Output 1:
4 11
Sample Input 2:
7 14
1 8 7 2 4 11 15
Sample Output 2:
No Solution
思路
- 说白了这一题就是寻找两数使得和为定值,如果是两重循环遍历找面对这么大的数据是肯定要超时的
- 比较好的一个想法就是用键值对,也就是
map容器,假设目前遍历到的值为t,我们要查询的是m-t是否存在数组里,用map查询代替一个for循环会快很多,这题在Leetcode上也是有类似的
代码
#include<bits/stdc++.h>
using namespace std;
int a[100010];
int main()
{
int n, m;
cin >> n >> m;
map<int, int> mp;
for(int i=0;i<n;i++)
{
cin >> a[i];
if(mp.count(a[i]) != 0) //存在相同的数字个数要对应相加
mp[a[i]]++;
else
mp[a[i]] = 1;
}
sort(a, a+n); //升序保证v1<=v2
for(int i=0;i<n;i++)
{
int t = m - a[i];
if( (t != a[i] && mp.count(t) != 0) ||
(t == a[i] && mp[t] > 1) ) //两个数不相等的时候有就行了,否则该数要不止一个
{
cout << a[i] << " " << t;
return 0;
}
}
cout << "No Solution";
return 0;
}
引用
https://pintia.cn/problem-sets/994805342720868352/problems/994805432256675840
PTA(Advanced Level)1048.Find Coins的更多相关文章
- PAT (Advanced Level) 1048. Find Coins (25)
先对序列排序,然后枚举较小值,二分较大值. #include<iostream> #include<cstring> #include<cmath> #includ ...
- PTA(Advanced Level)1036.Boys vs Girls
This time you are asked to tell the difference between the lowest grade of all the male students and ...
- PTA (Advanced Level) 1004 Counting Leaves
Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...
- PTA (Advanced Level) 1020 Tree Traversals
Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...
- PTA(Advanced Level)1025.PAT Ranking
To evaluate the performance of our first year CS majored students, we consider their grades of three ...
- PTA (Advanced Level) 1009 Product of Polynomials
1009 Product of Polynomials This time, you are supposed to find A×B where A and B are two polynomial ...
- PTA (Advanced Level) 1008 Elevator
Elevator The highest building in our city has only one elevator. A request list is made up with Npos ...
- PTA (Advanced Level) 1007 Maximum Subsequence Sum
Maximum Subsequence Sum Given a sequence of K integers { N1, N2, ..., NK }. A continuous su ...
- PTA (Advanced Level) 1006 Sign In and Sign Out
Sign In and Sign Out At the beginning of every day, the first person who signs in the computer room ...
随机推荐
- [CQOI2016]手机号码 数位DP
[CQOI2016]手机号码 用来数位DP入门,数位DP把当前是否需要限制取数范围(是否正在贴着临界值跑,即下面的limited)和一切需要满足的条件全部塞进记忆化搜索参数里面就好了,具体情况转移便好 ...
- 【luogu4145】上帝造题的七分钟2 / 花神游历各国--区间开根-线段树
题目背景 XLk觉得<上帝造题的七分钟>不太过瘾,于是有了第二部. 题目描述 "第一分钟,X说,要有数列,于是便给定了一个正整数数列. 第二分钟,L说,要能修改,于是便有了对一段 ...
- idea svn设置忽略提交文件
1.找到版本控制位置 2.新建变动列表(装载忽略的文件内容) 3. 将默认的变动列表中需要忽略的文件拖入ignored列表下 4. 提交时,选择default即可. 设置完毕之后,可以在提交文件时将之 ...
- 在AspNetCore3.0中使用Autofac
1. 引入Nuget包 Autofac Autofac.Extensions.DependencyInjection 2. 修改Program.cs 将默认ServiceProviderFactory ...
- springboot+druid+sqlite遇到的问题
1.springboot中使用druid查询sqlite报错getFetchDirection error ResultSet closed https://blog.csdn.net/u011943 ...
- ICEM-五通孔管
原视频下载地址:https://yunpan.cn/cqaQ2t5DrRcKa 访问密码 d111
- ubuntu dnsmasq问题
在很多ubuntu开启wifi热点的教程中,配置比较繁琐的是hostapd+dnsmasq,很多教程都给出了dnsmasq的安装过程,其实在ubuntu桌面版系统下,已经集成到NetworkManag ...
- sh: 1: Syntax error: Bad fd number
Start on Ubuntu 6.10,Using dash default(theDebian Almquist Shell) instead bash(the GNUBourne-Again S ...
- 2.4 Go语言基础之切片
本文主要介绍Go语言中切片(slice)及它的基本使用. 一.引子 因为数组的长度是固定的并且数组长度属于类型的一部分,所以数组有很多的局限性. 例如: func arraySum(x [3]int) ...
- c++ string构造函数学习
#include <iostream>#include <string> using namespace std; int main(){ string a1; cout &l ...