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的更多相关文章

  1. PAT (Advanced Level) 1048. Find Coins (25)

    先对序列排序,然后枚举较小值,二分较大值. #include<iostream> #include<cstring> #include<cmath> #includ ...

  2. 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 ...

  3. PTA (Advanced Level) 1004 Counting Leaves

    Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...

  4. PTA (Advanced Level) 1020 Tree Traversals

    Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...

  5. PTA(Advanced Level)1025.PAT Ranking

    To evaluate the performance of our first year CS majored students, we consider their grades of three ...

  6. 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 ...

  7. PTA (Advanced Level) 1008 Elevator

    Elevator The highest building in our city has only one elevator. A request list is made up with Npos ...

  8. PTA (Advanced Level) 1007 Maximum Subsequence Sum

    Maximum Subsequence Sum Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous su ...

  9. 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 ...

随机推荐

  1. Oracle 三种连接方式 NESTED LOOP HASH JOIN SORT MERGE JOIN

    NESTED LOOP: 对于被连接的数据子集较小的情况,嵌套循环连接是个较好的选择.在嵌套循环中,内表被外表驱动,外表返回的每一行都要在内表中检索找到与它匹配的行,因此整个查询返回的结果集不能太大( ...

  2. 023_统计当前 Linux 系统中可以登录计算机的账户有多少个

    #!/bin/bash #方法 1: grep "bash$" /etc/passwd | wc -l #方法 2: #-F END都要大写! awk -F: '/bash$/{x ...

  3. Shell基本语法知识

    Shell 就是一个命令解释器,他的作用就是解释执行用户输入的命令及程序等,用户每输入一条命令,Shell 就解释一条.这种从键盘一输入命令,就可以立即得到回应的对话方式,就称为交互的方式. 当命令或 ...

  4. RTMP协议分析及推流过程

    1.RTMP(实时消息传输协议)是Adobe 公司开发的一个基于TCP的应用层协议. 2.RTMP协议中基本的数据单元称为消息(Message). 3.当RTMP协议在互联网中传输数据的时候,消息会被 ...

  5. Ubuntu14.04 挂载u盘

    插入u盘后, $cat /proc/partitions 发现多了 sdb sdb4 sdb是统称,所以新插入的U盘就是/dev/sdb4 $ls /dev |grep sdb sdb sdb4 查看 ...

  6. CodeForces 494B Obsessive String ——(字符串DP+KMP)

    这题的题意就很晦涩.题意是:问有多少种方法,把字符串s划分成不重叠的子串(可以不使用完s的所有字符,但是这些子串必须不重叠),使得t串是所有这些新串的子串.譬如第一个样例,"ababa&qu ...

  7. ubuntu上面Parity 安装

      sudo wget https://raw.githubusercontent.com/paritytech/parity/master/scripts/parity.service -O /et ...

  8. 响应式css样式

    <div class="a"> 123 </div> @media(orientation:portrait) and (max-width:600px){ ...

  9. ViewPager翻页控件简单使用方法

    例子布局文件: 主activity布局文件:activity_view_pager.xml <?xml version="1.0" encoding="utf-8& ...

  10. scikit-learn机器学习(一)简单线性回归

    # -*- coding: utf-8 -*- import numpy as np import matplotlib.pyplot as plt ## 设置字符集,防止中文乱码 import ma ...