1649C - Weird Sum - CodeForces Solution


brute force implementation math matrices *1400

Please click on ads to support us..

Python Code:

d={};i=0
for s in[*open(0)][1:]:
 i+=1;j=0
 for x in s.split():j+=1;a,b=d.setdefault(x,([],[]));a+=i,;b+=j,
print(sum(j*(2*i-len(l)+1)for x in d for l in d[x]for i,j in enumerate(sorted(l))))

C++ Code:

#include <bits/stdc++.h>
#define int long long
using namespace std;

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int n,m,x;
    cin >> n >> m;

    map<int, vector< int > >  a;
    map<int, vector< int > >  b;

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cin >> x;
            a[x].push_back(i);
            b[x].push_back(j);
        }
    }

    map<int, vector< int > > :: iterator i;

    for (i = a.begin(); i != a.end(); i++)
    {
        sort(i->second.begin(),i->second.end());
    }
        for (i = b.begin(); i != b.end(); i++)
    {
        sort(i->second.begin(),i->second.end());
    }

    int l,s1=0,s2=0;

    for (i = a.begin(); i != a.end(); i++)
    {
        l=i->first;
        for (int j = 0; j< a[l].size()-1 ; j++)
        {
            s1+= (a[l][j+1] - a[l][j])*(j+1)*(a[l].size()-1-j);
        }
    }
    for (i = b.begin(); i != b.end(); i++)
    {
        l=i->first;
        for (int j = 0; j< b[l].size()-1 ; j++)
        {
            s2+= (b[l][j+1] - b[l][j])*(j+1)*(b[l].size()-1-j);
        }
    }

    cout<<s1+s2<<endl;
}

 				   		 					  	    		     	


Comments

Submit
0 Comments
More Questions

25A - IQ test
785A - Anton and Polyhedrons
1542B - Plus and Multiply
306A - Candies
1651C - Fault-tolerant Network
870A - Search for Pretty Integers
1174A - Ehab Fails to Be Thanos
1169A - Circle Metro
780C - Andryusha and Colored Balloons
1153A - Serval and Bus
1487C - Minimum Ties
1136A - Nastya Is Reading a Book
1353B - Two Arrays And Swaps
1490E - Accidental Victory
1335A - Candies and Two Sisters
96B - Lucky Numbers (easy)
1151B - Dima and a Bad XOR
1435B - A New Technique
1633A - Div 7
268A - Games
1062B - Math
1294C - Product of Three Numbers
749A - Bachgold Problem
1486B - Eastern Exhibition
1363A - Odd Selection
131B - Opposites Attract
490C - Hacking Cypher
158B - Taxi
41C - Email address
1373D - Maximum Sum on Even Positions