Serie: Algorithms
cpp
42 líneas
· Actualizado 2026-04-05
greedy_v2.cpp
Algorithms/greedy_v2.cpp
// Greedy V2 Algorithm
#include <iostream>
#include <map>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
// use map to count the freq of b[k] = a[k] - k
map<int, long long> cnt;
for (int k = 1; k <= n; k++) {
int a;
cin >> a;
cnt[a - k]++; // count the freq of b[k] = a[k] - k
}
// for each b[k], count the number of pairs C(c, 2) = c * (c - 1) / 2
long long ans = 0;
map<int, long long>::iterator it;
for (it = cnt.begin(); it != cnt.end(); it++) {
long long c = it->second;
ans += c * (c - 1) / 2;
}
cout << ans << endl;
}
return 0;
}
Artículos relacionados
Algorithms
java
Actualizado 2026-03-02
#include <iostream>.java
#include <iostream>.java — java source code from the Algorithms learning materials (Algorithms/#include <iostream>.java).
Leer artículo →
Algorithms
cpp
Actualizado 2026-04-07
748.cpp
748.cpp — cpp source code from the Algorithms learning materials (Algorithms/748.cpp).
Leer artículo →
Algorithms
cpp
Actualizado 2026-04-07
827.cpp
827.cpp — cpp source code from the Algorithms learning materials (Algorithms/827.cpp).
Leer artículo →
Algorithms
cpp
Actualizado 2026-04-07
827_best_greedy.cpp
827_best_greedy.cpp — cpp source code from the Algorithms learning materials (Algorithms/827_best_greedy.cpp).
Leer artículo →
Algorithms
cpp
Actualizado 2026-04-07
8402.cpp
8402.cpp — cpp source code from the Algorithms learning materials (Algorithms/8402.cpp).
Leer artículo →
Algorithms
cpp
Actualizado 2026-04-07
860.cpp
860.cpp — cpp source code from the Algorithms learning materials (Algorithms/860.cpp).
Leer artículo →