【PAT甲级】1048 Find Coins (25 分)(二分)
题意:
输入两个正整数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 分)(二分)的更多相关文章
- PAT 甲级 1048 Find Coins (25 分)(较简单,开个数组记录一下即可)
1048 Find Coins (25 分) Eva loves to collect coins from all over the universe, including some other ...
- PAT Advanced 1048 Find Coins (25 分)
Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...
- 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 ...
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)
1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which ...
- PAT 甲级 1071 Speech Patterns (25 分)(map)
1071 Speech Patterns (25 分) People often have a preference among synonyms of the same word. For ex ...
- 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 ...
- 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 ...
- 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 ...
- PAT 甲级 1037 Magic Coupon (25 分) (较简单,贪心)
1037 Magic Coupon (25 分) The magic shop in Mars is offering some magic coupons. Each coupon has an ...
随机推荐
- DFT 问答 II
1. Boundary Scan A:Boundary scan 顾名思义,是附加在芯片I/O 周边的扫描测试链,它通过专门的测试端口(TAP)访问.在测试模式下,边界扫描链会接管功能逻辑,对I/O进 ...
- php preg正则表达式的组成部分
定界符号 : 多种都可以,常用为// 原子 : 最小的一个匹配单位 (放在定界符中),在一个正则表达式中,至少要有一个原子 1,打印字符(a-z A-Z 0-9 ~!@#$%^&*()_+.. ...
- 20200227英语上课笔记 about advantage and disadvantage
Hello and welcome to class! Remember to keep your microphone off when you are not speaking Pronuncia ...
- 【10】DP青蛙跳台阶
一只青蛙一次可以跳上1级台阶,也可以跳上2级台阶.求该青蛙跳上一个 n 级的台阶总共有多少种跳法. 答案需要取模 1e9+7(1000000007),如计算初始结果为:1000000008,请返回 1 ...
- nginx.conf nginx反向代理配置文件
nginx反向代理配置文件 nginx.conf proxy_default.conf proxy.conf vhost/*.conf upstream/*.conf cache/*.conf ngi ...
- java判断两个时间相差得天数
方法一:通过Calendar类得日期比较,在这需要考虑闰年和平年,也要考虑跨年份 /** * date2比date1多的天数 * @param date1 * @param date2 * @retu ...
- 在elementui表单中实现对vue-quill-editor富文本编辑器内容的绑定
1.v-model(表单标签双向绑定指令) v-model相当于:value=""和@input=""的结合 代码1: <input type=" ...
- JS中数组实现(倒序遍历数组,数组连接字符串)
// =================== 求最大值===================================== <script> var arr = [10,35,765 ...
- mybatis中条件查询大于等于和小于等于写法
原符号 < <= > >= & ' "替换符号 < <= > >= & ' " createDat ...
- 在ubuntu永久添加alias
1. cd 进入家目录 操作如下: cd ~ 2.显示隐藏文件 操作如下:ls -al 3.vim打开.bashrc 操作如下: vim .bashrc 4.按a编辑添加alias example=' ...