結果

問題 No.723 2つの数の和
ユーザー weizen
提出日時 2018-08-04 21:38:15
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
TLE  
実行時間 -
コード長 1,321 bytes
コンパイル時間 451 ms
コンパイル使用メモリ 60,084 KB
実行使用メモリ 13,752 KB
最終ジャッジ日時 2024-09-19 17:57:18
合計ジャッジ時間 4,409 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 15 TLE * 1 -- * 6
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:11:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   11 |         scanf("%lld",&N);
      |         ~~~~~^~~~~~~~~~~
main.cpp:12:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   12 |         scanf("%lld",&X);
      |         ~~~~~^~~~~~~~~~~
main.cpp:14:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   14 |                  scanf("%lld",a+i+1);
      |                  ~~~~~^~~~~~~~~~~~~~

ソースコード

diff #

#include <stdio.h>
#include <iostream>
#include <algorithm>

typedef long long ll;

ll a[100002];

int main(){
        ll N,X;
        scanf("%lld",&N);
        scanf("%lld",&X);
        for(int i = 0; i < N; i++){
                 scanf("%lld",a+i+1);
        }
        std::sort(a+1,a+N+1);
        a[0] = a[N+1] = X+1;

        int cnt = 0;


        for(int left = 1; left < N+1; left++){
                // X-a[left] <= a_right となる領域のうち左端を探索する
                int bl = 1, br = N+1;
                int b = bl;
                while(bl <= br){
                        b = (bl+br)/2;
                        if( X-a[left] < a[b]){
                                br = b-1;
                        }else if( X-a[left] > a[b]){
                                bl = b+1;
                        }else{ /* X-a[left] == a[b] */
                                while(X-a[left] == a[b-1]) b--;
                                break;
                        }
                }

                if(X-a[left] == a[b]){
                        while(X-a[left] == a[b]){
                                //printf("(%d,%d)\n",a[left],a[b]);
                                b++;
                                cnt++;
                        }
                }
        }

        printf("%d\n",cnt);
}
0