P1102A-B 数对
一、题目描述

二、解题思路
给出了c是已知,然我们求A和B是否存在,把等式变换一下,A = C + B,然后用hash判断即可,当然我用的是map(stl大法好),防止重复的没有被加到。
三、代码实现
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 int n,m; 5 int a[200010]; 6 inline ll read() 7 { 8 ll s = 0,w = 1; 9 char ch = getchar(); 10 while(ch < '0' || ch > '9'){ 11 if(ch == '-') w = -1; 12 ch = getchar(); 13 } 14 while(ch >= '0' && ch <= '9'){ 15 s = (s << 1) + (s << 3) + (ch ^ 48); 16 ch = getchar(); 17 } 18 return s * w; 19 } 20 inline void write(ll x) 21 { 22 if(x < 0) putchar('-'),x = -x; 23 if(x > 9) write(x / 10); 24 putchar(x % 10 + '0'); 25 } 26 int main() 27 { 28 ll ans = 0; 29 map <ll,ll> t; 30 cin >> n >> m; 31 for(int i = 1;i <= n;i++){ 32 a[i] =read(); 33 t[a[i]]++; 34 } 35 for(int i = 1;i <= n;i++) 36 ans += t[a[i] - m]; 37 cout << ans; 38 }
本文来自博客园,作者:{scanner},转载请注明原文链接:{https://home.cnblogs.com/u/scannerkk/}

浙公网安备 33010602011771号