题意:

输入两个正整数N和M(N<=10000,M<=1000),然后输入N个正整数(<=500),输出两个数字和恰好等于M的两个数(小的数字尽可能小且输出在前),如果没有输出"No Solution"。

AAAAAccepted code:

 #define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int a[];
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,k;
cin>>n>>k;
for(int i=;i<=n;++i)
cin>>a[i];
sort(a+,a++n);
int ans=;
for(int i=;i<=n;++i){
int l=,r=n;
while(l<=r){
int mid=(l+r)>>;
if(mid!=i){
if(a[i]+a[mid]<k)
l=mid+;
if(a[i]+a[mid]>k)
r=mid-;
if(a[i]+a[mid]==k){
ans=a[mid];
break;
}
}
if(mid==i){
if(a[i]+a[mid-]<k)
l=mid+;
if(a[i]+a[mid-]>k)
r=mid-;
if(a[i]+a[mid-]==k){
ans=a[mid-];
break;
}
}
}
if(ans){
cout<<a[i]<<" "<<ans;
return ;
}
}
cout<<"No Solution";
return ;
}

【PAT甲级】1048 Find Coins (25 分)(二分)的更多相关文章

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

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

  2. PAT Advanced 1048 Find Coins (25 分)

    Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...

  3. PAT甲 1048. Find Coins (25) 2016-09-09 23:15 29人阅读 评论(0) 收藏

    1048. Find Coins (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva loves t ...

  4. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  5. PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)

    1146 Topological Order (25 分)   This is a problem given in the Graduate Entrance Exam in 2018: Which ...

  6. PAT 甲级 1071 Speech Patterns (25 分)(map)

    1071 Speech Patterns (25 分)   People often have a preference among synonyms of the same word. For ex ...

  7. PAT 甲级 1063 Set Similarity (25 分) (新学,set的使用,printf 输出%,要%%)

    1063 Set Similarity (25 分)   Given two sets of integers, the similarity of the sets is defined to be ...

  8. PAT 甲级 1059 Prime Factors (25 分) ((新学)快速质因数分解,注意1=1)

    1059 Prime Factors (25 分)   Given any positive integer N, you are supposed to find all of its prime ...

  9. PAT 甲级 1051 Pop Sequence (25 分)(模拟栈,较简单)

    1051 Pop Sequence (25 分)   Given a stack which can keep M numbers at most. Push N numbers in the ord ...

  10. PAT 甲级 1037 Magic Coupon (25 分) (较简单,贪心)

    1037 Magic Coupon (25 分)   The magic shop in Mars is offering some magic coupons. Each coupon has an ...

随机推荐

  1. 下载图片(vue 下载图片)

    downloadImg(){ const url = this.imgUrl // window.open(_this.detail.imgUrl) let xmlhttp=new XMLHttpRe ...

  2. 2019 ICPC南京网络赛 F题 Greedy Sequence(贪心+递推)

    计蒜客题目链接:https://nanti.jisuanke.com/t/41303 题目:给你一个序列a,你可以从其中选取元素,构建n个串,每个串的长度为n,构造的si串要满足以下条件, 1. si ...

  3. 2019 ICPC南京站网络赛 H题 Holy Grail(BF算法最短路)

    计蒜客题目链接:https://nanti.jisuanke.com/t/41305 给定的起点是S,终点是T,反向跑一下就可以了,注意判负环以及每次查询需要添加边 AC代码: #include< ...

  4. java1.8特性

    java1.8特性 1.lambda表达式 Java8为集合类引入了另一个重要概念:流(stream).一个流通常以一个集合类实例为其数据源,然后在其上定义各种操作 例如 .filter .forEa ...

  5. IntelliJ IDEA 2017.3尚硅谷-----设置项目文件编码

    这也可以 R暂时显示 C转换

  6. Git的基本使用 -- Git配置

    文件种类 仓库级别(当前仓库有效) local 用户级别(当前用户有效) global 系统级别(系统全局有效) system 系统级别的配置 git config --system user.nam ...

  7. ToolStripComboBox的DataSource和DataTable使用技巧

      可以使用Items属性private void GetData() //一下数据均为测试{toolStripComboBox1.Items.Clear();            DataTabl ...

  8. mysql修改密码的4种方式

    转:https://www.cnblogs.com/jdxn/p/6847089.html 方法1: 用SET PASSWORD命令 首先登录MySQL. 格式:mysql> set passw ...

  9. frm、myd、myi、opt、par文件

    .frm 表结构文件 .myd 表数据文件 .myi 表索引文件 .opr 储存数据库的默认字符集 .par 储存分区信息 mysql 5.6版本分区表有一个文件:表名.par, 该文件在5.7.6版 ...

  10. [杭电_HDU] 2013

    #include <iostream> using namespace std; int main() { int n; while (cin >> n) { ; //最终桃子 ...