PAT:1048. Find Coins (25)(二分查找) AC

#include<stdio.h>
#include<algorithm>
using namespace std;

int arr[100066];

int FIND(int l,int r,int aim)    //二分查找,从l到r,查找aim
{
  int mid;
  while(l<=r)
  {
    mid=(l+r)/2;
    if(arr[mid]==aim)
      return mid;        //找到:返回坐标
    else if(arr[mid]<aim)
      l=mid+1;
    else
      r=mid-1;
  }
  return -1;            //没有找到
}

int main()
{
  int n,m;
  scanf("%d%d",&n,&m);
  for(int i=0 ; i<n ; ++i)
    scanf("%d",&arr[i]);
  sort(arr,arr+n);          //排序之后才能用二分查找
  for(int i=0 ; i<n ; ++i)
  {
    int x=FIND(i+1,n-1,m-arr[i]);  //找下标为i数字的对立数,l从i+1开始可以保证找到的数下标不是原数。而且数组升序,前面的没找到,后面的也用不到前面的了
    if(x!=-1)        //-1代表对立数字存在   //另一种写法:FIND(0,n-1,m-arr[i])   if(x!=-1 && x!=i)   还要求了存在的数字不是原来的arr[i]
    {
      printf("%d %d",arr[i],m-arr[i]);
      return 0;
    }
  }
  printf("No Solution");
  return 0;
}
时间: 2024-08-03 08:06:15

PAT:1048. Find Coins (25)(二分查找) AC的相关文章

PAT:1048. Find Coins (25)(哈希表法) AC

#include<stdio.h> #include<string.h> int HARSH[1066]; int main() { memset(HARSH,0,sizeof(HARSH)); int n,m; scanf("%d%d",&n,&m); for(int i=0 ; i<n ; ++i) { int tmp; scanf("%d",&tmp); ++HARSH[tmp]; } for(int i=

PAT:1048. Find Coins (25)(双指针法) AC

#include<stdio.h> #include<algorithm> using namespace std; int arr[100066]; int main() { int n,m; scanf("%d%d",&n,&m); for(int i=0 ; i<n ; ++i) scanf("%d",&arr[i]); sort(arr,arr+n); int l=0,r=n-1; //左右指针 whil

【PAT甲级】1048 Find Coins (25 分)(二分)

题意: 输入两个正整数N和M(N<=10000,M<=1000),然后输入N个正整数(<=500),输出两个数字和恰好等于M的两个数(小的数字尽可能小且输出在前),如果没有输出"No Solution". 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int a[100007];int main(){ ios::sync_with_stdio(false

PAT 1048. Find Coins

two sum题目,算是贪婪吧 #include <cstdio> #include <cstdlib> #include <vector> #include <algorithm> using namespace std; int main() { int N, M; scanf("%d%d", &N, &M); vector<int> coins(N); for (int i=0; i<N; i++)

1048. Find Coins (25)

题目例如以下: 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 pa

pat1048. Find Coins (25)

1048. Find Coins (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 a

1085. Perfect Sequence (25)【二分查找】——PAT (Advanced Level) Practise

题目信息 1085. Perfect Sequence (25) 时间限制300 ms 内存限制65536 kB 代码长度限制16000 B Given a sequence of positive integers and another positive integer p. The sequence is said to be a "perfect sequence" if M <= m * p where M and m are the maximum and minim

PAT甲级1010踩坑记录(二分查找)——10测试点未过待更新

题目分析: 首先这题有很多的坑点,我在写完之后依旧还有第10个测试点没有通过,而且代码写的不优美比较冗长勿喷,本篇博客用于记录写这道题的一些注意点 1.关于两个不同进制的数比大小一般采用将两个数都转化为10进制之后比较大小(下面统称已知进制数为N1,未知进制数为N2) 2.虽然两个数都只有10位,且每一位上的数字是从‘0’~‘z’,分别代表0~35,但是这并不意味值这题的进制范围就是2~36,radix完全有可能很大很大到long long,写‘0’~‘z’只是为了让结果计算出来相对小一些,并且

Codeforces 474B Worms (二分查找)

题目链接:Codeforces 474B Worms 题意:给出一串数字比如2 7 3 4 9. 表示第一堆编号是[1,2].第二堆编号是[3,9].第三堆编号是[10,12].第四堆编号是[13,16].第五堆编号是[17,25]. 预处理出每堆的上界二分查找答案. AC代码: </pre><pre name="code" class="cpp">#include<stdio.h> #include<map> #in