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 ...
随机推荐
- Shell 07 项目案例
案例1:编写一键部署软件脚本 案例2:启动脚本 案例3:编写监控脚本 案例4:编写安全检测脚本 案例5:编写进度显示脚本 一.编写一键部署软件脚本 #!/bin/bash N=$(yum repo ...
- python pywin32 安装
pip install pywin32 参考: https://blog.csdn.net/qq_38161040/article/details/85075158
- CF1197C
CF1197C 题意: 有一个递增的数列,把它分成k段,找到这样的分段方法,即每段最大值减最小值的和最小 解法: 分成k段,即要加k-1个隔断,这k-1个隔断,能隔开差值最大的几个,那最后得到分隔后的 ...
- macOS Mojave 10.14 无法安装brew缺少Command Line Tools for Xcode的解决办法
问题描述: 首先我的版本是 Xcode 10.1 如果按照以前的方法安装brew 复制 1 /usr/bin/ruby -e "$(curl -fsSL https://raw.github ...
- OpenResty之指令与常用API
1. 指令 通过 Lua 编写 Nginx 脚本的基本构建块是指令.指令常用于指定 Lua 代码是几时执行的以及如何使用运行的结果.下图展示了指令执行的顺序. lua_capture_error_lo ...
- kafka 基本原理简介
Kafka是啥?用Kafka官方的话来说就是: Kafka is used for building real-time data pipelines and streaming apps. It i ...
- P1944 最长括号匹配_NOI导刊2009提高(1)
P1944 最长括号匹配_NOI导刊2009提高 题解 宁愿相信世上有鬼,也不能随便相信某谷题目标签 我想了半天然后看了眼题解,发现用栈来模拟就好了 栈来模拟,还要用到一个bool数组,标记是否已经匹 ...
- 原生js实现深复制
function deepClone (obj) { if (obj === null) { // 如果是null则直接返回 return obj; } let copy = Array.isArra ...
- iOS-static和const联合使用;使用static const 与 #define
static和const联合使用: static将一个全局变量变成局部变量 const将一个局部变量变成局部常量 // 定义了一个局部常量 static const CGFloat ...
- PHP服务器完整安装
一.腾讯云购买服务器 Linux 获取root登录账号密码,进入云服务控制台,选择实例,链接Linux 确保是纯净的系统,没装任何环境,否则可能会安装失败 先配置安全组,否则默认禁用所有规则也会导致安 ...