まず、次の情報を取得します。
T \\ no of arrays
n \\ size of array
a[i] \\ elements of array
それから私はのための指標のために出力を与えているa[i] = a[j]
とi!=j
。
例えば:
入力:
2 \\\ no arrays
3 \\ \ size of array 1
1 2 3 \\\ elements of array 1
3 \\\ size of array 2
1 1 2
出力:
0 \\\ as there is no same element
2 \\\ for (0,1) and (1,0) gets has same element "1"
制約:
1 ≤ T ≤ 10
1 ≤ n ≤ 100000
1 ≤ a[i] ≤ 1000000
これは私のコードです:
int main() {
int t,T,n,i,j,count;
scanf("%d",&T);
for(t=0;t<T;t++){
scanf("%d",&n);
int a[n];
count =0;
int c[1000000] = { 0 };
for(i=0;i<n;i++){
scanf("%d",&a[i]);
c[a[i]-1]++ ;
}
for(i=0;i<1000000;i++){
if(c[i]>1)
count+= c[i]*(c[i]-1);
}
printf("%d\n",count);
}
return 0;
}
このコードは、このテストケースを満たしていません。
これが正しいコードです
#include<stdio.h>
#include<stdlib.h>
int main() {
int t,T,n,i,j;
long long int count,k;
scanf("%d",&T);
int *c = calloc(1000000,sizeof(int));
for(t=0;t<T;t++){
scanf("%d",&n);
int temp;
count =0;
for(i=0;i<n;i++){
scanf("%d",&temp);
c[temp-1]++ ;
}
for(i=0;i<1000000;i++){
if(c[i]>1){
k = c[i];
count+= k*(k-1);
}
c[i] = 0;
}
printf("%lld\n",count);
}
return 0;
}
変更点:
calloc
配列をゼロに初期化するために使用されます(理由はわかりませんが、配列の初期化の方法でセグメンテーション違反が発生します)n
である100000
、従って出力はとして高いようであってもよいn^2
、すなわち10000000000
どのうちint
従って使用範囲、long long int
のためcount
の変数。この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加