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 ...
随机推荐
- InputStreamReader 和 FileReader联系与区别
两者关系: FileReader继承自InputStreamReader : 区别: InputStreamReader 是字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字 ...
- 【説明する】KMP
KMP是一个困扰我很久的算法,听老师或者是学姐讲了差不多有4次了,但是还是搞不太懂,今天终于,终于,终于搞懂了! ——2017-10-29 Vanora 首先推荐一下KMP详解——July 读罢之后内 ...
- windows环境rabbitmq安装步骤
windows环境rabbitmq安装步骤: 1 提前安装erl; 2 rabbitmq安装后自动启动; 3 从开始菜单进入rabbit命令窗,启用插件; 下面是命令: 启用插件 rabbitmq ...
- 深入分析JAVA IO(BIO、NIO、AIO)
IO的基本常识 1.同步 用户进程触发IO操作并等待或者轮询的去查看IO操作是否完成 2.异步 用户触发IO操作以后,可以干别的事,IO操作完成以后再通知当前线程继续处理 3.阻塞 当一个线程调用 r ...
- Flutter生成带图片的二维码
现在的APP中经常需要用自己的信息生成一个二维码给别人扫,下面就介绍一下Flutter中怎么生成一个带图片的二维码. 需要用到的插件qr_flutter 首先在 pubspec.yaml 文件中添加以 ...
- maven在整合springmvc+hibernate运行时遇到的一些问题
在这里大概记录一下自己在搭建的时候遇到的一些小问题. 1,在获取hibernate的sessionFactory对象时报空指针异常,我的常规配置如下:
- SQL-W3School-基础:SQL INSERT INTO 语句
ylbtech-SQL-W3School-基础:SQL INSERT INTO 语句 1.返回顶部 1. INSERT INTO 语句 INSERT INTO 语句用于向表格中插入新的行. 语法 IN ...
- x86架构64位模式下的寄存器列表
在此列出x86架构处理器在64位模式下的可用寄存器列表,方便查阅- 这里要注意的是,在64位模式下,所有通用寄存器都能访问第8位部分,低16位部分以及低32位部分. 以下是64位模式下AMD64 AB ...
- js常用的正则
1.5位整数带两位小数/^\d{0,5}(\.\d{0,2})?$/g 2.邮箱/^([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$ ...
- PHP判断访问者是PC端还是移动端
function isMobile() { // 如果有HTTP_X_WAP_PROFILE则一定是移动设备 if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])) { ...