131B - Opposites Attract - CodeForces Solution


implementation math *1200

Please click on ads to support us..

Python Code:

n = input()
pari = {}
paruDZ = 0
saraksts = list(map(int,input().split()))
for i in saraksts:
  if i in pari:
    pari[i]+=1
  else:
    pari[i] = 1

for k in pari:
  if k == 0 :
    paruDZ +=  pari[k] * (pari[k] - 1)
  elif k * -1 in pari:
    
    paruDZ += pari[k] * pari[k * -1]

print(paruDZ //2)

C++ Code:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string.h>
#include<bitset>
#include<cmath>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<string>
#include<cstdlib> 
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define test_case int t;cin>>t;while(t--)
typedef long long int ll;
typedef long double ld;
using namespace std;
void Abdo_Saad() { std::ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL); }
ll gcd(ll a, ll b) { return(b == 0 ? a : gcd(b, a % b)); }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
void set_bit(int& n, int idx) { n |= 1 << idx; } 
void reset_bit(int& n, int idx) { n &= ~(1 << idx); }
void flib_bit(int& n, int idx) { n ^= 1 << idx; }
void solve()
{
	int n;
	cin >> n;
	vector<int>v(n),freq(11);
	for (int i = 0;i < n;i++)
	{
		cin >> v[i];
		if(v[i]>=0)freq[v[i]]++;
	}
	ll cnt = 0;
	for (int i = 0;i < n;i++)
	{
		if (v[i] < 0)cnt += freq[v[i] * -1];
	}
	cout << cnt + ll(freq[0]) * (freq[0] - 1) / 2;
	
}
int main()
{
	Abdo_Saad();
	//freopen("strings.in", "r", stdin);
	//freopen(".out","w",stdout);
	//test_case
	solve();
}


Comments

Submit
0 Comments
More Questions

620A - Professor GukiZ's Robot
1342A - Road To Zero
1520A - Do Not Be Distracted
352A - Jeff and Digits
1327A - Sum of Odd Integers
1276A - As Simple as One and Two
812C - Sagheer and Nubian Market
272A - Dima and Friends
1352C - K-th Not Divisible by n
545C - Woodcutters
1528B - Kavi on Pairing Duty
339B - Xenia and Ringroad
189A - Cut Ribbon
1182A - Filling Shapes
82A - Double Cola
45A - Codecraft III
1242A - Tile Painting
1663E - Are You Safe
1663D - Is it rated - 3
1311A - Add Odd or Subtract Even
977F - Consecutive Subsequence
939A - Love Triangle
755A - PolandBall and Hypothesis
760B - Frodo and pillows
1006A - Adjacent Replacements
1195C - Basketball Exercise
1206A - Choose Two Numbers
1438B - Valerii Against Everyone
822A - I'm bored with life
9A - Die Roll