tinycodinglm / cp2 /cp-0031.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
2.97 kB
cp-practice-sim trace
System You are a competitive-programming assistant. Deliver correct, complete, runnable solutions that read standard input and write standard output. Give only a concise rationale: approach, correctness idea, and time/space com...

You are a competitive-programming assistant. Deliver correct, complete, runnable solutions that read standard input and write standard output. Give only a concise rationale: approach, correctness idea, and time/space complexity.

User 2026-11-08T04:20:03.537Z

Distinct Badges

Topic: Hashing and counting (maps/sets) | Difficulty: beginner

Problem

A club handed out n badges, each stamped with an integer id. Several badges may share the same id. Count how many different ids appear.

Input

The first line contains n. The second line contains n integers, the badge ids.

Output

Print one integer: the number of distinct ids.

Constraints

  • 1 <= n <= 2*10^5
  • |id| <= 10^9

Examples

Example 1

Input:

markdown
5
3 1 3 2 1

Output:

markdown
3

Example 2

Input:

markdown
3
-5 -5 -5

Output:

markdown
1

Note: All ids are equal, including negative ones.

Solve this in C++17, Rust, Kotlin, one complete program per language. Include a concise rationale (approach, correctness idea, time/space complexity).

Assistant 2026-11-08T04:20:05.511Z

Approach. Insert every id into a hash set and print its size.

Correctness. A set stores each value once, so its size equals the number of distinct values.

Complexity. Time: O(n) expected. Space: O(n).

C++17

cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
    int n;
    scanf("%d", &n);
    unordered_set<long long> s;
    for (int i = 0; i < n; i++) {
        long long x;
        scanf("%lld", &x);
        s.insert(x);
    }
    printf("%zu\n", s.size());
}

Rust

rust
use std::collections::HashSet;
use std::io::*;
fn main() {
    let mut t = String::new();
    stdin().read_to_string(&mut t).unwrap();
    let mut it = t.split_whitespace();
    let n: usize = it.next().unwrap().parse().unwrap();
    let s: HashSet<i64> = it.take(n).map(|x| x.parse().unwrap()).collect();
    println!("{}", s.len());
}

Kotlin

kotlin
import java.util.StringTokenizer

fun main() {
    val st = StringTokenizer(System.`in`.bufferedReader().readText())
    val n = st.nextToken().toInt()
    val s = HashSet<Long>()
    repeat(n) { s.add(st.nextToken().toLong()) }
    println(s.size)
}