好题分享0

P2141 [NOIP2014 普及组] 珠心算测验

原题链接 : 

[NOIP2014 普及组] 珠心算测验 - 洛谷

思路 : 

用哈希表来存出现过的两数之和,最后ans++即可

代码 : 

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'

using namespace std;
typedef long long LL;
int gcd(int a,int b){ return b==0 ? a : gcd(b,a%b); }
int lcm(int a,int b){ if(a==0||b==0) return 0; return (a*b)/gcd(a,b); }
bool is_prime(int x){if(x<2) return false;
for(int i=2;i<=x/i;i++) if(x%i==0) return false; return true;}
//numbers.erase(unique(numbers.begin(), numbers.end()), numbers.end()); // 去重操作
const int N = 2e5+10;
int n;
vector<int> a;
map<int,int> mp;
inline void solve(){
	cin>>n;
	for(int i=0;i<n;i++){
		int x; cin>>x;
		a.push_back(x);
	}
	a.erase(unique(a.begin(), a.end()), a.end());
	for(int i=0;i<n;i++)
		for(int j=i+1;j<n;j++){
			mp[a[i]+a[j]]++;
			if(mp[a[i]+a[j]]==2)
				mp[a[i]+a[j]]--;
		}
	LL ans = 0;
	for(int i=0;i<n;i++){
		ans += mp[a[i]];
	}
	cout<<ans<<endl;
}
 
int main()
{
    IOS
    int _;
    // cin >> _;
    _ = 1; 
    while(_ --) solve();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ros275229/article/details/132594821