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 1 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 (≤, the total number of coins) and M (≤, 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 V​1​​ and V​2​​ (separated by a space) such that V​1​​+V​2​​=M and V​1​​≤V​2​​. If such a solution is not unique, output the one with the smallest V​1​​. 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

 #include <iostream>
#include <algorithm>
#include <vector>
#include <cstdlib>
using namespace std;
int N, M;
//放法一,使用窗口函数
int main()
{
cin >> N >> M;
vector<int>nums(N);
vector<pair<int, int>>res;
for (int i = ; i < N; ++i)
cin >> nums[i];
sort(nums.begin(), nums.end(), [](int a, int b) {return a < b; });
int l = , r = N - , m;
while (l < r)//找到中间的数刚好与M/2最接近
{
m = (l + r) / ;
if (nums[m] <= M / && nums[m + ] > M / )
break;
if (nums[m] > M / )
r = m - ;
else
l = m + ;
}
r = m + ;
l = m;
while(l>= && r<N)//然后使用窗口函数,进行左右移动
{
if (nums[l] + nums[r] == M)
{
res.push_back(make_pair(nums[l], nums[r]));
r++;
}
else if (nums[l] + nums[r] < M)
r++;
else
l--;
}
sort(res.begin(), res.end(), [](pair<int, int> a, pair<int, int> b) {return a.first < b.first; });
if (res.size() == )
cout << "No Solution" << endl;
else
cout << res[].first << " " << res[].second << endl;
return ;
} //方法二,使用数找数原理
int main()
{
cin >> N >> M;
int nums[], t;//根据题目要新建的数组大小
memset(nums, , sizeof(int) * );
for (int i = ; i < N; ++i)
{
cin >> t;
nums[t]++;//统计个数
}
for (int i = ; i < ; ++i)
{
if (nums[i] > )//个数大于0
{
nums[i]--;//使用掉一个数字
if (M > i && nums[M - i] > )
{
cout << i << " " << M - i << endl;//由最小值开始遍历,故为最优答案
return ;
}
nums[i]++;//没有使用,还回去
}
}
cout << "No Solution" << endl;
return ;
}

PAT甲级——A1048 Find Coins的更多相关文章

  1. PAT 甲级 1048 Find Coins

    https://pintia.cn/problem-sets/994805342720868352/problems/994805432256675840 Eva loves to collect c ...

  2. PAT 甲级 1048 Find Coins (25 分)(较简单,开个数组记录一下即可)

    1048 Find Coins (25 分)   Eva loves to collect coins from all over the universe, including some other ...

  3. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  4. 【转载】【PAT】PAT甲级题型分类整理

    最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...

  5. PAT甲级题分类汇编——杂项

    本文为PAT甲级分类汇编系列文章. 集合.散列.数学.算法,这几类的题目都比较少,放到一起讲. 题号 标题 分数 大意 类型 1063 Set Similarity 25 集合相似度 集合 1067 ...

  6. PAT甲级题分类汇编——序言

    今天开个坑,分类整理PAT甲级题目(https://pintia.cn/problem-sets/994805342720868352/problems/type/7)中1051~1100部分.语言是 ...

  7. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

  8. PAT甲级1131. Subway Map

    PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...

  9. PAT甲级1127. ZigZagging on a Tree

    PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...

随机推荐

  1. 杂项-IM:IM(即时通讯),一种基于互联网的即时交流消息的业务

    ylbtech-杂项-IM:IM(即时通讯),一种基于互联网的即时交流消息的业务 即时通讯(Instant Messaging)是目前Internet上最为流行的通讯方式,各种各样的即时通讯软件也层出 ...

  2. jedis3.1.0在weblogic(jdk1.6)中无法运行

    文章目录 错误 探索 总结 错误 在tomcat中运行是没有问题的,可是在weblogic中是不能够运行的,weblogic中的jdk客户要求是1.6的. 如果更换版本jedis2.9.0是没有问题的 ...

  3. JavaScript开发人员必知的10个关键习惯

    还在一味没有目的的编写JavaScript代码吗?那么你就OUT了!让我们一起来看看小编为大家搜罗的JavaScript开发人员应该具备的十大关键习惯吧! 随着新技术的不断发展,JavaScript已 ...

  4. Git远程仓库版本回退

    1.首先将本地仓库版本回退到自己想要的版本. git reset commit_id 2.将回退后的版本强制推送到远程仓库. git push -f origin master

  5. <linux常用命令>初级版

    显示时间 date 显示日历cal 变换目录 cd 显示当前所在目录 pwd 建立新目录 mkdir -p a/b/c 删除空目录 rmdir 当前目录下文件和目录显示 ls 复制 cp 文件 路径 ...

  6. DIV常用属性大全自己整理

    一.属性列表 代码如下: color : #999999 文字颜色 font-family : 宋体 文字字型 font-size : 10pt 文字大小 font-style:itelic 文字斜体 ...

  7. 基于VGGnet的人脸识别系统-ubuntu 系统下的Caffe环境搭建(CPU)

    对于caffe的系统一般使用linux系统,当然也有windows版本的caffe,不过如果你一开始使用了windows下面的caffe,后面学习的过程中,会经常遇到各种错误,网上下载的一些源码.模型 ...

  8. Python全栈开发:json与pickle

    #!/usr/bin/env python # -*- coding;utf-8 -*- """ 正解(序列化):将Python数据类型转换成json或者pickle格式 ...

  9. 【案例】鼠标按下,DIV跟随移动

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. art-template官方文档

    http://aui.github.io/art-template/zh-cn/docs/