程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> can you find it?

can you find it?

編輯:C++入門知識

最近感覺二分壓力超級大,所以和妹子一起做了二分的題目,好水的題目啊,可是沒辦法誰叫我們太弱了呢,繼續加油,我要變大牛,不要做菜鳥。

Description

Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate if you can find the three numbers Ai, Bj, Ck, which satisfy the formula Ai+Bj+Ck = X.
Input

There are many cases. Every data case is described as followed: In the first line there are three integers L, N, M, in the second line there are L integers represent the sequence A, in the third line there are N integers represent the sequences B, in the forth line there are M integers represent the sequence C. In the fifth line there is an integer S represents there are S integers X to be calculated. 1<=L, N, M<=500, 1<=S<=1000. all the integers are 32-integers.
Output

For each case, firstly you have to print the case number as the form "Case d:", then for the S queries, you calculate if the formula can be satisfied or not. If satisfied, you print "YES", otherwise print "NO".
Sample Input

3 3 3
1 2 3
1 2 3
1 2 3
3
1
4
10
 
一看數據那麼大,就想到二分了,首先把二個變成一個再二分時間復雜度為nlog(n*n)這樣就不會超時了。二分算法就不說了,地球人都知道。
 
代碼:
[cpp] 
<span style="font-family:FangSong_GB2312;font-size:18px;">#include<iostream> 
#include<cmath> 
#include<algorithm> 
using namespace std; 
int a[505],b[505],c[505]; 
int dp[250025]; 
int main() 

  int i,j,k,m,n,s,t,cas=0,p,flag; 
  while(scanf("%d%d%d",&k,&m,&n)!=EOF) 
  { 
    for(i=0;i<k;i++) 
      scanf("%d",&a[i]); 
    for(i=0;i<m;i++) 
      scanf("%d",&b[i]); 
    for(i=0;i<n;i++) 
      scanf("%d",&c[i]); 
    p=0; 
    for(i=0;i<k;i++) 
      for(j=0;j<m;j++) 
        dp[p++]=a[i]+b[j]; 
     sort(dp,dp+p); 
     printf("Case %d:\n",++cas); 
     scanf("%d",&t); 
    while(t--) 
    { 
     scanf("%d",&s);   flag=0; 
     for(i=0;i<n;i++) 
     { 
       int l=0,r=p-1,mid; 
       while(l<=r) 
       { 
         mid=(l+r)/2; 
         if(dp[mid]+c[i]==s){ flag=1;break;} 
         else if(dp[mid]+c[i]<s) l=mid+1; 
         else if(dp[mid]+c[i]>s) r=mid-1; 
       } 
       if(flag) break; 
    } 
    
    if(flag) printf("YES\n"); 
    else printf("NO\n"); 
  } 

  return 0; 

     
       
     
     
</span> 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved