352B - Jeff and Periods - CodeForces Solution


implementation sortings *1300

Please click on ads to support us..

Python Code:

n=int(input()) ; arr=[int(i) for i in input().split()] ; dic=dict() ; c=0
for i in range(n):
    x=arr[i]
    if x not in dic: dic[x]=[i]
    else:
        dic[x].append(i)


arr2=[]
x=list(dic.keys())
x.sort()
for i in x:
    z=dic[i]
    if len(z)==1: arr2.append([i,0])
    else:
        d=z[1]-z[0]
        for j in range(2,len(z)):
            if z[j]-z[j-1]!=d:
                break
        else:arr2.append([i,d])
            

print(len(arr2))
for i in arr2: 
    print(*i)           
            

C++ Code:

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

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;

ll m = 1000000007;

void solve() {
    map<int,vector<int>> indexes;
    int n;
    cin >> n;
    vector<int> a(n);
    for(int i = 0; i < n; i++) {
        cin >> a[i];
        indexes[a[i]].push_back(i+1);
    }
    int cnt = 0;
    vector<vector<int>> ans;
    for(auto i = indexes.begin(); i !=indexes.end(); i++) {
        vector<int> temp = i->second;
        if(temp.size() == 1) {
            cnt++;
            ans.push_back({i->first, 0});
            continue;
        }
        int diff = temp[1] - temp[0];
        int flag = 0;
        for(int j = 1; j < temp.size(); j++) {
            if(temp[j] - temp[j-1] != diff) {
                flag = 1;
                break;
            }
        }
        if(!flag) {
            cnt++;
            ans.push_back({i->first, diff});
        }
    }
    cout << cnt << endl;
    for(int i = 0; i < ans.size(); i++)
        cout << ans[i][0] << " " << ans[i][1] << endl;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int tc = 1;
    // cin >> tc;
    for (int t = 1; t <= tc; t++)
    {
        // cout << "Case #" << t << ": ";
        solve();
    }
}



Comments

Submit
0 Comments
More Questions

869. Reordered Power of 2
1593C - Save More Mice
1217. Minimum Cost to Move Chips to The Same Position
347. Top K Frequent Elements
1503. Last Moment Before All Ants Fall Out of a Plank
430. Flatten a Multilevel Doubly Linked List
1290. Convert Binary Number in a Linked List to Integer
1525. Number of Good Ways to Split a String
72. Edit Distance
563. Binary Tree Tilt
1306. Jump Game III
236. Lowest Common Ancestor of a Binary Tree
790. Domino and Tromino Tiling
878. Nth Magical Number
2099. Find Subsequence of Length K With the Largest Sum
1608A - Find Array
416. Partition Equal Subset Sum
1446. Consecutive Characters
1618A - Polycarp and Sums of Subsequences
1618B - Missing Bigram
938. Range Sum of BST
147. Insertion Sort List
310. Minimum Height Trees
2110. Number of Smooth Descent Periods of a Stock
2109. Adding Spaces to a String
2108. Find First Palindromic String in the Array
394. Decode String
902. Numbers At Most N Given Digit Set
221. Maximal Square
1200. Minimum Absolute Difference