prob_desc_description
stringlengths
63
3.8k
prob_desc_output_spec
stringlengths
17
1.47k
lang_cluster
stringclasses
2 values
src_uid
stringlengths
32
32
code_uid
stringlengths
32
32
lang
stringclasses
7 values
prob_desc_output_to
stringclasses
3 values
prob_desc_memory_limit
stringclasses
19 values
file_name
stringclasses
111 values
tags
listlengths
0
11
prob_desc_created_at
stringlengths
10
10
prob_desc_sample_inputs
stringlengths
2
802
prob_desc_notes
stringlengths
4
3k
exec_outcome
stringclasses
1 value
difficulty
int64
-1
3.5k
prob_desc_input_from
stringclasses
3 values
prob_desc_time_limit
stringclasses
27 values
prob_desc_input_spec
stringlengths
28
2.42k
prob_desc_sample_outputs
stringlengths
2
796
source_code
stringlengths
42
65.5k
hidden_unit_tests
stringclasses
1 value
You are given a sequence of integers of length $$$n$$$ and integer number $$$k$$$. You should print any integer number $$$x$$$ in the range of $$$[1; 10^9]$$$ (i.e. $$$1 \le x \le 10^9$$$) such that exactly $$$k$$$ elements of given sequence are less than or equal to $$$x$$$.Note that the sequence can contain equal elements.If there is no such $$$x$$$, print "-1" (without quotes).
Print any integer number $$$x$$$ from range $$$[1; 10^9]$$$ such that exactly $$$k$$$ elements of given sequence is less or equal to $$$x$$$. If there is no such $$$x$$$, print "-1" (without quotes).
C
55297e2a65144323af4d6abd6a6ef050
34f3f9951bbba90c280194ef4c29c3b8
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "sortings" ]
1567258500
["7 4\n3 7 5 1 10 3 20", "7 2\n3 7 5 1 10 3 20"]
NoteIn the first example $$$5$$$ is also a valid answer because the elements with indices $$$[1, 3, 4, 6]$$$ is less than or equal to $$$5$$$ and obviously less than or equal to $$$6$$$.In the second example you cannot choose any number that only $$$2$$$ elements of the given sequence will be less than or equal to this number because $$$3$$$ elements of the given sequence will be also less than or equal to this number.
PASSED
1,200
standard input
2 seconds
The first line of the input contains integer numbers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$0 \le k \le n$$$). The second line of the input contains $$$n$$$ integer numbers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the sequence itself.
["6", "-1"]
#include<stdio.h> #include<stdlib.h> long int partition(long int a[],long int l,long int u) { long int v,i,j,temp; v=a[l]; i=l; j=u+1; do { do i++; while(a[i]<v&&i<=u); do j--; while(v<a[j]); if(i<j) { temp=a[i]; a[i]=a[j]; a[j]=temp; } }while(i<j); a[l]=a[j]; a[j]=v; return(j); } void quick_sort(long int a[],long int l,long int u) { long int j; if(l<u) { j=partition(a,l,u); quick_sort(a,l,j-1); quick_sort(a,j+1,u); } } main() { long int n,k,i,a[200040],j; scanf("%ld %ld",&n,&k); for(i=0;i<n;++i) { scanf("%ld",&a[i]); } quick_sort(a,0,n-1); if(k==0 && a[0]==1) { printf("-1"); exit(0); } if(k==0 && ((a[0]==2) || (a[0]==3))) { printf("%ld",a[0]-1); exit(0); } if(k==0) { printf("-1"); exit(0); } if(k==1 && n==1) { printf("%ld",a[0]); exit(0); } if(a[k-1]==a[k]) { printf("-1"); exit(0); } long int max=-90; for(j=0;j<k;++j) { if(a[j]>max) { max=a[j]; } } printf("%ld",max); }
You are given a sequence of integers of length $$$n$$$ and integer number $$$k$$$. You should print any integer number $$$x$$$ in the range of $$$[1; 10^9]$$$ (i.e. $$$1 \le x \le 10^9$$$) such that exactly $$$k$$$ elements of given sequence are less than or equal to $$$x$$$.Note that the sequence can contain equal elements.If there is no such $$$x$$$, print "-1" (without quotes).
Print any integer number $$$x$$$ from range $$$[1; 10^9]$$$ such that exactly $$$k$$$ elements of given sequence is less or equal to $$$x$$$. If there is no such $$$x$$$, print "-1" (without quotes).
C
55297e2a65144323af4d6abd6a6ef050
8f7d7988cf593c7f7580c0da1441756e
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "sortings" ]
1567258500
["7 4\n3 7 5 1 10 3 20", "7 2\n3 7 5 1 10 3 20"]
NoteIn the first example $$$5$$$ is also a valid answer because the elements with indices $$$[1, 3, 4, 6]$$$ is less than or equal to $$$5$$$ and obviously less than or equal to $$$6$$$.In the second example you cannot choose any number that only $$$2$$$ elements of the given sequence will be less than or equal to this number because $$$3$$$ elements of the given sequence will be also less than or equal to this number.
PASSED
1,200
standard input
2 seconds
The first line of the input contains integer numbers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$0 \le k \le n$$$). The second line of the input contains $$$n$$$ integer numbers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the sequence itself.
["6", "-1"]
#include<stdio.h> long long int count; void merge(long long int arr[],long long int l,long long int m,long long int r) { long long int i,j,k,a; long long int x = m-l+1; long long int y = r-m; long long int left[x],right[y]; for(i=0;i<x;i++) { left[i] = arr[l+i]; } for(i=0;i<y;i++) { right[i] = arr[m+1+i]; } i=0; j=0; k=l; while(i<x && j<y) { if(left[i]<=right[j]) { arr[k] = left[i]; i++; } else{ arr[k] = right[j]; j++; } k++; } while(i<x) { arr[k] = left[i]; i++; k++; } while(j<y) { arr[k] = right[j]; j++; k++; } } void sort(long long int arr[],long long int l,long long int r) { if(l<r) { long long int m = l + (r-l)/2; sort(arr,l,m); sort(arr,m+1,r); merge(arr,l,m,r); } } int main() { long long int a,n,k,h,ans=-1; scanf("%lld %lld",&n,&h); long long int arr[n]; for(a=0;a<n;a++) { scanf("%lld",&arr[a]); } sort(arr,0,n-1); if(arr[h]!=arr[h-1]) ans=arr[h-1]; if(h==0) { if(arr[0]==1) ans=-1; else ans=1; } printf("%lld",ans); }
You are given a sequence of integers of length $$$n$$$ and integer number $$$k$$$. You should print any integer number $$$x$$$ in the range of $$$[1; 10^9]$$$ (i.e. $$$1 \le x \le 10^9$$$) such that exactly $$$k$$$ elements of given sequence are less than or equal to $$$x$$$.Note that the sequence can contain equal elements.If there is no such $$$x$$$, print "-1" (without quotes).
Print any integer number $$$x$$$ from range $$$[1; 10^9]$$$ such that exactly $$$k$$$ elements of given sequence is less or equal to $$$x$$$. If there is no such $$$x$$$, print "-1" (without quotes).
C
55297e2a65144323af4d6abd6a6ef050
7ca1fdcf960a56f5720ffc49755a940d
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "sortings" ]
1567258500
["7 4\n3 7 5 1 10 3 20", "7 2\n3 7 5 1 10 3 20"]
NoteIn the first example $$$5$$$ is also a valid answer because the elements with indices $$$[1, 3, 4, 6]$$$ is less than or equal to $$$5$$$ and obviously less than or equal to $$$6$$$.In the second example you cannot choose any number that only $$$2$$$ elements of the given sequence will be less than or equal to this number because $$$3$$$ elements of the given sequence will be also less than or equal to this number.
PASSED
1,200
standard input
2 seconds
The first line of the input contains integer numbers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$0 \le k \le n$$$). The second line of the input contains $$$n$$$ integer numbers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the sequence itself.
["6", "-1"]
#include<stdio.h> #include<string.h> int arr[200005]; int temp[200005]; void merge(int l, int r, int m) { int count1=0,count2=0,count3=0; while(count1<m-l+1 && count2<r-m) { if(arr[l+count1]<arr[m+1+count2]) { temp[count3]=arr[l+count1]; count1++; count3++; } else { temp[count3]=arr[m+1+count2]; count2++; count3++; } } while(count1<m-l+1) { temp[count3]=arr[l+count1]; count1++; count3++; } while(count2<r-m) { temp[count3]=arr[m+1+count2]; count2++; count3++; } for(int i=0;i<count3;i++) { arr[l+i]=temp[i]; } } void mergesort(int l,int r) { int m=l+(r-l)/2; if(l<r) { mergesort(l,m); mergesort(m+1,r); merge(l,r,m); } } int main() { int n,k; scanf("%d %d",&n,&k); for(int i=0;i<n;i++) { scanf("%d",&arr[i]); } mergesort(0,n); // for(int i=1;i<=n;i++) // { // printf("%d ",arr[i]); // } int ans; if(arr[k]==arr[k+1] && k!=n) { printf("-1\n"); return 0; } else if(k==n) { printf("%d\n",arr[k]); return 0; } else { if(k==0) { if(arr[1]==0 || arr[1]==1) { printf("-1\n"); return 0; } else { printf("1\n"); return 0; } } printf("%d\n",arr[k]); } return 0; }
You are given a sequence of integers of length $$$n$$$ and integer number $$$k$$$. You should print any integer number $$$x$$$ in the range of $$$[1; 10^9]$$$ (i.e. $$$1 \le x \le 10^9$$$) such that exactly $$$k$$$ elements of given sequence are less than or equal to $$$x$$$.Note that the sequence can contain equal elements.If there is no such $$$x$$$, print "-1" (without quotes).
Print any integer number $$$x$$$ from range $$$[1; 10^9]$$$ such that exactly $$$k$$$ elements of given sequence is less or equal to $$$x$$$. If there is no such $$$x$$$, print "-1" (without quotes).
C
55297e2a65144323af4d6abd6a6ef050
b8af1b4c7fe791771f570a9e9f51960c
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "sortings" ]
1567258500
["7 4\n3 7 5 1 10 3 20", "7 2\n3 7 5 1 10 3 20"]
NoteIn the first example $$$5$$$ is also a valid answer because the elements with indices $$$[1, 3, 4, 6]$$$ is less than or equal to $$$5$$$ and obviously less than or equal to $$$6$$$.In the second example you cannot choose any number that only $$$2$$$ elements of the given sequence will be less than or equal to this number because $$$3$$$ elements of the given sequence will be also less than or equal to this number.
PASSED
1,200
standard input
2 seconds
The first line of the input contains integer numbers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$0 \le k \le n$$$). The second line of the input contains $$$n$$$ integer numbers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the sequence itself.
["6", "-1"]
#include<stdio.h> typedef long long int ll; void merge(ll arr[], ll l, ll m, ll r) { ll i, j, k; ll n1 = m - l + 1; ll n2 = r - m; /* create temp arrays */ ll L[n1], R[n2]; /* Copy data to temp arrays L[] and R[] */ for (i = 0; i < n1; i++) L[i] = arr[l + i]; for (j = 0; j < n2; j++) R[j] = arr[m + 1+ j]; /* Merge the temp arrays back into arr[l..r]*/ i = 0; // Initial index of first subarray j = 0; // Initial index of second subarray k = l; // Initial index of merged subarray while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy the remaining elements of L[], if there are any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy the remaining elements of R[], if there are any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } /* l is for left index and r is right index of the sub-array of arr to be sorted */ void mergeSort(long long int arr[], long long int l,long long int r) { if (l < r) { // Same as (l+r)/2, but avoids overflow for // large l and h ll m = l+(r-l)/2; // Sort first and second halves mergeSort(arr, l, m); mergeSort(arr, m+1, r); merge(arr, l, m, r); } } int main() { long long int n,k; scanf("%lld%lld",&n,&k); long long int i; long long int arr[n]; for(i=0;i<n;i++) { scanf("%lld",&arr[i]); } mergeSort(arr,0,n-1); if(k==0) { if(arr[0]==1) printf("-1"); else printf("%lld\n",arr[0]-1); } else { if(arr[k-1]==arr[k]) printf("-1\n"); else { printf("%lld\n",arr[k-1]); } } return 0; }
You are given a sequence of integers of length $$$n$$$ and integer number $$$k$$$. You should print any integer number $$$x$$$ in the range of $$$[1; 10^9]$$$ (i.e. $$$1 \le x \le 10^9$$$) such that exactly $$$k$$$ elements of given sequence are less than or equal to $$$x$$$.Note that the sequence can contain equal elements.If there is no such $$$x$$$, print "-1" (without quotes).
Print any integer number $$$x$$$ from range $$$[1; 10^9]$$$ such that exactly $$$k$$$ elements of given sequence is less or equal to $$$x$$$. If there is no such $$$x$$$, print "-1" (without quotes).
C
55297e2a65144323af4d6abd6a6ef050
5fae256d3dcdb7b72508275c1d170f1b
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "sortings" ]
1567258500
["7 4\n3 7 5 1 10 3 20", "7 2\n3 7 5 1 10 3 20"]
NoteIn the first example $$$5$$$ is also a valid answer because the elements with indices $$$[1, 3, 4, 6]$$$ is less than or equal to $$$5$$$ and obviously less than or equal to $$$6$$$.In the second example you cannot choose any number that only $$$2$$$ elements of the given sequence will be less than or equal to this number because $$$3$$$ elements of the given sequence will be also less than or equal to this number.
PASSED
1,200
standard input
2 seconds
The first line of the input contains integer numbers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$0 \le k \le n$$$). The second line of the input contains $$$n$$$ integer numbers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the sequence itself.
["6", "-1"]
#include<stdio.h> #include<stdlib.h> # define lli long long int int a[200007]={0},b[200007]={0}; int compare(const void* a, const void* b) { const int* x = (int*) a; const int* y = (int*) b; if (*x > *y) return 1; else if (*x < *y) return -1; return 0; } void merge(int arr[],lli start,lli mid,lli end) { lli i,j,k; lli n1 = mid-start+1; lli n2 = end-(mid+1)+1; int l[n1],r[n2]; for(i=0;i<n1;i++) { l[i] = arr[start+i]; } for(i=0;i<n2;i++) { r[i] = arr[mid+1+i]; } i=0; j=0; k=start; while(i<n1 && j<n2) { if(l[i] <= r[j]) { arr[k]=l[i]; i++; k++; } else { arr[k]=r[j]; j++; k++; } } while(i<n1) { arr[k]=l[i]; i++; k++; } while(j<n2) { arr[k]=r[j]; j++; k++; } } void mergesort(int arr[],lli start,lli end) { if(start < end) { lli mid = start + (end - start)/2; mergesort(arr,start,mid); mergesort(arr,mid+1,end); merge(arr,start,mid,end); } } int main() { lli n,k; scanf("%lld %lld",&n,&k); a[0]=0; for(lli i=1;i<=n;i++) { scanf("%d",&a[i]); } // qsort(a,n+1,sizeof(int),compare); mergesort(a,0,n); /* for(lli i=0;i<=n;i++) { printf("%d ",a[i]); } printf("\n");*/ if(k==0) { if(a[1]==1) printf("-1\n"); else printf("1\n"); return 0; } if(k==n) { if(((a[k])<=1000000000) && ((a[k])>=1)) printf("%d\n",a[k]); return 0; } else { if((a[k+1]!=a[k])&& ((a[k])<=1000000000) && ((a[k])>=1)) printf("%d\n",a[k]); // if(((a[k+1]-a[k])>1) && ((a[k]+1)<=1000000000) && ((a[k]+1)>=1)) // printf("%d\n",a[k]+1); // else if((a[k]==k) && (a[k+1]>k) && ((a[k])<=1000000000) && ((a[k])>=1)) // printf("%d\n",a[k]); else printf("-1\n"); } return 0; }
You are given a sequence of integers of length $$$n$$$ and integer number $$$k$$$. You should print any integer number $$$x$$$ in the range of $$$[1; 10^9]$$$ (i.e. $$$1 \le x \le 10^9$$$) such that exactly $$$k$$$ elements of given sequence are less than or equal to $$$x$$$.Note that the sequence can contain equal elements.If there is no such $$$x$$$, print "-1" (without quotes).
Print any integer number $$$x$$$ from range $$$[1; 10^9]$$$ such that exactly $$$k$$$ elements of given sequence is less or equal to $$$x$$$. If there is no such $$$x$$$, print "-1" (without quotes).
C
55297e2a65144323af4d6abd6a6ef050
360a8fd0dee0d0ae27b3908e4ff3b09f
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "sortings" ]
1567258500
["7 4\n3 7 5 1 10 3 20", "7 2\n3 7 5 1 10 3 20"]
NoteIn the first example $$$5$$$ is also a valid answer because the elements with indices $$$[1, 3, 4, 6]$$$ is less than or equal to $$$5$$$ and obviously less than or equal to $$$6$$$.In the second example you cannot choose any number that only $$$2$$$ elements of the given sequence will be less than or equal to this number because $$$3$$$ elements of the given sequence will be also less than or equal to this number.
PASSED
1,200
standard input
2 seconds
The first line of the input contains integer numbers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$0 \le k \le n$$$). The second line of the input contains $$$n$$$ integer numbers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the sequence itself.
["6", "-1"]
#include <stdio.h> #include <stdlib.h> int compare(const void * a, const void * b) { return ( *(int*)a - *(int*)b ); } int main () { int n, k; int *arr; scanf("%d %d", &n, &k); arr = calloc(n, sizeof(int)); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } qsort(arr, n, sizeof(int), compare); if (k == 0) { for (int i = 0; i < n; i++) { if (arr[i] == 1) { printf("-1"); return 0; } } printf("1"); } else if (arr[k] == arr[k - 1]) printf("-1"); else printf("%d", arr[k - 1]); return 0; }
You are given a sequence of integers of length $$$n$$$ and integer number $$$k$$$. You should print any integer number $$$x$$$ in the range of $$$[1; 10^9]$$$ (i.e. $$$1 \le x \le 10^9$$$) such that exactly $$$k$$$ elements of given sequence are less than or equal to $$$x$$$.Note that the sequence can contain equal elements.If there is no such $$$x$$$, print "-1" (without quotes).
Print any integer number $$$x$$$ from range $$$[1; 10^9]$$$ such that exactly $$$k$$$ elements of given sequence is less or equal to $$$x$$$. If there is no such $$$x$$$, print "-1" (without quotes).
C
55297e2a65144323af4d6abd6a6ef050
10299592e895cd4085d052cbbf261d72
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "sortings" ]
1567258500
["7 4\n3 7 5 1 10 3 20", "7 2\n3 7 5 1 10 3 20"]
NoteIn the first example $$$5$$$ is also a valid answer because the elements with indices $$$[1, 3, 4, 6]$$$ is less than or equal to $$$5$$$ and obviously less than or equal to $$$6$$$.In the second example you cannot choose any number that only $$$2$$$ elements of the given sequence will be less than or equal to this number because $$$3$$$ elements of the given sequence will be also less than or equal to this number.
PASSED
1,200
standard input
2 seconds
The first line of the input contains integer numbers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$0 \le k \le n$$$). The second line of the input contains $$$n$$$ integer numbers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the sequence itself.
["6", "-1"]
#include <stdio.h> #include <stdlib.h> void bubble_sort(int arr[], int n){ int i, j; for (i=0;i<n;i++){ for(j=0;j<n-i-1;j++){ if (arr[j]>arr[j+1]){ swap_element(&arr[j],&arr[j+1]); } } } } void swap_element(int *a, int *b){ int temp = *a; *a = *b; *b = temp; } int compare (const void * a, const void * b) { return ( *(int*)a - *(int*)b ); } int main() { int n, k, result; result = -1; scanf("%d %d",&n,&k); int seq[n]; int i; for (i=0;i<n;i++){ scanf("%d",&seq[i]); } //bubble_sort(seq,n); qsort (seq, n, sizeof(int), compare); if ((k==0)&&(seq[0]>1)){ result = 1; } if ((k>0)&&(seq[k-1]!=seq[k])){ result = seq[k-1]; } printf("%d", result); // for (i=0;i<n;i++){ // printf("%d",seq[i]); // printf(" "); // } return 0; }
You are given a sequence of integers of length $$$n$$$ and integer number $$$k$$$. You should print any integer number $$$x$$$ in the range of $$$[1; 10^9]$$$ (i.e. $$$1 \le x \le 10^9$$$) such that exactly $$$k$$$ elements of given sequence are less than or equal to $$$x$$$.Note that the sequence can contain equal elements.If there is no such $$$x$$$, print "-1" (without quotes).
Print any integer number $$$x$$$ from range $$$[1; 10^9]$$$ such that exactly $$$k$$$ elements of given sequence is less or equal to $$$x$$$. If there is no such $$$x$$$, print "-1" (without quotes).
C
55297e2a65144323af4d6abd6a6ef050
c6ece58a8fd9a11684eb8b73f5a92cf5
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "sortings" ]
1567258500
["7 4\n3 7 5 1 10 3 20", "7 2\n3 7 5 1 10 3 20"]
NoteIn the first example $$$5$$$ is also a valid answer because the elements with indices $$$[1, 3, 4, 6]$$$ is less than or equal to $$$5$$$ and obviously less than or equal to $$$6$$$.In the second example you cannot choose any number that only $$$2$$$ elements of the given sequence will be less than or equal to this number because $$$3$$$ elements of the given sequence will be also less than or equal to this number.
PASSED
1,200
standard input
2 seconds
The first line of the input contains integer numbers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$0 \le k \le n$$$). The second line of the input contains $$$n$$$ integer numbers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the sequence itself.
["6", "-1"]
#include<stdio.h> #include<stdlib.h> int comp(const void*first,const void*second) { return (*(int*)first-*(int*)second); } int main() { long long int a[200005]={},n,k,i,j; scanf("%lld %lld",&n,&k); for(i=0;i<n;i++) { scanf("%lld",&a[i]); } qsort(a, n, sizeof(long long int), comp); if(k==0 && a[0]==1) { printf("-1"); } else if(k==0 && a[0]>1) { printf("1\n"); } else if(a[k]==a[k-1]) { printf("-1\n"); } else { printf("%lld\n",a[k-1]); } return 0; }
You are given a sequence of integers of length $$$n$$$ and integer number $$$k$$$. You should print any integer number $$$x$$$ in the range of $$$[1; 10^9]$$$ (i.e. $$$1 \le x \le 10^9$$$) such that exactly $$$k$$$ elements of given sequence are less than or equal to $$$x$$$.Note that the sequence can contain equal elements.If there is no such $$$x$$$, print "-1" (without quotes).
Print any integer number $$$x$$$ from range $$$[1; 10^9]$$$ such that exactly $$$k$$$ elements of given sequence is less or equal to $$$x$$$. If there is no such $$$x$$$, print "-1" (without quotes).
C
55297e2a65144323af4d6abd6a6ef050
078504522c223fa51d3be27e56d6661f
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "sortings" ]
1567258500
["7 4\n3 7 5 1 10 3 20", "7 2\n3 7 5 1 10 3 20"]
NoteIn the first example $$$5$$$ is also a valid answer because the elements with indices $$$[1, 3, 4, 6]$$$ is less than or equal to $$$5$$$ and obviously less than or equal to $$$6$$$.In the second example you cannot choose any number that only $$$2$$$ elements of the given sequence will be less than or equal to this number because $$$3$$$ elements of the given sequence will be also less than or equal to this number.
PASSED
1,200
standard input
2 seconds
The first line of the input contains integer numbers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$0 \le k \le n$$$). The second line of the input contains $$$n$$$ integer numbers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the sequence itself.
["6", "-1"]
#include<stdio.h> #include<limits.h> void sort(long long int arr[],int n) { long long int min,temp,index; for(int i=0;i<n-1;i++) { min = arr[i]; index = i; for(int j=i+1;j<n;j++) { if(min>arr[j]) { min = arr[j]; index = j; } } temp = arr[i]; arr[i] = min; arr[index] = temp; } } void merge(int arr[], int l, int m, int r) { int i, j, k; int n1 = m - l + 1; int n2 = r - m; int arr1[n1], arr2[n2]; for(int i=0;i<n1;i++) { arr1[i] = arr[l + i]; } for(int j=0;j<n2;j++) { arr2[j] = arr[m+1+j]; } i=0;j=0;k=l; while(i<n1 && j<n2) { if(arr1[i]<=arr2[j]) { arr[k] = arr1[i]; i++; } else { arr[k] = arr2[j]; j++; } k++; } while(i<n1) { arr[k] = arr1[i]; k++; i++; } while(j<n2) { arr[k] = arr2[j]; k++; j++; } } void mergeSort(int arr[], int l, int r) { if (l < r) { // Same as (l+r)/2, but avoids overflow for // large l and h int m = l+(r-l)/2; // Sort first and second halves mergeSort(arr, l, m); mergeSort(arr, m+1, r); merge(arr, l, m, r); } } int main() { int n,k; scanf("%d %d",&n,&k); int arr[n]; for(int i=0;i<n;i++) { scanf("%d",&arr[i]); } mergeSort(arr,0,n-1); /* for(int i=0;i<n;i++) { printf("%d ",arr[i]); } printf("\n");*/ if(k==0) { if(arr[0]>1) { printf("%d\n",arr[0]-1); } else { printf("%d",-1); } return 0; } if(n==1) { if(arr[0]>1) { printf("%d\n",arr[0]); } else { printf("%d",-1); } return 0; } if(n==k) { printf("%d",arr[k-1]); return 0; } if(arr[k]-arr[k-1]<1) { printf("-1"); } else { printf("%d",arr[k]-1); } }
You are given a sequence of integers of length $$$n$$$ and integer number $$$k$$$. You should print any integer number $$$x$$$ in the range of $$$[1; 10^9]$$$ (i.e. $$$1 \le x \le 10^9$$$) such that exactly $$$k$$$ elements of given sequence are less than or equal to $$$x$$$.Note that the sequence can contain equal elements.If there is no such $$$x$$$, print "-1" (without quotes).
Print any integer number $$$x$$$ from range $$$[1; 10^9]$$$ such that exactly $$$k$$$ elements of given sequence is less or equal to $$$x$$$. If there is no such $$$x$$$, print "-1" (without quotes).
C
55297e2a65144323af4d6abd6a6ef050
6a3cbfb3a549626acafb13a9fcc1ffa9
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "sortings" ]
1567258500
["7 4\n3 7 5 1 10 3 20", "7 2\n3 7 5 1 10 3 20"]
NoteIn the first example $$$5$$$ is also a valid answer because the elements with indices $$$[1, 3, 4, 6]$$$ is less than or equal to $$$5$$$ and obviously less than or equal to $$$6$$$.In the second example you cannot choose any number that only $$$2$$$ elements of the given sequence will be less than or equal to this number because $$$3$$$ elements of the given sequence will be also less than or equal to this number.
PASSED
1,200
standard input
2 seconds
The first line of the input contains integer numbers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$0 \le k \le n$$$). The second line of the input contains $$$n$$$ integer numbers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the sequence itself.
["6", "-1"]
#include<stdio.h> #include<limits.h> void merge(int arr[], int l, int m, int r) { int i, j, k; int n1 = m - l + 1; int n2 = r - m; int arr1[n1], arr2[n2]; for(int i=0;i<n1;i++) { arr1[i] = arr[l + i]; } for(int j=0;j<n2;j++) { arr2[j] = arr[m+1+j]; } i=0;j=0;k=l; while(i<n1 && j<n2) { if(arr1[i]<=arr2[j]) { arr[k] = arr1[i]; i++; } else { arr[k] = arr2[j]; j++; } k++; } while(i<n1) { arr[k] = arr1[i]; k++; i++; } while(j<n2) { arr[k] = arr2[j]; k++; j++; } } void mergeSort(int arr[], int l, int r) { if (l < r) { int m = (l+r)/2; mergeSort(arr, l, m); mergeSort(arr, m+1, r); merge(arr, l, m, r); } } int main() { int n,k; scanf("%d %d",&n,&k); int arr[n]; for(int i=0;i<n;i++) { scanf("%d",&arr[i]); } mergeSort(arr,0,n-1); if(k==0) { if(arr[0]>1) { printf("%d\n",arr[0]-1); } else { printf("%d",-1); } return 0; } if(n==1) { if(arr[0]>1) { printf("%d\n",arr[0]); } else { printf("%d",-1); } return 0; } if(n==k) { printf("%d",arr[k-1]); return 0; } if(arr[k]-arr[k-1]<1) { printf("-1"); } else { printf("%d",arr[k]-1); } }
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
C
e2836276aee2459979b232e5b29e6d57
d931c667c2d542051875147432f6d375
GNU C
standard output
256 megabytes
train_002.jsonl
[ "dsu", "dfs and similar" ]
1362065400
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
PASSED
1,400
standard input
2 seconds
The first line contains two integers n and m (2 ≤ n, m ≤ 100) — the number of employees and the number of languages. Then n lines follow — each employee's language list. At the beginning of the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
["0", "2", "1"]
#include <stdio.h> #define N 110 int emp, lan, ans = 0, non = 0; int num_lan[N][N], lan_link[N][N], marked[N], emp_lan[N]; void dfs (int x) { int j; marked[x] = 1; for (j = 0; j < lan; j++) { if (lan_link[x][j] && !marked[j]) { dfs (j); } } } int main() { int i, j, k, l; scanf("%d %d", &emp, &lan); for (i = 0; i < emp; i++) { scanf("%d", &j); emp_lan[i] = j; for (k = 0; k < j; k++) { scanf("%d", &num_lan[i][k]); num_lan[i][k]--; } } for (i = 0; i < emp; i++) { for (j = 0; j < emp_lan[i] - 1; j++) { k = num_lan[i][j]; l = num_lan[i][j + 1]; lan_link[k][l] = 1; lan_link[l][k] = 1; } if (emp_lan[i] == 0) ++non; } for (i = 0; i < emp; i++) { for (j = 0; j < emp_lan[i]; ++j) { if (!marked[num_lan[i][j]]) { dfs(num_lan[i][j]); ++ans; // printf("= %d %d %d\n", i, j, emp_lan[i]); } } } if (ans > 0) --ans; printf("%d\n", non + ans); return 0; }
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
C
e2836276aee2459979b232e5b29e6d57
ee16c86468f6a6614880349fb7f5f60d
GNU C
standard output
256 megabytes
train_002.jsonl
[ "dsu", "dfs and similar" ]
1362065400
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
PASSED
1,400
standard input
2 seconds
The first line contains two integers n and m (2 ≤ n, m ≤ 100) — the number of employees and the number of languages. Then n lines follow — each employee's language list. At the beginning of the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
["0", "2", "1"]
#include<stdio.h> #include<string.h> int lang[105],re[105],p[105]; int fin(int x) { if(re[x]==x) return x; else return fin(re[x]); } int main() { int n,m; int i,j,flg; int k,a; int sum1=0,sum2=0; scanf("%d %d",&n,&m); for(i=1;i<=n;i++) re[i]=i; for(i=1;i<=n;i++) { scanf("%d",&k); if(k==0) p[i]=1; while(k--) { scanf("%d",&a); if(lang[a]==0) lang[a]=i; else { if(re[i]==i) re[i]=fin(lang[a]); else { int c=fin(lang[a]); if(c!=fin(i)); re[c]=fin(i); } } } } flg=1; for(i=1;i<=n;i++) { if(p[i]==1) sum1++; if(p[i]!=1&&re[i]==i) { if(flg) flg=0; else sum2++; } } printf("%d\n",sum1+sum2); return 0; }
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
C
e2836276aee2459979b232e5b29e6d57
62365fec84d26ad061907d04ab47962f
GNU C
standard output
256 megabytes
train_002.jsonl
[ "dsu", "dfs and similar" ]
1362065400
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
PASSED
1,400
standard input
2 seconds
The first line contains two integers n and m (2 ≤ n, m ≤ 100) — the number of employees and the number of languages. Then n lines follow — each employee's language list. At the beginning of the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
["0", "2", "1"]
#include<stdio.h> #include<stdlib.h> #include<string.h> struct node { int val; struct node* next; }; typedef struct node node; node* insert(node* head, int data) { node* newnode=(node*)malloc(sizeof(node)); newnode->val=data; newnode->next=NULL; if(!head) head=newnode; else { newnode->next=head; head=newnode; } return head; } void printlist(node* head) { node* temp=head; while(temp) { printf("%d ",temp->val); temp=temp->next; } printf("\n"); } void dfs(node** list, int index, int visited[]) { node* temp=list[index]; while(temp) { if(!visited[temp->val]) { visited[temp->val]=1; dfs(list,temp->val,visited); } temp=temp->next; } return; } int main() { int n; int m; scanf("%d %d",&n,&m); node** language_index=(node**)malloc(sizeof(node)*(m+1)); node** spoken=(node**)malloc(sizeof(node)*(n+1)); node** list=(node**)malloc(sizeof(node)*(n+1)); int i; for(i=0;i<=m;i++) language_index[i]=NULL; for(i=0;i<=n;i++) spoken[i]=NULL; for(i=0;i<=n;i++) list[i]=NULL; int count,j,lang,flag=0; for(i=1;i<=n;i++) { scanf("%d",&count); if(count!=0) flag=1; for(j=0;j<count;j++) { scanf("%d",&lang); language_index[lang]=insert(language_index[lang],i); spoken[i]=insert(spoken[i],lang); } } if(!flag) { printf("%d\n",n); return 0; } for(i=1;i<=n;i++) { node* temp1=spoken[i]; while(temp1) { //printf("spoken %d: %d\n",i,temp1->val); node* temp2=language_index[temp1->val]; while(temp2) { if(temp2->val!=i) list[i]=insert(list[i],temp2->val); temp2=temp2->next; } temp1=temp1->next; } } //printf("\n"); int visited[n+1]; memset(visited,0,sizeof(visited)); int ans=0; for(i=1;i<=n;i++) { if(!visited[i]) { //printf("%d\n",i); visited[i]=1; ans++; dfs(list,i,visited); } } printf("%d\n",ans-1); return 0; }
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
C
e2836276aee2459979b232e5b29e6d57
a1a1628a9a83980be8633e44d61cfdfc
GNU C
standard output
256 megabytes
train_002.jsonl
[ "dsu", "dfs and similar" ]
1362065400
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
PASSED
1,400
standard input
2 seconds
The first line contains two integers n and m (2 ≤ n, m ≤ 100) — the number of employees and the number of languages. Then n lines follow — each employee's language list. At the beginning of the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
["0", "2", "1"]
#include<stdio.h> int f[105]; int fa(int x) { int z=x,y; while (f[z]!=z) z=f[z]; while (x!=z) { y=f[x]; f[x]=z; x=y; } } int main() { int i,j,n,m,ans=0; int b[105][105]={0}; scanf("%d%d",&n,&m); for (i=1;i<=n;i++) { int x,y; scanf("%d",&x); for (j=1;j<=x;j++) { scanf("%d",&y); b[i][y]=1; } } for (i=0;i<=n;i++) f[i]=i; int flag=0; for (i=1;i<=m;i++) { int x=0; for (j=1;j<=n;j++) if (b[j][i]) flag=x=j; fa(x); for (j=1;j<=n;j++) if (b[j][i]) { fa(j); f[f[j]]=f[x]; } } for (i=1;i<=n;i++) fa(i); for (i=1;i<=n;i++) if (f[i]==i) ans++; if (!flag) ans++; printf("%d",ans-1); return 0; }
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
C
e2836276aee2459979b232e5b29e6d57
ec756c2647277615b7493921ebe798e3
GNU C
standard output
256 megabytes
train_002.jsonl
[ "dsu", "dfs and similar" ]
1362065400
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
PASSED
1,400
standard input
2 seconds
The first line contains two integers n and m (2 ≤ n, m ≤ 100) — the number of employees and the number of languages. Then n lines follow — each employee's language list. At the beginning of the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
["0", "2", "1"]
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define NO_ID 0 static unsigned langs[101]; static bool seen[101]; int main() { unsigned n_employees, n_langs, n_cur_langs, *cur_langs, id, my_id, cur_id, cost, graphs, i, j, k; cost = 0; graphs = 0; id = NO_ID; scanf("%u %u", &n_employees, &n_langs); for (i = 0; i < n_employees; i++) { scanf("%u", &n_cur_langs); if (n_cur_langs != 0) { cur_langs = malloc(sizeof(unsigned) * n_cur_langs); for (j = 0; j < n_cur_langs; j++) scanf("%u", cur_langs + j); my_id = id + 1; for (j = 0; j < n_cur_langs; j++) { cur_id = langs[cur_langs[j]]; if (cur_id != NO_ID && cur_id < my_id) my_id = cur_id; } if (my_id == id + 1) id++; for (j = 0; j < n_cur_langs; j++) { cur_id = langs[cur_langs[j]]; if (cur_id == NO_ID) { langs[cur_langs[j]] = my_id; } else { // Merge: cur_id becomes my_id. for (k = 1; k <= n_langs; k++) if (langs[k] == cur_id) langs[k] = my_id; } } } else { cost++; } } graphs = 0; for (i = 1; i <= n_langs; i++) { if (langs[i] != NO_ID && !seen[langs[i]]) { graphs++; seen[langs[i]] = true; } } if (graphs > 0) cost += graphs - 1; printf("%u\n", cost); return 0; }
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
C
e2836276aee2459979b232e5b29e6d57
0cc9c2d7a4e5ef11ef361de18fa0cf79
GNU C
standard output
256 megabytes
train_002.jsonl
[ "dsu", "dfs and similar" ]
1362065400
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
PASSED
1,400
standard input
2 seconds
The first line contains two integers n and m (2 ≤ n, m ≤ 100) — the number of employees and the number of languages. Then n lines follow — each employee's language list. At the beginning of the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
["0", "2", "1"]
#include <stdio.h> int n; char lang[128][128], mat[128][128]; char mark[128]; int dfs(int x) { int i; if ( mark[x] ) return 0; mark[x] = 1; for ( i = 0; i < n; ++i ) if ( mat[x][i] ) dfs(i); return 0; } int main() { int i, j, k, m, nol = 1; scanf("%d%d", &n, &m); for ( i = 0; i < n; ++i ) { scanf("%d", &k); if ( k ) nol = 0; for ( j = 0; j < k; ++j ) { int t; scanf("%d", &t); lang[i][--t] = 1; } } for ( i = 0; i < n; ++i ) for ( j = 0; j < n; ++j ) for ( k = 0; k < m; ++k ) if ( lang[i][k] && lang[j][k] ) { mat[i][j] = 1; break; } for ( i = j = 0; i < n; ++i ) if ( !mark[i] ) { dfs(i); ++j; } if ( --j < 0 ) j = 0; if ( nol ) j = n; printf("%d\n", j); return 0; }
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
C
e2836276aee2459979b232e5b29e6d57
da394ca1bfa85263bc773e1754505054
GNU C
standard output
256 megabytes
train_002.jsonl
[ "dsu", "dfs and similar" ]
1362065400
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
PASSED
1,400
standard input
2 seconds
The first line contains two integers n and m (2 ≤ n, m ≤ 100) — the number of employees and the number of languages. Then n lines follow — each employee's language list. At the beginning of the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
["0", "2", "1"]
#include<stdio.h> #include<stdlib.h> #define M 100 int main(){ int **graph,n,m,k,i,j,val,*l,*w,found2, found,count; scanf("%d",&n); scanf("%d",&m); if(n<1||n>100||m<1||m>100) return -1; graph=malloc(n*sizeof(int *)); for(i=0; i<n; i++){ graph[i]=malloc(m*sizeof(int)); } l=malloc(m*sizeof(int)); w=malloc(n*sizeof(int)); for(i=0; i<n; i++){ for(j=0; j<m; j++){ graph[i][j]=0; } } for(i=0; i<n; i++){ l[i]=0; w[i]=0; } found = 0; for(i=0; i<n; i++){ scanf("%d",&k); if(k!=0){ found=1; } for(j=0; j<k; j++){ scanf("%d",&val); graph[i][val-1]=1; } } if(found==0){ printf("%d",n); return 0; } w[0]=1; i=0; count=0; while(i<n){ found = 0; found2 = 0; for(k=0; k<n&&!found; k++){ if(w[k]==1){ i=k; found = 1; } } for(k=0; !found&&k<n&&!found2; k++){ if(w[k]==0){ i=k; w[k]=1; found2=1; count++; } } if(found || found2){ for(j=0; j<m; j++){ if(graph[i][j]==1&&l[j]!=1){ for(k=0; k<n; k++){ if(k!=i&&graph[k][j]==1){ w[k]=1; } } l[j]=1; } } w[i]=2; }else{ i=n; } } printf("%d",count); return 0; }
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
C
e2836276aee2459979b232e5b29e6d57
d847f9a0fea1ba0972eff05f12bbf71b
GNU C
standard output
256 megabytes
train_002.jsonl
[ "dsu", "dfs and similar" ]
1362065400
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
PASSED
1,400
standard input
2 seconds
The first line contains two integers n and m (2 ≤ n, m ≤ 100) — the number of employees and the number of languages. Then n lines follow — each employee's language list. At the beginning of the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
["0", "2", "1"]
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct _node *link; typedef struct _node { link next; int value; } node; typedef struct _vertex { link adj; int set; } vertex; void dfsColor(vertex arr[], int colored[], int length, int start); int numDisjoint(vertex arr[], int length); link addNode(link l, int val); int main (int argc, char *argv[]) { int numEmp, numLang; int i, j; int knownLang; int currLang, prevLang; int none = 0; int numGraphs; scanf("%d %d", &numEmp, &numLang); vertex arr[numLang]; memset(arr, 0, numLang * sizeof(vertex)); for (i = 0; i < numEmp; i++) { scanf("%d", &knownLang); if (knownLang == 0) { none++; } prevLang = 0; for (j = 0; j < knownLang; j++) { scanf("%d", &currLang); arr[currLang - 1].set = 1; if (prevLang != 0) { arr[currLang - 1].adj = addNode(arr[currLang - 1].adj, prevLang); arr[prevLang - 1].adj = addNode(arr[prevLang - 1].adj, currLang); } prevLang = currLang; } } numGraphs = numDisjoint(arr, numLang); if (numGraphs > 0) { printf("%d\n", numGraphs + none - 1); } else { printf("%d\n", none); } return EXIT_SUCCESS; } int numDisjoint(vertex arr[], int length) { int i; int numDisjoint = 0; int colored[length]; memset(colored, 0, length * sizeof(int)); for (i = 0; i < length; i++) { if (arr[i].set && !colored[i]) { dfsColor(arr, colored, length, i); numDisjoint++; } } return numDisjoint; } void dfsColor(vertex arr[], int colored[], int length, int start) { colored[start] = 1; link curr = arr[start].adj; while (curr != NULL) { if (!colored[curr->value - 1]) { dfsColor(arr, colored, length, curr->value - 1); } curr = curr->next; } } link addNode(link l, int val) { link newNode; link curr = l; if (l == NULL) { newNode = malloc(sizeof(node)); newNode->value = val; newNode->next = NULL; return newNode; } else { while (curr->next != NULL) { if (curr-> value == val) { return l; } curr = curr->next; } if (curr->value != val) { newNode = malloc(sizeof(node)); newNode->value = val; newNode->next = NULL; curr->next = newNode; } } return l; }
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
C
e2836276aee2459979b232e5b29e6d57
4865c7a0f09527eecdfa0b4501978549
GNU C
standard output
256 megabytes
train_002.jsonl
[ "dsu", "dfs and similar" ]
1362065400
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
PASSED
1,400
standard input
2 seconds
The first line contains two integers n and m (2 ≤ n, m ≤ 100) — the number of employees and the number of languages. Then n lines follow — each employee's language list. At the beginning of the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
["0", "2", "1"]
#include <stdio.h> #include <stdlib.h> #include <string.h> int n, m; int a[101][101]; int v[101]; int visit(int x) { int i, j; if (v[x] == 1) return 0; v[x] = 1; for (j=1; j<=m; j++) { if (a[x][j] == 1) { for (i=1; i<=n; i++) { if (a[i][j] == 1) { visit(i); } } } } return 1; } int main(void) { int i, j, k, l, lang; memset(a, 0, sizeof(a)); memset(v, 0, sizeof(v)); lang = 0; scanf("%d %d", &n, &m); for (i=1; i<=n; i++) { scanf("%d", &k); lang += k; for (j=1; j<=k; j++) { scanf("%d", &l); a[i][l] = 1; } } for (i=1, j=0; i<=n; i++) j += visit(i); printf("%d\n", (lang != 0) ? j-1 : j); return 0; }
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
C
e2836276aee2459979b232e5b29e6d57
1541f3959a75bc2e8f2e013b25b481d3
GNU C
standard output
256 megabytes
train_002.jsonl
[ "dsu", "dfs and similar" ]
1362065400
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
PASSED
1,400
standard input
2 seconds
The first line contains two integers n and m (2 ≤ n, m ≤ 100) — the number of employees and the number of languages. Then n lines follow — each employee's language list. At the beginning of the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
["0", "2", "1"]
// contest 277 problem A - round 170 #include<stdio.h> #define MAX_V 110 #define MAX_E 10010 // knowsLanguage[i][j] is set if person i knows language j int knowsLanguage[MAX_V][MAX_V]; // observe the 110's, generally declare slightly more memory than is necessary // For constructing the graph int to[MAX_E], next[MAX_E], last[MAX_V], num_edge; // recall that globals are initialised to 0 void add_edge(int i, int j){ to[num_edge] = j; next[num_edge] = last[i]; last[i] = num_edge++; } // Flood-fill int seen[MAX_V]; void dfs(int i){ int j; seen[i] = 1; for(j=last[i]; j>=0; j=next[j]) if(!seen[to[j]]) dfs(to[j]); } int main(){ int n, m, i, j, k, x; memset(last, -1, sizeof(last)); // read the input scanf("%d%d", &n, &m); for(i=0; i<n; i++){ scanf("%d", &k); for(j=0; j<k; j++){ scanf("%d", &x); knowsLanguage[i][x-1] = 1; } } // special case - if nobody knows any language int special = 1; for(i=0; i<n; i++) for(j=0; j<m; j++) if(knowsLanguage[i][j]) special = 0; if(special){ printf("%d\n", n); return 0; } // construct the graph - an edge between two people if they know a common language for(i=0; i<n; i++) for(j=0; j<n; j++) // for all pairs of vertices for(k=0; k<m; k++) // if there is a common language, connect them if(knowsLanguage[i][k] && knowsLanguage[j][k]){ add_edge(i, j); break; // don't add multiple edges from i to j for each language they have in common } // count the number of components int num_components = 0; for(i=0; i<n; i++) if(!seen[i]){ num_components++; dfs(i); } // print the answer printf("%d\n", num_components-1); return 0; }
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
C
e2836276aee2459979b232e5b29e6d57
4f9d070fde5c3ff1b0e65086513e55e2
GNU C
standard output
256 megabytes
train_002.jsonl
[ "dsu", "dfs and similar" ]
1362065400
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
PASSED
1,400
standard input
2 seconds
The first line contains two integers n and m (2 ≤ n, m ≤ 100) — the number of employees and the number of languages. Then n lines follow — each employee's language list. At the beginning of the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
["0", "2", "1"]
#include <stdio.h> typedef struct { int k; char visit; unsigned int flag[4]; } Employee_t; Employee_t A[100]; int InSet(int i, int j) { int res = 0; res |= A[i].flag[0] & A[j].flag[0]; res |= A[i].flag[1] & A[j].flag[1]; res |= A[i].flag[2] & A[j].flag[2]; res |= A[i].flag[3] & A[j].flag[3]; return res; } void Merge(int i, int j) { A[i].flag[0] |= A[j].flag[0]; A[i].flag[1] |= A[j].flag[1]; A[i].flag[2] |= A[j].flag[2]; A[i].flag[3] |= A[j].flag[3]; } int main() { int N, M, i, j, Num, Ans = 0, Sum = 0; scanf("%d %d", &N, &M); for(i=0; i<N; ++i) { scanf("%d", &A[i].k); Sum += A[i].k; A[i].visit = 0; for(j=0; j<4; ++j) { A[i].flag[j] = 0; } for(j=0; j<A[i].k; ++j) { scanf("%d", &Num); --Num; if(Num < 32) { A[i].flag[0] |= 1<<Num; } else if(Num < 64) { A[i].flag[1] |= 1<<(Num-32); } else if(Num < 96) { A[i].flag[2] |= 1<<(Num-64); } else { A[i].flag[3] |= 1<<(Num-96); } } } if(Sum == 0) { printf("%d\n", N); return 0; } for(i=0; i<N; ++i) { if(!A[i].visit) { ++Ans; while(1) { char flag = 0; for(j=i+1; j<N; ++j) { if(!A[j].visit) { if(InSet(i, j)) { flag = 1; A[j].visit = 1; Merge(i, j); } } } if(!flag) { break; } } A[i].visit = 1; } } printf("%d\n", Ans-1); return 0; }
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
C
e2836276aee2459979b232e5b29e6d57
5c60e843455e0ea5edccae9a444c3fde
GNU C
standard output
256 megabytes
train_002.jsonl
[ "dsu", "dfs and similar" ]
1362065400
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
PASSED
1,400
standard input
2 seconds
The first line contains two integers n and m (2 ≤ n, m ≤ 100) — the number of employees and the number of languages. Then n lines follow — each employee's language list. At the beginning of the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
["0", "2", "1"]
#define N 101 #include<stdio.h> //对于某一个人一个语言都不懂的情况,需要单独处理! int n,m; int connection[N][N]; int visited[N]; void bfs(int who){ int start=0; int end=0; int queue[N]; int i; queue[end]=who; visited[queue[end]]=1; end++; while(start<end){ int top=queue[start]; start++; for(i=0;i<n;i++){ if(connection[i][top]==1 && !visited[i]){ queue[end]=i; visited[queue[end]]=1; end++; } if(connection[top][i]==1 && !visited[i]){ queue[end]=i; visited[queue[end]]=1; end++; } } } } int main() { int language[N][N]; scanf("%d%d",&n,&m); int i,j,k=0; for(i=0;i<n;i++){ //for(j=0;j<k;j++){//这种处理问题比较大! //其实编译器的提示显示的是一个错误,而不是简单地让我把 //k设置为0 for(j=0;j<=m;j++){ language[i][j]=0; } } //分开处理更准确! for(i=0;i<n;i++){ //for(j=0;j<k;j++){//这种处理问题比较大! for(j=0;j<n;j++){ connection[i][j]=0; } } //这里我只需要统计什么语言都不会的人数! //还不用统计,只需要做一个判断就可以! int all_no_language=1; for(i=0;i<n;i++){ int languages; scanf("%d",&languages); if(languages!=0){ all_no_language=0; } for(j=0;j<languages;j++){ int which; scanf("%d",&which); //我用这种技术来处理相同的语言! language[i][which]=1; } } if(all_no_language){ printf("%d\n",n); return 0; } for(k=1;k<=m;k++){ for(i=0;i<n;i++){ for(j=0;j<n;j++){ if(language[i][k]==1 && language[j][k]==1){ //代表i,j会说同一种语言! connection[i][j]=1; connection[j][i]=1; } } } } for(i=0;i<n;i++){ for(j=0;j<n;j++){ visited[j]=0; } bfs(i); for(j=0;j<n;j++){ //看来这个visited技术是我经常要使用的技术 //它能记录当前值都与哪些值相连通,就是只要被访问过的就都是连通的! if(visited[j]==1){ //有中间人翻译的! connection[i][j]=1; connection[j][i]=1; } } } for(j=0;j<n;j++){ visited[j]=0; } int count=0; j=0; while(1){ int who=-1; //这样做可以节省一点运行时间! while(1){ if(j>=n){ break; } if(!visited[j]){ who=j; break; } j++; } //问题最终抽象成,就是问有几个相互不连接的部分! //上面的代码就是先构建出整个图的结构!知道哪些地方连通,哪些地方不连通! if(who==-1)break; bfs(who); count++; } //这里还要分为会语言的和不会语言的 //不会语言的都要学习 //会语言的两个人之间只需找一个翻译就可以! //这里需要处理一个特殊情况就是所有人一个语言都不会就要所有人都去学习 //如果有一个人会一门语言,那么我们只需让不同部分的人之间学习一门语言就可以,就是不同部分个数减一次学习! printf("%d\n",count-1); return 0; }
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
C
e2836276aee2459979b232e5b29e6d57
9e93cd3cef94370ddcb2fdda1c8d424f
GNU C
standard output
256 megabytes
train_002.jsonl
[ "dsu", "dfs and similar" ]
1362065400
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
PASSED
1,400
standard input
2 seconds
The first line contains two integers n and m (2 ≤ n, m ≤ 100) — the number of employees and the number of languages. Then n lines follow — each employee's language list. At the beginning of the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
["0", "2", "1"]
#define N 101 #include<stdio.h> //对于某一个人一个语言都不懂的情况,需要单独处理! int n,m; int connection[N][N]; int visited[N]; void bfs(int who){ int start=0; int end=0; int queue[N]; int i; queue[end]=who; visited[queue[end]]=1; end++; while(start<end){ int top=queue[start]; start++; for(i=0;i<n;i++){ if(connection[i][top]==1 && !visited[i]){ queue[end]=i; visited[queue[end]]=1; end++; } if(connection[top][i]==1 && !visited[i]){ queue[end]=i; visited[queue[end]]=1; end++; } } } } int main() { int language[N][N]; scanf("%d%d",&n,&m); int i,j,k=0; for(i=0;i<n;i++){ //for(j=0;j<k;j++){//这种处理问题比较大! //其实编译器的提示显示的是一个错误,而不是简单地让我把 //k设置为0 for(j=0;j<=m;j++){ language[i][j]=0; } } //分开处理更准确! for(i=0;i<n;i++){ //for(j=0;j<k;j++){//这种处理问题比较大! for(j=0;j<n;j++){ connection[i][j]=0; } } int have_language[N]; for(i=0;i<n;i++){ have_language[i]=1; } for(i=0;i<n;i++){ int languages; scanf("%d",&languages); if(languages==0){ have_language[i]=0; } for(j=0;j<languages;j++){ int which; scanf("%d",&which); //我用这种技术来处理相同的语言! language[i][which]=1; } } for(k=1;k<=m;k++){ for(i=0;i<n;i++){ for(j=0;j<n;j++){ if(language[i][k]==1 && language[j][k]==1){ //代表i,j会说同一种语言! connection[i][j]=1; connection[j][i]=1; } } } } for(i=0;i<n;i++){ for(j=0;j<n;j++){ visited[j]=0; } bfs(i); for(j=0;j<n;j++){ //看来这个visited技术是我经常要使用的技术 //它能记录当前值都与哪些值相连通,就是只要被访问过的就都是连通的! if(visited[j]==1){ //有中间人翻译的! connection[i][j]=1; connection[j][i]=1; } } } for(j=0;j<n;j++){ visited[j]=0; } int count=0; while(1){ int who=-1; for(j=0;j<n;j++){ if(!visited[j]){ who=j; break; } } //问题最终抽象成,就是问有几个相互不连接的部分! //上面的代码就是先构建出整个图的结构!知道哪些地方连通,哪些地方不连通! if(who==-1)break; bfs(who); count++; } //这里还要分为会语言的和不会语言的 //不会语言的都要学习 //会语言的两个人之间只需找一个翻译就可以! //这里需要处理一个特殊情况就是所有人一个语言都不会就要所有人都去学习 //如果有一个人会一门语言,那么我们只需让不同部分的人之间学习一门语言就可以,就是不同部分个数减一次学习! int all_no_language=1; for(j=0;j<n;j++){ if(have_language[j]){ all_no_language=0; break; } } if(all_no_language) printf("%d\n",count); else printf("%d\n",count-1); return 0; }
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
C
e2836276aee2459979b232e5b29e6d57
b49d871f1e818e32f2dbc7e64f66a2fd
GNU C
standard output
256 megabytes
train_002.jsonl
[ "dsu", "dfs and similar" ]
1362065400
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
PASSED
1,400
standard input
2 seconds
The first line contains two integers n and m (2 ≤ n, m ≤ 100) — the number of employees and the number of languages. Then n lines follow — each employee's language list. At the beginning of the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
["0", "2", "1"]
#include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #define MOD 1000000007 struct nod { int val,d,par,finish_time,weight,num; char color; struct nod *next,*par_node; }; void init(int num_v,struct nod *p[num_v],struct nod *vr[num_v]){ int i=1; while(i<num_v){ vr[i]=malloc(sizeof(struct nod)); vr[i]->color='w'; vr[i]->val=i; vr[i]->d=0; vr[i]->next=NULL; p[i]=NULL; i++; } } void create_adj(int num_v,struct nod *p[num_v],int ver1,int ver2,int weight){ struct nod *tmp=malloc(sizeof(struct nod)); tmp->val=ver2; tmp->d=0; tmp->color='w'; tmp->next=p[ver1]; tmp->weight=weight; p[ver1]=tmp; } void visit_dfsl(int num_v,struct nod *p[num_v],struct nod *vr[num_v],int time,int vertex,struct nod **link){ time++; vr[vertex]->color='g'; vr[vertex]->d=time; struct nod *k=p[vertex]; while(k!=NULL){ if(vr[k->val]->color=='w'){ vr[k->val]->par=vertex; k->par=vertex; visit_dfsl(num_v, p, vr, time, k->val,link); } k=k->next; } vr[vertex]->color='b'; //add_topo_sort(link,vertex); time++; vr[vertex]->finish_time=time; } int nu=0; int dfsl(int num_v,struct nod *p[num_v],struct nod *vr[num_v],struct nod **link,int m){ int i=1,time=0; while(p[i]==NULL && i<=m){ i++; nu++; } if(i<=m){ visit_dfsl(num_v,p,vr,time,i,link); i++; } while(i<=m){ if(vr[i]->color=='w'){ nu++; visit_dfsl(num_v,p,vr,time,i,link); } i++; } return nu; } int main(){ int n,m,i=1; scanf("%d %d",&n,&m); struct nod *p[n+m+1],*vr[n+m+1]; init(n+m+1, p, vr); while(i<=n){ int a,j=0; scanf("%d",&a); while(j<a){ int h; scanf("%d",&h); h+=n; create_adj(n+m+1, p, i, h, 0); create_adj(n+m+1, p, h, i, 0); j++; } i++; } printf("%d\n",dfsl(n+m+1, p, vr, &vr[1],n)); return 0; }
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
C
e2836276aee2459979b232e5b29e6d57
89b225dc617ac66cf9a2a841c2b72ff4
GNU C
standard output
256 megabytes
train_002.jsonl
[ "dsu", "dfs and similar" ]
1362065400
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
PASSED
1,400
standard input
2 seconds
The first line contains two integers n and m (2 ≤ n, m ≤ 100) — the number of employees and the number of languages. Then n lines follow — each employee's language list. At the beginning of the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
["0", "2", "1"]
#include <stdio.h> #include <stdlib.h> typedef struct link1 { int d; struct link1 *next; }node; node *a[100005],*b[100005],*c[100005]; int n,m,s,d; int top=-1; //int front,rear; int visited[100005]; int stack[100005]; void print() { int i; for(i=1;i<=n;i++) { node *v=a[i]; while(v!=NULL) { printf("%d ",v->d); v=v->next; } printf("\n"); } } int k,maxdepth; int flag1=0; int flag2=0; void dfs1(node *v,int depth) { visited[v->d]=1; int r=v->d; if(v->next==NULL) return; else { v=v->next; while(v!=NULL) { if(visited[v->d]==0) dfs1(b[v->d],depth+1); v=v->next; } } } void dfs(node *v,int depth) { // flag1=0; visited[v->d]=1; int r=v->d; v=v->next; //printf("%d ",v->d); while(v!=NULL) { if(visited[v->d]==0) { visited[v->d]=1; dfs(a[v->d],depth+1); } v=v->next; // dfs(v->next->next,depth+1); } stack[++top]=r; } int main() { int i,x,y,j; scanf("%d %d",&n,&m); // a=(node *)malloc(n*sizeof(node)); for(i=1;i<=n;i++) { a[i]=(node *)malloc(sizeof(node)); a[i]->d=(i); a[i]->next=NULL; b[i]=(node *)malloc(sizeof(node)); b[i]->d=(i); b[i]->next=NULL; visited[i]=0; } for(i=1;i<=m;i++) { c[i]=(node *)malloc(sizeof(node)); c[i]->d=(i); c[i]->next=NULL; } int s=0; for(i=1;i<=n;i++) { scanf("%d",&k); if(k==0) s++; for(j=1;j<=k;j++) { scanf("%d",&x); node *p=(node *)malloc(sizeof(node)); p->d=i; p->next=c[x]->next; c[x]->next=p; } // print(); } if(s==n) { printf("%d\n",s); return 0; } for(i=1;i<=m;i++) { node *v=c[i]->next; while(v!=NULL) { int r=v->d; node *u=c[i]->next; while(u!=NULL) { if(u->d!=v->d) { node *p=(node *)malloc(sizeof(node)); p->d=u->d; p->next=a[r]->next; a[r]->next=p; } u=u->next; } v=v->next; } } // print(); int count=0; for(i=1;i<=n;i++) { if(visited[i]==0) { count++; dfs(a[i],0); } } printf("%d\n",count-1); // print(); /* for(i=1;i<=m;i++) { if(!visited[i]) dfs(a[i],0); } for(i=0;i<=top;i++) printf("%d ",stack[i]); for(i=1;i<=m;i++) visited[i]=0; int c=0; while(top!=-1) { if(visited[stack[top]]==0) { dfs1(b[stack[top]],0); c++; } top--; } printf("\n%d\n",c);*/ return 0; }
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
C
e2836276aee2459979b232e5b29e6d57
9c1da095ff7529b3158bc93d46a25caa
GNU C
standard output
256 megabytes
train_002.jsonl
[ "dsu", "dfs and similar" ]
1362065400
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
PASSED
1,400
standard input
2 seconds
The first line contains two integers n and m (2 ≤ n, m ≤ 100) — the number of employees and the number of languages. Then n lines follow — each employee's language list. At the beginning of the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
["0", "2", "1"]
#include <stdio.h> #define N 110 int n, m; int e[N][N], l[N][N], ln[N], v[N]; void dfs(int x) { int i; v[x] = 1; for (i = 0; i < m; ++i) { if (e[x][i] && !v[i]) { dfs(i); } } } int main(void) { int i, j, k; int ans = 0, non = 0; scanf("%d%d", &n, &m); for (i = 0; i < n; ++i) { scanf("%d", &ln[i]); for (j = 0; j < ln[i]; ++j) { scanf("%d", &l[i][j]); --l[i][j]; } for (j = 0; j < ln[i]; ++j) { for (k = j + 1; k < ln[i]; ++k) { e[l[i][j]][l[i][k]] = 1; e[l[i][k]][l[i][j]] = 1; } } if (ln[i] == 0) ++non; } for (i = 0; i < n; ++i) { for (j = 0; j < ln[i]; ++j) { if (!v[l[i][j]]) { dfs(l[i][j]); ++ans; } } } if (ans > 0) --ans; printf("%d\n", non + ans); return 0; }
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
C
e2836276aee2459979b232e5b29e6d57
85e5574fff990edfe5da312c78814f44
GNU C
standard output
256 megabytes
train_002.jsonl
[ "dsu", "dfs and similar" ]
1362065400
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
PASSED
1,400
standard input
2 seconds
The first line contains two integers n and m (2 ≤ n, m ≤ 100) — the number of employees and the number of languages. Then n lines follow — each employee's language list. At the beginning of the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
["0", "2", "1"]
#include <stdio.h> #define SIZE 100 int link (int lang[][SIZE], int m, int a, int b) { int i; for (i = 0; i < m && (lang[a][i] != 1 || lang[b][i] != 1); i++) ; return i < m; } void dfs (int v, int n, int m, int lang[][SIZE], int clr[SIZE]) { int i = 0; clr[v] = 1; for (i = 0; i < n; i++) if (clr[i] == 0 && link (lang, m, v, i)) dfs (i, n, m, lang, clr); } int main() { int n, m, i, j, lang[SIZE][SIZE] = { 0 }, clr[SIZE] = { 0 }, cnt = 0, zero = 0; scanf ("%d %d", &n, &m); for (i = 0; i < n; i++) { int k; scanf ("%d", &k); zero += k == 0; for (j = 0; j < k; j++) { int t; scanf ("%d", &t); lang[i][t - 1] = 1; } } if (zero == n) { printf ("%d", n); return 0; } for (i = 0; i < n; i++) if (clr[i] == 0) { cnt++; dfs (i, n, m, lang, clr); } printf ("%d", cnt - 1); return 0; }
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
C
e2836276aee2459979b232e5b29e6d57
afd0f825d3250261f19c06db8747bdb7
GNU C
standard output
256 megabytes
train_002.jsonl
[ "dsu", "dfs and similar" ]
1362065400
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
PASSED
1,400
standard input
2 seconds
The first line contains two integers n and m (2 ≤ n, m ≤ 100) — the number of employees and the number of languages. Then n lines follow — each employee's language list. At the beginning of the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
["0", "2", "1"]
#include <stdio.h> #include <stdlib.h> int t[101][101]; typedef struct { int to , nextid; }lang; lang LE[1001000]; int L[101]; int visited[101]; void dfs (int a) { visited[a]=1; int i; for(i=L[a];i!=-1;i=LE[i].nextid) { if(!visited[LE[i].to]){ ;dfs(LE[i].to);} } } int main() { int n,m,i,a,j,b,k,l=1,c=0; for (i=1;i<=100;i++) L[i]=-1; scanf("%d%d",&n,&m); for (i=1;i<=n;i++) { scanf("%d",&a); if(a==0)c++; for(j=0;j<a;j++) { scanf("%d",&b); t[i][b]=1; for(k=1;k<i;k++) { if(t[k][b]==1) { LE[l].nextid=L[i]; L[i]=l; LE[l].to=k; l++; LE[l].nextid=L[k]; L[k]=l; LE[l].to=i; l++; } } } } int ans=-1; if (c==n) {printf("%d",n); return 0;} for(i=1;i<=n;i++) { if(!visited[i]) {ans++; dfs(i);} } printf("%d",ans); return 0; }
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
C
e2836276aee2459979b232e5b29e6d57
63b64f82844e6776c77c8d5bd6dc3234
GNU C
standard output
256 megabytes
train_002.jsonl
[ "dsu", "dfs and similar" ]
1362065400
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
PASSED
1,400
standard input
2 seconds
The first line contains two integers n and m (2 ≤ n, m ≤ 100) — the number of employees and the number of languages. Then n lines follow — each employee's language list. At the beginning of the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
["0", "2", "1"]
#include <stdio.h> #include <stdlib.h> int t[101][101]; typedef struct { int to , nextid; }lang; lang LE[1001000]; int L[101]; int visited[101]; void dfs (int a) { visited[a]=1; int i; for(i=L[a];i!=-1;i=LE[i].nextid) { if(!visited[LE[i].to]){ ;dfs(LE[i].to);} } } int main() { int n,m,i,a,j,b,k,l=1,c=0; for (i=1;i<=100;i++) L[i]=-1; scanf("%d%d",&n,&m); for (i=1;i<=n;i++) { scanf("%d",&a); if(a==0)c++; for(j=0;j<a;j++) { scanf("%d",&b); t[i][b]=1; for(k=1;k<i;k++) { if(t[k][b]==1) { LE[l].nextid=L[i]; L[i]=l; LE[l].to=k; l++; LE[l].nextid=L[k]; L[k]=l; LE[l].to=i; l++; } } } } int ans=-1; if (c==n) {printf("%d",n); return 0;} for(i=1;i<=n;i++) { if(!visited[i]) {ans++; dfs(i);} } printf("%d",ans); return 0; }
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
C
e2836276aee2459979b232e5b29e6d57
6a0cf4bcdd82e679dc32782547b3665c
GNU C
standard output
256 megabytes
train_002.jsonl
[ "dsu", "dfs and similar" ]
1362065400
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
PASSED
1,400
standard input
2 seconds
The first line contains two integers n and m (2 ≤ n, m ≤ 100) — the number of employees and the number of languages. Then n lines follow — each employee's language list. At the beginning of the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
["0", "2", "1"]
#include<stdio.h> int v[110]; int e[110][110]; int l[110][110]; int visit(int i); int main(){ int i,j,n,m,r,o; scanf("%d %d",&n,&m); for(i=1,o=0;i<=n;i++){ scanf("%d",e[i]); o|=e[i][0]; for(j=1;j<=e[i][0];j++){ scanf("%d",e[i]+j); l[e[i][j]][++l[e[i][j]][0]]=i; } } for(i=1,r=0;i<=n;i++){ r+=visit(i); } printf("%d",r-(o!=0)); } int visit(int i){ int j,k; if(!v[i]){ v[i]=1; for(j=1;j<=e[i][0];j++){ for(k=1;k<=l[e[i][j]][0];k++){ visit(l[e[i][j]][k]); } } return 1; }else{ return 0; } }
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
C
e2836276aee2459979b232e5b29e6d57
b9a5e30f1480713f02f50ad08e3a027b
GNU C
standard output
256 megabytes
train_002.jsonl
[ "dsu", "dfs and similar" ]
1362065400
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
PASSED
1,400
standard input
2 seconds
The first line contains two integers n and m (2 ≤ n, m ≤ 100) — the number of employees and the number of languages. Then n lines follow — each employee's language list. At the beginning of the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
["0", "2", "1"]
#include<stdio.h> int n,m; int v[110]; int k[110][110]; int visit(int i); int main(){ int i,j,l,p,b; scanf("%d %d",&n,&m); b=p=0; for(i=1;i<=n;i++){ scanf("%d",&l); b|=l; while(l--){ scanf("%d",&j); k[i][j]=1; } } for(i=1;i<=n;i++){ p+=visit(i); } printf("%d",p-(b!=0)); } int visit(int i){ int j,l; if(v[i]){ return 0; }else{ v[i]=1; for(j=1;j<=m;j++){ if(k[i][j]){ for(l=1;l<=n;l++){ if(k[l][j]){ visit(l); } } } } return 1; } }
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
C
e2836276aee2459979b232e5b29e6d57
e6ae1ec1a7a9a9ca31452f95491815af
GNU C
standard output
256 megabytes
train_002.jsonl
[ "dsu", "dfs and similar" ]
1362065400
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
PASSED
1,400
standard input
2 seconds
The first line contains two integers n and m (2 ≤ n, m ≤ 100) — the number of employees and the number of languages. Then n lines follow — each employee's language list. At the beginning of the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
["0", "2", "1"]
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { static int ID_linguaggi[100]; static int *linguaggi_curr; int N, M; int temp,i,t; int temp2; int curr; int graph_names=0; int grafi=0; int monete=0; scanf("%d %d", &N, &M); //memset(ID_linguaggi,-1,sizeof(int)*101); int k ; for(k = 0;k < 100;k++) ID_linguaggi[k] = -1; for(; N>0; N--) { scanf("%d",&temp); if (!temp) monete++; else { i=temp; linguaggi_curr=(int*)malloc(temp*sizeof(int)); curr=-1; for(; temp>0; temp--) { scanf("%d", &linguaggi_curr[temp - 1]); linguaggi_curr[temp-1]--; if (ID_linguaggi[linguaggi_curr[temp-1]]>curr){ curr=ID_linguaggi[linguaggi_curr[temp-1]]; } } if (curr==-1) { for(; i>0; i--){ ID_linguaggi[linguaggi_curr[i-1]]=graph_names; } graph_names++; grafi++; } else { for(; i>0; i--) if(ID_linguaggi[linguaggi_curr[i-1]]==-1) ID_linguaggi[linguaggi_curr[i-1]]=curr; else { if(ID_linguaggi[linguaggi_curr[i-1]]!=curr) { temp2=ID_linguaggi[linguaggi_curr[i-1]]; grafi--; for(t=0; t<100; t++) if (ID_linguaggi[t]==temp2) ID_linguaggi[t]=curr; } } } free(linguaggi_curr); } } if (grafi) grafi--; int emp3 = grafi+monete; printf("%d",emp3); return 0; }
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
C
e2836276aee2459979b232e5b29e6d57
45aaf75a1cac747c2355596ac36697b5
GNU C
standard output
256 megabytes
train_002.jsonl
[ "dsu", "dfs and similar" ]
1362065400
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
PASSED
1,400
standard input
2 seconds
The first line contains two integers n and m (2 ≤ n, m ≤ 100) — the number of employees and the number of languages. Then n lines follow — each employee's language list. At the beginning of the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
["0", "2", "1"]
#include<stdio.h> int i,j,n,m,x,y,s,t,d,h[102],hh[102],hhh[102],u,uu,p; int a[102][102]; void parca(int i) { int j,k; h[i]=1; if(!hh[i]) s++; else return; hh[i]=1; for(j=1;j<=m;j++) { if(a[i][j] && !hhh[j]) hhh[j]=1; } for(j=1;j<=n;j++) { if(!h[j]) { d=0; for(k=1;k<=m;k++) { if(a[j][k] && hhh[k]) d=1; } if(d) parca(j); } } h[i]=0; } int main() { int j,dd=0; scanf("%d %d",&n,&m); for(i=1;i<=n;i++) { scanf("%d",&x); if(!x) t++,hh[i]=1; else dd=1; for(j=0;j<x;j++) { scanf("%d",&y); a[i][y]=1; } } for(i=1;i<=n;i++) { if(hh[i]) continue; s=0; for(j=1;j<=m;j++) hhh[j]=0; parca(i); if(s) t++; } printf("%d",t-1*(dd)); return 0; }
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
C
e2836276aee2459979b232e5b29e6d57
a8f297274a75acac54d6dcb255f69431
GNU C
standard output
256 megabytes
train_002.jsonl
[ "dsu", "dfs and similar" ]
1362065400
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
PASSED
1,400
standard input
2 seconds
The first line contains two integers n and m (2 ≤ n, m ≤ 100) — the number of employees and the number of languages. Then n lines follow — each employee's language list. At the beginning of the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
["0", "2", "1"]
/* Problem: 277A - Learning Languages */ /* Solver: Gusztav Szmolik */ #include <stdio.h> struct stack { unsigned short r; unsigned short c; }; unsigned short t[100][100]; unsigned short v[100]; struct stack st[99]; int main () { unsigned short n; unsigned short m; unsigned short i; unsigned short j; unsigned short z; unsigned short k; unsigned short a; unsigned short ai; unsigned short ic; unsigned short vc; unsigned short cc; unsigned short cr; unsigned short sp; unsigned short en; if (scanf("%hu %hu",&n,&m) != 2) return -1; if (n < 2 || n > 100 || m < 2 || m > 100) return -1; for (i = 0; i < n; i++) for (j = 0; j < m; j++) t[i][j] = 0; z = 1; for (i = 0; i < n; i++) { if (scanf("%hu",&k) != 1) return -1; if (k > m) return -1; if (z && k) z = 0; for (j = 0; j < k; j++) { if (scanf("%hu",&a) != 1) return -1; if (a < 1 || a > m) return -1; ai = a-1; if (t[i][ai]) return -1; t[i][ai] = 1; } } for (i = 0; i < n; i++) v[i] = 0; ic = vc = i = 0; while (vc < n) { if (v[i]) { i++; continue; } v[i] = 1; vc++; for (cc = 0; cc < m && !t[i][cc]; cc++); if (cc == m) { ic++; i++; continue; } cr = i; sp = en = 0; while (!en) { for (j = i+1; j < n && (!t[j][cc] || v[j]); j++); if (j == n) { cc++; while (cc < m && !t[cr][cc]) cc++; if (cc == m) { if (sp) { sp--; cr = st[sp].r; cc = st[sp].c; } else { ic++; i++; en = 1; } } } else { v[j] = 1; vc++; st[sp].r = cr; st[sp].c = cc; sp++; cr = j; for (cc = 0; !t[cr][cc]; cc++); } } } printf ("%hu\n",(z ? n : ic-1)); return 0; }
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
C
e2836276aee2459979b232e5b29e6d57
95dcc14251758aed345fe786ece6ec33
GNU C
standard output
256 megabytes
train_002.jsonl
[ "dsu", "dfs and similar" ]
1362065400
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
PASSED
1,400
standard input
2 seconds
The first line contains two integers n and m (2 ≤ n, m ≤ 100) — the number of employees and the number of languages. Then n lines follow — each employee's language list. At the beginning of the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
["0", "2", "1"]
#include <stdio.h> #define MAXV 111 int parent[MAXV],rank[MAXV]; void init(int n) { int i; for(i=0;i<n;i++) { parent[i] = i; rank[i] = 1; } } int root(int x) { if(parent[x] != x) parent[x] = root(parent[x]); return parent[x]; } void connect(int x,int y) { int rx = root(x); int ry = root(y); if(rx == ry) return ; if(rank[rx] > rank[ry]) { parent[ry] = rx; rank[rx] += rank[ry]; } if(rank[rx] <= rank[ry]) { parent[rx] = ry; rank[ry]+=rank[rx]; } } int a[110][110]; int main() { int n,m,i,j; scanf("%d %d",&n,&m); for(i=0;i<n;i++) { int k; scanf("%d",&k); for(j=0;j<k;j++) { int x; scanf("%d",&x); a[i][x-1] = 1; } } int chk = 0; for(i=0;i<n;i++) for(j=0;j<m;j++) if(a[i][j]) chk = 1; if(!chk) { printf("%d\n",n); return 0; } init(n); for(i=0;i<n;i++) for(j=0;j<n;j++) if(i<j) { int temp = 0; int k; for(k=0;k<m;k++) if(a[i][k] && a[j][k]) temp = 1; if(temp) connect(i,j); } int ans = 0; for(i=0;i<n;i++) if(root(i) == i) ans++; printf("%d\n",ans-1); return 0; }
Let's call two strings $$$s$$$ and $$$t$$$ anagrams of each other if it is possible to rearrange symbols in the string $$$s$$$ to get a string, equal to $$$t$$$.Let's consider two strings $$$s$$$ and $$$t$$$ which are anagrams of each other. We say that $$$t$$$ is a reducible anagram of $$$s$$$ if there exists an integer $$$k \ge 2$$$ and $$$2k$$$ non-empty strings $$$s_1, t_1, s_2, t_2, \dots, s_k, t_k$$$ that satisfy the following conditions: If we write the strings $$$s_1, s_2, \dots, s_k$$$ in order, the resulting string will be equal to $$$s$$$; If we write the strings $$$t_1, t_2, \dots, t_k$$$ in order, the resulting string will be equal to $$$t$$$; For all integers $$$i$$$ between $$$1$$$ and $$$k$$$ inclusive, $$$s_i$$$ and $$$t_i$$$ are anagrams of each other. If such strings don't exist, then $$$t$$$ is said to be an irreducible anagram of $$$s$$$. Note that these notions are only defined when $$$s$$$ and $$$t$$$ are anagrams of each other.For example, consider the string $$$s = $$$ "gamegame". Then the string $$$t = $$$ "megamage" is a reducible anagram of $$$s$$$, we may choose for example $$$s_1 = $$$ "game", $$$s_2 = $$$ "gam", $$$s_3 = $$$ "e" and $$$t_1 = $$$ "mega", $$$t_2 = $$$ "mag", $$$t_3 = $$$ "e": On the other hand, we can prove that $$$t = $$$ "memegaga" is an irreducible anagram of $$$s$$$.You will be given a string $$$s$$$ and $$$q$$$ queries, represented by two integers $$$1 \le l \le r \le |s|$$$ (where $$$|s|$$$ is equal to the length of the string $$$s$$$). For each query, you should find if the substring of $$$s$$$ formed by characters from the $$$l$$$-th to the $$$r$$$-th has at least one irreducible anagram.
For each query, print a single line containing "Yes" (without quotes) if the corresponding substring has at least one irreducible anagram, and a single line containing "No" (without quotes) otherwise.
C
eb5c93620709436493b2e560a63dbb02
8447d0422d3123391494a31411a1507a
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "constructive algorithms", "two pointers", "data structures", "binary search", "strings" ]
1580652300
["aaaaa\n3\n1 1\n2 4\n5 5", "aabbbbbbc\n6\n1 2\n2 4\n2 2\n1 9\n5 7\n3 5"]
NoteIn the first sample, in the first and third queries, the substring is "a", which has itself as an irreducible anagram since two or more non-empty strings cannot be put together to obtain "a". On the other hand, in the second query, the substring is "aaa", which has no irreducible anagrams: its only anagram is itself, and we may choose $$$s_1 = $$$ "a", $$$s_2 = $$$ "aa", $$$t_1 = $$$ "a", $$$t_2 = $$$ "aa" to show that it is a reducible anagram.In the second query of the second sample, the substring is "abb", which has, for example, "bba" as an irreducible anagram.
PASSED
1,800
standard input
2 seconds
The first line contains a string $$$s$$$, consisting of lowercase English characters ($$$1 \le |s| \le 2 \cdot 10^5$$$). The second line contains a single integer $$$q$$$ ($$$1 \le q \le 10^5$$$)  — the number of queries. Each of the following $$$q$$$ lines contain two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le |s|$$$), representing a query for the substring of $$$s$$$ formed by characters from the $$$l$$$-th to the $$$r$$$-th.
["Yes\nNo\nYes", "No\nYes\nYes\nYes\nNo\nNo"]
#include<stdio.h> #include<string.h> int main() { char s[200005]; scanf("%s", s); int n = strlen(s); int i, j; int count[200005][30]; for (j = 0; j < 30; j++) count[0][j] = 0; for (i = 0; i < n; i++) { for (j = 0; j < 30; j++) count[i + 1][j] = count[i][j]; count[i + 1][s[i] - 'a']++; } int q; scanf("%d", &q); int l, r; for (; q > 0; q--) { scanf("%d %d", &l, &r); if (l == r) { printf("Yes\n"); continue; } if (s[l - 1] != s[r - 1]) { printf("Yes\n"); continue; } i = 0; for (j = 0; j < 30; j++) if (count[r][j] > count[l - 1][j]) i++; if (i > 2) printf("Yes\n"); else printf("No\n"); } return 0; }
Let's define the sum of two permutations p and q of numbers 0, 1, ..., (n - 1) as permutation , where Perm(x) is the x-th lexicographically permutation of numbers 0, 1, ..., (n - 1) (counting from zero), and Ord(p) is the number of permutation p in the lexicographical order.For example, Perm(0) = (0, 1, ..., n - 2, n - 1), Perm(n! - 1) = (n - 1, n - 2, ..., 1, 0)Misha has two permutations, p and q. Your task is to find their sum.Permutation a = (a0, a1, ..., an - 1) is called to be lexicographically smaller than permutation b = (b0, b1, ..., bn - 1), if for some k following conditions hold: a0 = b0, a1 = b1, ..., ak - 1 = bk - 1, ak &lt; bk.
Print n distinct integers from 0 to n - 1, forming the sum of the given permutations. Separate the numbers by spaces.
C
ade941d5869b9a0cb18dad1e6d52218b
451eeb1561bb746e7f28e4ec0658acd7
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "data structures", "binary search", "math" ]
1421053200
["2\n0 1\n0 1", "2\n0 1\n1 0", "3\n1 2 0\n2 1 0"]
NotePermutations of numbers from 0 to 1 in the lexicographical order: (0, 1), (1, 0).In the first sample Ord(p) = 0 and Ord(q) = 0, so the answer is .In the second sample Ord(p) = 0 and Ord(q) = 1, so the answer is .Permutations of numbers from 0 to 2 in the lexicographical order: (0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0).In the third sample Ord(p) = 3 and Ord(q) = 5, so the answer is .
PASSED
2,000
standard input
2 seconds
The first line contains an integer n (1 ≤ n ≤ 200 000). The second line contains n distinct integers from 0 to n - 1, separated by a space, forming permutation p. The third line contains n distinct integers from 0 to n - 1, separated by spaces, forming permutation q.
["0 1", "1 0", "1 0 2"]
/* practice with Dukkha */ #include <stdio.h> #define N 200000 #define N_ (1 << 18) /* N_ = pow2(ceil(log2(N))) */ int tr[N_ + N_], n_; void build() { int i; for (i = n_; i < n_ + n_; i++) tr[i] = 1; for (i = n_ - 1; i >= 0; i--) tr[i] = tr[i << 1] + tr[i << 1 | 1]; } int query(int l, int r) { int x = 0; for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1) { if ((l & 1) == 1) x += tr[l++]; if ((r & 1) == 0) x += tr[r--]; } return x; } int query_(int a) { int i = 1, p = 0; while (i < n_) { i <<= 1; if (p + tr[i] <= a) { p += tr[i]; i |= 1; } } return i - n_; } void update(int i) { tr[i += n_]--; while (i > 1) { i >>= 1; tr[i] = tr[i << 1] + tr[i << 1 | 1]; } } int main() { static int aa[N]; int n, i, carry; scanf("%d", &n); n_ = 1; while (n_ < n) n_ <<= 1; build(); for (i = 0; i < n; i++) { int p; scanf("%d", &p); aa[i] = p == 0 ? 0 : query(0, p - 1); update(p); } build(); for (i = 0; i < n; i++) { int q; scanf("%d", &q); aa[i] += q == 0 ? 0 : query(0, q - 1); update(q); } for (i = n - 1, carry = 0; i >= 0; i--) { int a = aa[i] += carry; aa[i] = a % (n - i); carry = a / (n - i); } build(); for (i = 0; i < n; i++) { int a; update(a = query_(aa[i])); printf("%d ", a); } printf("\n"); return 0; }
Simon has a prime number x and an array of non-negative integers a1, a2, ..., an.Simon loves fractions very much. Today he wrote out number on a piece of paper. After Simon led all fractions to a common denominator and summed them up, he got a fraction: , where number t equals xa1 + a2 + ... + an. Now Simon wants to reduce the resulting fraction. Help him, find the greatest common divisor of numbers s and t. As GCD can be rather large, print it as a remainder after dividing it by number 1000000007 (109 + 7).
Print a single number — the answer to the problem modulo 1000000007 (109 + 7).
C
4867d014809bfc1d90672b32ecf43b43
ba2f884b2cc31c29b315012446e33cc9
GNU C
standard output
256 megabytes
train_002.jsonl
[ "number theory", "math" ]
1383379200
["2 2\n2 2", "3 3\n1 2 3", "2 2\n29 29", "4 5\n0 0 0 0"]
NoteIn the first sample . Thus, the answer to the problem is 8.In the second sample, . The answer to the problem is 27, as 351 = 13·27, 729 = 27·27.In the third sample the answer to the problem is 1073741824 mod 1000000007 = 73741817.In the fourth sample . Thus, the answer to the problem is 1.
PASSED
1,900
standard input
1 second
The first line contains two positive integers n and x (1 ≤ n ≤ 105, 2 ≤ x ≤ 109) — the size of the array and the prime number. The second line contains n space-separated integers a1, a2, ..., an (0 ≤ a1 ≤ a2 ≤ ... ≤ an ≤ 109).
["8", "27", "73741817", "1"]
#include<stdio.h> #define MOD 1000000007 typedef unsigned long long llu; llu arr[100009]; llu expmod(llu a, llu x, llu n){ if(n==0)return a % MOD; else if(n%2==0)return expmod(a, x*x%MOD, n/2); else return expmod(a*x%MOD, x, n-1); } // ([x,arr,n] + k*x^a) / (x^sum) llu g(llu arr[], llu n, llu a, llu k, llu sum, llu x){ if(a>=sum && k>0) return sum; else if(n>0 && arr[n-1]==a)return g(arr, n-1, a, k+1, sum, x); else if(k%x==0)return g(arr, n, a+1, k/x, sum, x); else return a; } int main(){ unsigned nn, xx; llu i, n, x; llu sum=0; scanf("%u%u", &nn, &xx); n = (llu)nn; x = (llu)xx; for(i=0;i<n;i++){ scanf("%llu", arr+i); sum+=arr[i]; } for(i=0;i<n;i++) arr[i]=sum-arr[i]; llu exp = (llu)g(arr, n, arr[n-1], 0, sum, x); llu result = expmod(1, x, exp); printf("%u",(unsigned)result); return 0; }
Simon has a prime number x and an array of non-negative integers a1, a2, ..., an.Simon loves fractions very much. Today he wrote out number on a piece of paper. After Simon led all fractions to a common denominator and summed them up, he got a fraction: , where number t equals xa1 + a2 + ... + an. Now Simon wants to reduce the resulting fraction. Help him, find the greatest common divisor of numbers s and t. As GCD can be rather large, print it as a remainder after dividing it by number 1000000007 (109 + 7).
Print a single number — the answer to the problem modulo 1000000007 (109 + 7).
C
4867d014809bfc1d90672b32ecf43b43
3067b588ea805229f1fe2b3886424a7f
GNU C
standard output
256 megabytes
train_002.jsonl
[ "number theory", "math" ]
1383379200
["2 2\n2 2", "3 3\n1 2 3", "2 2\n29 29", "4 5\n0 0 0 0"]
NoteIn the first sample . Thus, the answer to the problem is 8.In the second sample, . The answer to the problem is 27, as 351 = 13·27, 729 = 27·27.In the third sample the answer to the problem is 1073741824 mod 1000000007 = 73741817.In the fourth sample . Thus, the answer to the problem is 1.
PASSED
1,900
standard input
1 second
The first line contains two positive integers n and x (1 ≤ n ≤ 105, 2 ≤ x ≤ 109) — the size of the array and the prime number. The second line contains n space-separated integers a1, a2, ..., an (0 ≤ a1 ≤ a2 ≤ ... ≤ an ≤ 109).
["8", "27", "73741817", "1"]
#include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> #define REP(i,a,b) for(i=a;i<b;i++) #define rep(i,n) REP(i,0,n) #define ll long long #define M 1000000007 void intSort(int d[],int s){int i=-1,j=s,k,t;if(s<=1)return;k=(d[0]+d[s-1])/2;for(;;){while(d[++i]<k);while(d[--j]>k);if(i>=j)break;t=d[i];d[i]=d[j];d[j]=t;}intSort(d,i);intSort(d+j+1,s-j-1);} ll pw(ll a,ll b, ll md){ ll r; if(!b) return 1; r = pw(a,b/2,md); r = (r*r)%md; if(b%2) r = (r*a)%md; return r; } int main(){ int n, x; static int in[1000000]; ll res, cnt, now, dif, sum; int dame; int i, j; scanf("%d%d",&n,&x); n++; in[0]=0; REP(i,1,n) scanf("%d",in+i); intSort(in,n); res = sum = 0; rep(i,n) sum += in[i]; res = sum - in[n-1]; cnt = 0; now = in[n-1]; for(i=n-1;i>=0;i--){ if(in[i]==now){ cnt++; continue; } dif = now - in[i]; dame = 0; for(;;){ if(dif==0) break; if(cnt%x){ dame=1; break; } cnt /= x; dif--; res++; } if(dame) break; cnt++; now = in[i]; } res = pw(x, res, M); printf("%d\n",(int)res); return 0; }
Ania has a large integer $$$S$$$. Its decimal representation has length $$$n$$$ and doesn't contain any leading zeroes. Ania is allowed to change at most $$$k$$$ digits of $$$S$$$. She wants to do it in such a way that $$$S$$$ still won't contain any leading zeroes and it'll be minimal possible. What integer will Ania finish with?
Output the minimal possible value of $$$S$$$ which Ania can end with. Note that the resulting integer should also have $$$n$$$ digits.
C
0515ac888937a4dda30cad5e2383164f
9c2de92c57c1f64d7c57f68e947e9362
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "implementation", "greedy" ]
1569247500
["5 3\n51528", "3 2\n102", "1 1\n1"]
NoteA number has leading zeroes if it consists of at least two digits and its first digit is $$$0$$$. For example, numbers $$$00$$$, $$$00069$$$ and $$$0101$$$ have leading zeroes, while $$$0$$$, $$$3000$$$ and $$$1010$$$ don't have leading zeroes.
PASSED
1,000
standard input
1 second
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 200\,000$$$, $$$0 \leq k \leq n$$$) — the number of digits in the decimal representation of $$$S$$$ and the maximum allowed number of changed digits. The second line contains the integer $$$S$$$. It's guaranteed that $$$S$$$ has exactly $$$n$$$ digits and doesn't contain any leading zeroes.
["10028", "100", "0"]
/****************************************************************************** Online C Compiler. Code, Compile, Run and Debug C program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ #include <stdio.h> int main() { int n,k; scanf("%d %d", &n, &k); char arr[200000]; scanf("\n"); for(int i = 0; i < n; i++){ scanf("%c", arr+i); } if(k == 0){ for(int i = 0; i < n; i++){ printf("%c", arr[i]); } return 0; } if(n == 1){ printf("0"); return 0; } for(int i = 0; i < n; i++){ if(k == 0){ break; } if(i == 0){ if(arr[0] != '1'){ k--; arr[0] = '1'; } }else{ if(arr[i] != '0'){ arr[i] = '0'; k--; } } } for(int i = 0; i < n; i++){ printf("%c", arr[i]); } return 0; }
Ania has a large integer $$$S$$$. Its decimal representation has length $$$n$$$ and doesn't contain any leading zeroes. Ania is allowed to change at most $$$k$$$ digits of $$$S$$$. She wants to do it in such a way that $$$S$$$ still won't contain any leading zeroes and it'll be minimal possible. What integer will Ania finish with?
Output the minimal possible value of $$$S$$$ which Ania can end with. Note that the resulting integer should also have $$$n$$$ digits.
C
0515ac888937a4dda30cad5e2383164f
aedcfe5937aa1ea1e50ced47e9ac2e3f
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "implementation", "greedy" ]
1569247500
["5 3\n51528", "3 2\n102", "1 1\n1"]
NoteA number has leading zeroes if it consists of at least two digits and its first digit is $$$0$$$. For example, numbers $$$00$$$, $$$00069$$$ and $$$0101$$$ have leading zeroes, while $$$0$$$, $$$3000$$$ and $$$1010$$$ don't have leading zeroes.
PASSED
1,000
standard input
1 second
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 200\,000$$$, $$$0 \leq k \leq n$$$) — the number of digits in the decimal representation of $$$S$$$ and the maximum allowed number of changed digits. The second line contains the integer $$$S$$$. It's guaranteed that $$$S$$$ has exactly $$$n$$$ digits and doesn't contain any leading zeroes.
["10028", "100", "0"]
#include<stdio.h> char str[2000001]; int main() { int i,n,k; scanf("%d%d",&n,&k); int s=k; getchar(); gets(str); if(k>0&&str[0]!='1') { str[0]='1'; k--; } for(i=1;(i<n&&k>0);i++) { if(str[i]!='0') { str[i]='0'; k--; } } if(n==1&&s>0) printf("0\n"); else printf("%s\n",str); }
Ania has a large integer $$$S$$$. Its decimal representation has length $$$n$$$ and doesn't contain any leading zeroes. Ania is allowed to change at most $$$k$$$ digits of $$$S$$$. She wants to do it in such a way that $$$S$$$ still won't contain any leading zeroes and it'll be minimal possible. What integer will Ania finish with?
Output the minimal possible value of $$$S$$$ which Ania can end with. Note that the resulting integer should also have $$$n$$$ digits.
C
0515ac888937a4dda30cad5e2383164f
46214ee69962ac1d4dce26a862624361
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "implementation", "greedy" ]
1569247500
["5 3\n51528", "3 2\n102", "1 1\n1"]
NoteA number has leading zeroes if it consists of at least two digits and its first digit is $$$0$$$. For example, numbers $$$00$$$, $$$00069$$$ and $$$0101$$$ have leading zeroes, while $$$0$$$, $$$3000$$$ and $$$1010$$$ don't have leading zeroes.
PASSED
1,000
standard input
1 second
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 200\,000$$$, $$$0 \leq k \leq n$$$) — the number of digits in the decimal representation of $$$S$$$ and the maximum allowed number of changed digits. The second line contains the integer $$$S$$$. It's guaranteed that $$$S$$$ has exactly $$$n$$$ digits and doesn't contain any leading zeroes.
["10028", "100", "0"]
#include<stdio.h> int main() { long long int n,m,i,j,ara[10000],count=0; char ch[500000]; scanf("%I64d %I64d",&n,&m); getchar(); gets(ch); if(m==0) { for(i=0;i<n;i++) { printf("%c",ch[i]); } } else if(n==1 && m!=0) { printf("0"); } else if(n>1 && m>=1) { //strcmp(ch[0],'1')==0 if(ch[0]==49) { j=0; } else { j=1; ch[0]=49; } for(i=1;i<n;i++) { // strcmp(ch[i],'0')!=0 if(j==m) { break; } if(ch[i]!=48) { ch[i]=48; j++; } if(j==m) { break; } } // printf("ddf"); for(i=0;i<n;i++) { printf("%c",ch[i]); } //printf("UU"); } }
Ania has a large integer $$$S$$$. Its decimal representation has length $$$n$$$ and doesn't contain any leading zeroes. Ania is allowed to change at most $$$k$$$ digits of $$$S$$$. She wants to do it in such a way that $$$S$$$ still won't contain any leading zeroes and it'll be minimal possible. What integer will Ania finish with?
Output the minimal possible value of $$$S$$$ which Ania can end with. Note that the resulting integer should also have $$$n$$$ digits.
C
0515ac888937a4dda30cad5e2383164f
4e798996f2771345fcf3e0a5274d1872
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "implementation", "greedy" ]
1569247500
["5 3\n51528", "3 2\n102", "1 1\n1"]
NoteA number has leading zeroes if it consists of at least two digits and its first digit is $$$0$$$. For example, numbers $$$00$$$, $$$00069$$$ and $$$0101$$$ have leading zeroes, while $$$0$$$, $$$3000$$$ and $$$1010$$$ don't have leading zeroes.
PASSED
1,000
standard input
1 second
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 200\,000$$$, $$$0 \leq k \leq n$$$) — the number of digits in the decimal representation of $$$S$$$ and the maximum allowed number of changed digits. The second line contains the integer $$$S$$$. It's guaranteed that $$$S$$$ has exactly $$$n$$$ digits and doesn't contain any leading zeroes.
["10028", "100", "0"]
#include <stdio.h> #include <math.h> #include <stdlib.h> int main() { int n, k; scanf("%d %d", &n, &k); char num[n]; scanf("%s", num); if(n==1 && k>=1) { printf("%c\n", '0'); return 0; } else { if(num[0]!='1' && k>0) { num[0]='1'; k--; } int y=1; //printf("%c\n", num[0]); while(k>0 && y<=n-1) { if(num[y]!='0') { num[y]='0'; k--; } y++; } //printf("y is %d k is %d\n", y, k); //printf("%c\n", num[0]); printf("%s", num); printf("\n"); } return 0; }
Ania has a large integer $$$S$$$. Its decimal representation has length $$$n$$$ and doesn't contain any leading zeroes. Ania is allowed to change at most $$$k$$$ digits of $$$S$$$. She wants to do it in such a way that $$$S$$$ still won't contain any leading zeroes and it'll be minimal possible. What integer will Ania finish with?
Output the minimal possible value of $$$S$$$ which Ania can end with. Note that the resulting integer should also have $$$n$$$ digits.
C
0515ac888937a4dda30cad5e2383164f
0753bdf54ccfd8f29e8ccf5c465d9a2e
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "implementation", "greedy" ]
1569247500
["5 3\n51528", "3 2\n102", "1 1\n1"]
NoteA number has leading zeroes if it consists of at least two digits and its first digit is $$$0$$$. For example, numbers $$$00$$$, $$$00069$$$ and $$$0101$$$ have leading zeroes, while $$$0$$$, $$$3000$$$ and $$$1010$$$ don't have leading zeroes.
PASSED
1,000
standard input
1 second
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 200\,000$$$, $$$0 \leq k \leq n$$$) — the number of digits in the decimal representation of $$$S$$$ and the maximum allowed number of changed digits. The second line contains the integer $$$S$$$. It's guaranteed that $$$S$$$ has exactly $$$n$$$ digits and doesn't contain any leading zeroes.
["10028", "100", "0"]
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <string.h> // Problem URL: https://codeforces.com/contest/1230/problem/B int main() { unsigned long n, k; scanf("%lu%lu", &n, &k); // Allocating extra char for negative sign. // Problem did not say one way or another // whether there could be negative values. // In this case, I will just keep the negative // sign and use the greatest value (closest to // zero). char * S = malloc((n+2) * sizeof(char)); scanf("%s", S); bool isNegative = (S[0] == '-'); if (k == 1 && n == 1) strcpy(S, "0"); else if (k != 0) { unsigned long i = ((isNegative) ? (1) : (0)); if (S[i] != '1') { S[i] = '1'; --k; } ++i; if (isNegative) { ++n; } while (k > 0 && i < n){ if (S[i] != '0') { S[i] = '0'; --k; } i++; } } printf("%s", S); free(S); return 0; }
Ania has a large integer $$$S$$$. Its decimal representation has length $$$n$$$ and doesn't contain any leading zeroes. Ania is allowed to change at most $$$k$$$ digits of $$$S$$$. She wants to do it in such a way that $$$S$$$ still won't contain any leading zeroes and it'll be minimal possible. What integer will Ania finish with?
Output the minimal possible value of $$$S$$$ which Ania can end with. Note that the resulting integer should also have $$$n$$$ digits.
C
0515ac888937a4dda30cad5e2383164f
5bdcfa7bb9681fadfed3943b4b2cfbf8
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "implementation", "greedy" ]
1569247500
["5 3\n51528", "3 2\n102", "1 1\n1"]
NoteA number has leading zeroes if it consists of at least two digits and its first digit is $$$0$$$. For example, numbers $$$00$$$, $$$00069$$$ and $$$0101$$$ have leading zeroes, while $$$0$$$, $$$3000$$$ and $$$1010$$$ don't have leading zeroes.
PASSED
1,000
standard input
1 second
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 200\,000$$$, $$$0 \leq k \leq n$$$) — the number of digits in the decimal representation of $$$S$$$ and the maximum allowed number of changed digits. The second line contains the integer $$$S$$$. It's guaranteed that $$$S$$$ has exactly $$$n$$$ digits and doesn't contain any leading zeroes.
["10028", "100", "0"]
#include <stdio.h> #include <string.h> int main() { char s[200001]; int n, k; scanf("%d %d %s", &k, &n, s); if (strlen(s)==1) { if (n==0) printf("%s", s); else printf("0\n"); } else { int c; if (s[0]=='1') c=0; else { if (n!=0) { s[0]='1'; c=1; } } for (int i=1;s[i]!='\0'&&c<n;i++) { if (s[i]!='0') { s[i]='0'; c++; } } printf("%s", s); } }
Ania has a large integer $$$S$$$. Its decimal representation has length $$$n$$$ and doesn't contain any leading zeroes. Ania is allowed to change at most $$$k$$$ digits of $$$S$$$. She wants to do it in such a way that $$$S$$$ still won't contain any leading zeroes and it'll be minimal possible. What integer will Ania finish with?
Output the minimal possible value of $$$S$$$ which Ania can end with. Note that the resulting integer should also have $$$n$$$ digits.
C
0515ac888937a4dda30cad5e2383164f
bc1306bf4b06ef63803f123aadd6197e
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "implementation", "greedy" ]
1569247500
["5 3\n51528", "3 2\n102", "1 1\n1"]
NoteA number has leading zeroes if it consists of at least two digits and its first digit is $$$0$$$. For example, numbers $$$00$$$, $$$00069$$$ and $$$0101$$$ have leading zeroes, while $$$0$$$, $$$3000$$$ and $$$1010$$$ don't have leading zeroes.
PASSED
1,000
standard input
1 second
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 200\,000$$$, $$$0 \leq k \leq n$$$) — the number of digits in the decimal representation of $$$S$$$ and the maximum allowed number of changed digits. The second line contains the integer $$$S$$$. It's guaranteed that $$$S$$$ has exactly $$$n$$$ digits and doesn't contain any leading zeroes.
["10028", "100", "0"]
#include <stdio.h> int main() { char a[200010]; int p,n,k,h,j,i; scanf("%d %d",&n,&k); scanf("%s",a); if(k!=0){ if(n>1){ if(a[0]!='1') { a[0]='1'; k--; } for(int j=1;j<n && k>0;j++) { if(a[j]!='0') { a[j]='0'; k--; } } } else { a[0]='0'; a[1]='\0'; } printf("%s",a);} else printf("%s",a); return 0; }
Ania has a large integer $$$S$$$. Its decimal representation has length $$$n$$$ and doesn't contain any leading zeroes. Ania is allowed to change at most $$$k$$$ digits of $$$S$$$. She wants to do it in such a way that $$$S$$$ still won't contain any leading zeroes and it'll be minimal possible. What integer will Ania finish with?
Output the minimal possible value of $$$S$$$ which Ania can end with. Note that the resulting integer should also have $$$n$$$ digits.
C
0515ac888937a4dda30cad5e2383164f
6bd89bee1e03b863d1c44ec204727cbc
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "implementation", "greedy" ]
1569247500
["5 3\n51528", "3 2\n102", "1 1\n1"]
NoteA number has leading zeroes if it consists of at least two digits and its first digit is $$$0$$$. For example, numbers $$$00$$$, $$$00069$$$ and $$$0101$$$ have leading zeroes, while $$$0$$$, $$$3000$$$ and $$$1010$$$ don't have leading zeroes.
PASSED
1,000
standard input
1 second
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 200\,000$$$, $$$0 \leq k \leq n$$$) — the number of digits in the decimal representation of $$$S$$$ and the maximum allowed number of changed digits. The second line contains the integer $$$S$$$. It's guaranteed that $$$S$$$ has exactly $$$n$$$ digits and doesn't contain any leading zeroes.
["10028", "100", "0"]
#include <stdio.h> int main(void) { char s[200005]; int n,j=0,k; scanf("%d %d %s",&k,&n,s); if(k==1 && n==1) s[0]='0'; else for(int i=0;i<n;i++){ for(;s[j]!='\0';j++){ if(j==0 && s[j]!='1'){ s[j]='1'; break; } else if(j!=0 && s[j]!='0'){ s[j]='0'; break; } } } printf("%s",s); return 0; }
Ania has a large integer $$$S$$$. Its decimal representation has length $$$n$$$ and doesn't contain any leading zeroes. Ania is allowed to change at most $$$k$$$ digits of $$$S$$$. She wants to do it in such a way that $$$S$$$ still won't contain any leading zeroes and it'll be minimal possible. What integer will Ania finish with?
Output the minimal possible value of $$$S$$$ which Ania can end with. Note that the resulting integer should also have $$$n$$$ digits.
C
0515ac888937a4dda30cad5e2383164f
b4dcf1dfc25443b04d4f36a953a784bd
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "implementation", "greedy" ]
1569247500
["5 3\n51528", "3 2\n102", "1 1\n1"]
NoteA number has leading zeroes if it consists of at least two digits and its first digit is $$$0$$$. For example, numbers $$$00$$$, $$$00069$$$ and $$$0101$$$ have leading zeroes, while $$$0$$$, $$$3000$$$ and $$$1010$$$ don't have leading zeroes.
PASSED
1,000
standard input
1 second
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 200\,000$$$, $$$0 \leq k \leq n$$$) — the number of digits in the decimal representation of $$$S$$$ and the maximum allowed number of changed digits. The second line contains the integer $$$S$$$. It's guaranteed that $$$S$$$ has exactly $$$n$$$ digits and doesn't contain any leading zeroes.
["10028", "100", "0"]
#include <stdio.h> char a[200001]; int n,k, i, count; int main() { scanf("%d %d", &n, &k); scanf("%s", &a); if (k == 0){ printf("%s",a); return 0; } if (a[1] != '\0') { if (a[0] != '1') { a[0] = '1'; count = 1;} else count = 0; i = 0; while (count < k && i < n-1) { i++; if ( a[i] != '0') {a[i] = '0'; count++;} // printf("\n %d %d \n", i, k); } printf("%s",a); } else printf("0"); return 0; }
Ania has a large integer $$$S$$$. Its decimal representation has length $$$n$$$ and doesn't contain any leading zeroes. Ania is allowed to change at most $$$k$$$ digits of $$$S$$$. She wants to do it in such a way that $$$S$$$ still won't contain any leading zeroes and it'll be minimal possible. What integer will Ania finish with?
Output the minimal possible value of $$$S$$$ which Ania can end with. Note that the resulting integer should also have $$$n$$$ digits.
C
0515ac888937a4dda30cad5e2383164f
6467f0c742d7493a06885d8e9e175eae
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "implementation", "greedy" ]
1569247500
["5 3\n51528", "3 2\n102", "1 1\n1"]
NoteA number has leading zeroes if it consists of at least two digits and its first digit is $$$0$$$. For example, numbers $$$00$$$, $$$00069$$$ and $$$0101$$$ have leading zeroes, while $$$0$$$, $$$3000$$$ and $$$1010$$$ don't have leading zeroes.
PASSED
1,000
standard input
1 second
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 200\,000$$$, $$$0 \leq k \leq n$$$) — the number of digits in the decimal representation of $$$S$$$ and the maximum allowed number of changed digits. The second line contains the integer $$$S$$$. It's guaranteed that $$$S$$$ has exactly $$$n$$$ digits and doesn't contain any leading zeroes.
["10028", "100", "0"]
#include<stdio.h> int main() { int n,k,i,j; char s[200000]; scanf("%d %d",&n,&k); j=k; scanf("%s",s); if((s[0]!= '1')&&(k>0)) { s[0]='1'; k--; } for(i=1;(k>0)&&(i<n);i++) { if(s[i]!= '0') { s[i]='0'; k--; } } if((n==1)&&(j>0)) { printf("%d",0); } else { printf("%s",s); } return 0; }
Ania has a large integer $$$S$$$. Its decimal representation has length $$$n$$$ and doesn't contain any leading zeroes. Ania is allowed to change at most $$$k$$$ digits of $$$S$$$. She wants to do it in such a way that $$$S$$$ still won't contain any leading zeroes and it'll be minimal possible. What integer will Ania finish with?
Output the minimal possible value of $$$S$$$ which Ania can end with. Note that the resulting integer should also have $$$n$$$ digits.
C
0515ac888937a4dda30cad5e2383164f
192e9988c6301086e98bde1dedb1dfb0
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "implementation", "greedy" ]
1569247500
["5 3\n51528", "3 2\n102", "1 1\n1"]
NoteA number has leading zeroes if it consists of at least two digits and its first digit is $$$0$$$. For example, numbers $$$00$$$, $$$00069$$$ and $$$0101$$$ have leading zeroes, while $$$0$$$, $$$3000$$$ and $$$1010$$$ don't have leading zeroes.
PASSED
1,000
standard input
1 second
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 200\,000$$$, $$$0 \leq k \leq n$$$) — the number of digits in the decimal representation of $$$S$$$ and the maximum allowed number of changed digits. The second line contains the integer $$$S$$$. It's guaranteed that $$$S$$$ has exactly $$$n$$$ digits and doesn't contain any leading zeroes.
["10028", "100", "0"]
#include <stdio.h> //#include "yusr.c" //#define TEST //take input as char // replace first with 1 and following ones with 0 int main() { char digit[2000000]; int x; int l; int c; x= scanf("%d %d",&l,&c); getchar(); #ifdef TEST SHOWi(x); #endif x= scanf("%s",digit); getchar(); #ifdef TEST SHOWi(x); puts(digit); #endif if(c==0) { puts(digit); return 0; } if(l==1) { puts("0"); return 0; } if(digit[0]!='1') { digit[0]='1'; c--; } int i; for(i=1;i<l && c>0;i++) { #ifdef TEST printf("digit is %c\n",digit[i]); #endif if(digit[i]!='0') { digit[i]='0'; c--; } } puts(digit); return 0; }
Ania has a large integer $$$S$$$. Its decimal representation has length $$$n$$$ and doesn't contain any leading zeroes. Ania is allowed to change at most $$$k$$$ digits of $$$S$$$. She wants to do it in such a way that $$$S$$$ still won't contain any leading zeroes and it'll be minimal possible. What integer will Ania finish with?
Output the minimal possible value of $$$S$$$ which Ania can end with. Note that the resulting integer should also have $$$n$$$ digits.
C
0515ac888937a4dda30cad5e2383164f
99043a64de170f0ad98e6f169ad641cc
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "implementation", "greedy" ]
1569247500
["5 3\n51528", "3 2\n102", "1 1\n1"]
NoteA number has leading zeroes if it consists of at least two digits and its first digit is $$$0$$$. For example, numbers $$$00$$$, $$$00069$$$ and $$$0101$$$ have leading zeroes, while $$$0$$$, $$$3000$$$ and $$$1010$$$ don't have leading zeroes.
PASSED
1,000
standard input
1 second
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 200\,000$$$, $$$0 \leq k \leq n$$$) — the number of digits in the decimal representation of $$$S$$$ and the maximum allowed number of changed digits. The second line contains the integer $$$S$$$. It's guaranteed that $$$S$$$ has exactly $$$n$$$ digits and doesn't contain any leading zeroes.
["10028", "100", "0"]
#include<stdio.h> int main() { int n,k,i,j; scanf("%d%d",&n,&k); char a[n+1]; scanf("%s",a); if(n==1 && k!=0) printf("0\n"); else { if(a[0]>'1' && k) { a[0]='1'; k--; } for(i=0,j=1;i<k;i++,j++) { if(a[j]=='\0') break; if(a[j]=='0') i--; else a[j]='0'; } printf("%s\n",a); } return 0; }
Ania has a large integer $$$S$$$. Its decimal representation has length $$$n$$$ and doesn't contain any leading zeroes. Ania is allowed to change at most $$$k$$$ digits of $$$S$$$. She wants to do it in such a way that $$$S$$$ still won't contain any leading zeroes and it'll be minimal possible. What integer will Ania finish with?
Output the minimal possible value of $$$S$$$ which Ania can end with. Note that the resulting integer should also have $$$n$$$ digits.
C
0515ac888937a4dda30cad5e2383164f
0c320398ccb4a73d2860b9fc187a10cb
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "implementation", "greedy" ]
1569247500
["5 3\n51528", "3 2\n102", "1 1\n1"]
NoteA number has leading zeroes if it consists of at least two digits and its first digit is $$$0$$$. For example, numbers $$$00$$$, $$$00069$$$ and $$$0101$$$ have leading zeroes, while $$$0$$$, $$$3000$$$ and $$$1010$$$ don't have leading zeroes.
PASSED
1,000
standard input
1 second
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 200\,000$$$, $$$0 \leq k \leq n$$$) — the number of digits in the decimal representation of $$$S$$$ and the maximum allowed number of changed digits. The second line contains the integer $$$S$$$. It's guaranteed that $$$S$$$ has exactly $$$n$$$ digits and doesn't contain any leading zeroes.
["10028", "100", "0"]
#include <stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> long long cmpfunc (const void * a, const void * b) { return ( *(long long*)a - *(long long*)b ); } int main(void){ long long int test,i,j,n,count,flag=0,o1=0,o2=0,b1,x,m,l,max,sum2,min,f,c,r,o,sum1,sum=0,y,b,count1=0,a1,a2,a3,a4; char a[1000000]; scanf("%lld%lld%s",&a1,&a2,&a); if(a2>=1 && a1==1){ printf("0"); return 0; } if(a[0]!='1' &&a2>=1){ a[0]='1'; a2--; } for(i=1;i<a1 && a2>0;i++){ if(a[i]!='0'){ a[i]='0'; a2--; } } printf("%s",a); return 0; }
Ania has a large integer $$$S$$$. Its decimal representation has length $$$n$$$ and doesn't contain any leading zeroes. Ania is allowed to change at most $$$k$$$ digits of $$$S$$$. She wants to do it in such a way that $$$S$$$ still won't contain any leading zeroes and it'll be minimal possible. What integer will Ania finish with?
Output the minimal possible value of $$$S$$$ which Ania can end with. Note that the resulting integer should also have $$$n$$$ digits.
C
0515ac888937a4dda30cad5e2383164f
c7be7ac1f08dfd97bd10763b2a586715
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "implementation", "greedy" ]
1569247500
["5 3\n51528", "3 2\n102", "1 1\n1"]
NoteA number has leading zeroes if it consists of at least two digits and its first digit is $$$0$$$. For example, numbers $$$00$$$, $$$00069$$$ and $$$0101$$$ have leading zeroes, while $$$0$$$, $$$3000$$$ and $$$1010$$$ don't have leading zeroes.
PASSED
1,000
standard input
1 second
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 200\,000$$$, $$$0 \leq k \leq n$$$) — the number of digits in the decimal representation of $$$S$$$ and the maximum allowed number of changed digits. The second line contains the integer $$$S$$$. It's guaranteed that $$$S$$$ has exactly $$$n$$$ digits and doesn't contain any leading zeroes.
["10028", "100", "0"]
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { int n, k; scanf ("%d %d\n", &n, &k); char str[200001]; scanf ("%s", str); int count = 0, i; if (k == 0) printf ("%s", str); else{ if (n == 1) {str[0] = '0'; printf ("%s", str);} else { if (str[0] == '1'){ for (i = 1; i < n; i++){ if (count >= k) break; if (str[i] != '0'){ str[i] = '0'; count++; } } printf ("%s", str); } else { for (i = 1; i < n; i++){ if (count >= k-1) break; if (str[i] != '0'){ str[i] = '0'; count++; } } str[0] = '1'; printf ("%s", str); } } } return 0; }
Ania has a large integer $$$S$$$. Its decimal representation has length $$$n$$$ and doesn't contain any leading zeroes. Ania is allowed to change at most $$$k$$$ digits of $$$S$$$. She wants to do it in such a way that $$$S$$$ still won't contain any leading zeroes and it'll be minimal possible. What integer will Ania finish with?
Output the minimal possible value of $$$S$$$ which Ania can end with. Note that the resulting integer should also have $$$n$$$ digits.
C
0515ac888937a4dda30cad5e2383164f
21441bce043c5a1a759eec50ef50682a
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "implementation", "greedy" ]
1569247500
["5 3\n51528", "3 2\n102", "1 1\n1"]
NoteA number has leading zeroes if it consists of at least two digits and its first digit is $$$0$$$. For example, numbers $$$00$$$, $$$00069$$$ and $$$0101$$$ have leading zeroes, while $$$0$$$, $$$3000$$$ and $$$1010$$$ don't have leading zeroes.
PASSED
1,000
standard input
1 second
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 200\,000$$$, $$$0 \leq k \leq n$$$) — the number of digits in the decimal representation of $$$S$$$ and the maximum allowed number of changed digits. The second line contains the integer $$$S$$$. It's guaranteed that $$$S$$$ has exactly $$$n$$$ digits and doesn't contain any leading zeroes.
["10028", "100", "0"]
#include<stdio.h> int main() { int n,k; scanf("%d%d",&n,&k); char s[n]; scanf("%s",s); if(k){ if(n==1){ printf("0"); return 0; } int cnt=0; for(int i=0;i<k&&cnt<n;i++,cnt++){ //printf("\n%d",cnt); if(cnt==0){ if(s[0]=='1'){ i--; continue; } else{ s[0]='1'; } } else{ if(s[cnt]=='0'){ i--; continue; } else{ s[cnt]='0'; } } } } printf("%s",s); return 0; }
Ania has a large integer $$$S$$$. Its decimal representation has length $$$n$$$ and doesn't contain any leading zeroes. Ania is allowed to change at most $$$k$$$ digits of $$$S$$$. She wants to do it in such a way that $$$S$$$ still won't contain any leading zeroes and it'll be minimal possible. What integer will Ania finish with?
Output the minimal possible value of $$$S$$$ which Ania can end with. Note that the resulting integer should also have $$$n$$$ digits.
C
0515ac888937a4dda30cad5e2383164f
b46129ff30ddc9c28a4e28c650c5fd60
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "implementation", "greedy" ]
1569247500
["5 3\n51528", "3 2\n102", "1 1\n1"]
NoteA number has leading zeroes if it consists of at least two digits and its first digit is $$$0$$$. For example, numbers $$$00$$$, $$$00069$$$ and $$$0101$$$ have leading zeroes, while $$$0$$$, $$$3000$$$ and $$$1010$$$ don't have leading zeroes.
PASSED
1,000
standard input
1 second
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 200\,000$$$, $$$0 \leq k \leq n$$$) — the number of digits in the decimal representation of $$$S$$$ and the maximum allowed number of changed digits. The second line contains the integer $$$S$$$. It's guaranteed that $$$S$$$ has exactly $$$n$$$ digits and doesn't contain any leading zeroes.
["10028", "100", "0"]
#include<stdio.h> int main() { int n,k; char s; scanf("%d %d\n",&n,&k); int p=0;//counter till n int q=0;//counter till k if((n==1)&&(k==1)) { printf("0"); } else { while(p<n) { scanf("%c",&s); if((p==0)&&(s!='1')&&(q<k)) { s='1'; q++; } if((p!=0)&&(q<k)&&(s!='0')) { s='0'; q++; } printf("%c", s); p++; } } return 0; }
You are given two strings $$$s$$$ and $$$t$$$ both of length $$$n$$$ and both consisting of lowercase Latin letters.In one move, you can choose any length $$$len$$$ from $$$1$$$ to $$$n$$$ and perform the following operation: Choose any contiguous substring of the string $$$s$$$ of length $$$len$$$ and reverse it; at the same time choose any contiguous substring of the string $$$t$$$ of length $$$len$$$ and reverse it as well. Note that during one move you reverse exactly one substring of the string $$$s$$$ and exactly one substring of the string $$$t$$$.Also note that borders of substrings you reverse in $$$s$$$ and in $$$t$$$ can be different, the only restriction is that you reverse the substrings of equal length. For example, if $$$len=3$$$ and $$$n=5$$$, you can reverse $$$s[1 \dots 3]$$$ and $$$t[3 \dots 5]$$$, $$$s[2 \dots 4]$$$ and $$$t[2 \dots 4]$$$, but not $$$s[1 \dots 3]$$$ and $$$t[1 \dots 2]$$$.Your task is to say if it is possible to make strings $$$s$$$ and $$$t$$$ equal after some (possibly, empty) sequence of moves.You have to answer $$$q$$$ independent test cases.
For each test case, print the answer on it — "YES" (without quotes) if it is possible to make strings $$$s$$$ and $$$t$$$ equal after some (possibly, empty) sequence of moves and "NO" otherwise.
C
7cf9bb97385ee3a5b5e28f66eab163d0
81af505ab896d118f1d2c17469f78522
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "constructive algorithms", "sortings", "strings" ]
1572873300
["4\n4\nabcd\nabdc\n5\nababa\nbaaba\n4\nasdf\nasdg\n4\nabcd\nbadc"]
null
PASSED
2,000
standard input
1 second
The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of test cases. Then $$$q$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$s$$$ and $$$t$$$. The second line of the test case contains one string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. The third line of the test case contains one string $$$t$$$ consisting of $$$n$$$ lowercase Latin letters. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).
["NO\nYES\nNO\nYES"]
#include <stdio.h> #define MAX 200010 int main() { int q,n; int timeOfExchange=0; scanf("%d",&q); char str1[MAX]; char str2[MAX]; int check[26] = {0}; int check2[26] = { 0 }; char temp; int skip = 0; int LocalPos; int todo = 0; for(int i = 0; i < q; i++) { for (int j = 0; j < 26; j++) { check[j] = 0; check2[j] = 0; } timeOfExchange = 0; skip = 0; //init scanf("%d",&n); scanf("%s", str1); scanf("%s", str2); for (int ii = 0; ii < n; ii++) { check[str1[ii] - 'a'] ++; check[str2[ii] - 'a'] --; check2[str2[ii] - 'a'] ++; } for (int j = 0; j < 26; j++) { if (check[j] != 0) { printf("NO\n"); skip = 1; break;//NO } } //pre if (skip) continue; for (int i = 0; i < 26; i++) { if (check2[i] >= 2) { skip = 1; break; } } if (skip) { printf("YES\n"); continue; } for (int i = 0; i < n; i++) { if (str1[i] != str2[i]) { for (int j = 0; j < n; j++) { if (str1[i] == str2[j] &&(j>i) ) { LocalPos = j; break; } } //printf("%d : %d\n",i, LocalPos); for (int z = LocalPos; z > i; z--) { timeOfExchange++; temp = str2[z]; str2[z] = str2[z - 1]; str2[z - 1] = temp; } } } if ((timeOfExchange%2)==0) printf("YES\n"); else printf("NO\n"); } return 0; }
Today you are to solve the problem even the famous Hercule Poirot can't cope with! That's why this crime has not yet been solved and this story was never included in Agatha Christie's detective story books. You are not informed on what crime was committed, when and where the corpse was found and other details. We only know that the crime was committed in a house that has n rooms and m doors between the pairs of rooms. The house residents are very suspicious, that's why all the doors can be locked with keys and all the keys are different. According to the provided evidence on Thursday night all the doors in the house were locked, and it is known in what rooms were the residents, and what kind of keys had any one of them. The same is known for the Friday night, when all the doors were also locked. On Friday it was raining heavily, that's why nobody left the house and nobody entered it. During the day the house residents could open and close doors to the neighboring rooms using the keys at their disposal (every door can be opened and closed from each side); move freely from a room to a room if a corresponding door is open; give keys to one another, being in one room. "Little grey matter" of Hercule Poirot are not capable of coping with such amount of information. Find out if the positions of people and keys on the Thursday night could result in the positions on Friday night, otherwise somebody among the witnesses is surely lying.
Print "YES" (without quotes) if the second arrangement can result from the first one, otherwise, print "NO".
C
52b13cca189853e6af02bea8d3d85276
d2a3afb944c04b9fc3b19ce11ecd7553
GNU C
standard output
256 megabytes
train_002.jsonl
[ "dsu", "graphs" ]
1291536000
["2 1 2\n1 2\nDmitry 1 1 1\nNatalia 2 0\nNatalia 1 1 1\nDmitry 2 0", "4 4 3\n1 3\n1 2\n2 3\n3 4\nArtem 1 1 4\nDmitry 1 1 2\nEdvard 4 2 1 3\nArtem 2 0\nDmitry 1 0\nEdvard 4 4 1 2 3 4"]
null
PASSED
2,300
standard input
2 seconds
The first line contains three preset integers n, m и k (1 ≤ n, m, k ≤ 1000) — the number of rooms, the number of doors and the number of house residents respectively. The next m lines contain pairs of room numbers which join the doors. The rooms are numbered with integers from 1 to n. There cannot be more that one door between the pair of rooms. No door connects a room with itself. The next k lines describe the residents' position on the first night. Every line contains a resident's name (a non-empty line consisting of no more than 10 Latin letters), then after a space follows the room number, then, after a space — the number of keys the resident has. Then follow written space-separated numbers of the doors that can be unlocked by these keys. The doors are numbered with integers from 1 to m in the order in which they are described in the input data. All the residents have different names, uppercase and lowercase letters considered to be different. Every m keys occurs exactly once in the description. Multiple people may be present in one room, some rooms may be empty. The next k lines describe the position of the residents on the second night in the very same format. It is guaranteed that in the second night's description the residents' names remain the same and every m keys occurs exactly once.
["YES", "NO"]
#include<math.h> #include<time.h> #include<stdio.h> #include<string.h> #include<stdlib.h> #define oo 1000000000 #define pi 3.14159265359 #define zero(a) (abb(a)<=1e-7) #define lowbit(a) ((a)&(-(a))) #define min(a,b) ((a)<(b)?(a):(b)) #define max(a,b) ((a)>(b)?(a):(b)) #define abb(a) ((a)>0?(a):(-(a))) #define cj(x1,y1,x2,y2) ((x1)*(y2)-(x2)*(y2)) #define dj(x1,y1,x2,y2) ((x1)*(y1)+(y1)*(y2)) #define dis(x1,y1,x2,y2) sqrt(((x2)-(x1))*((x2)-(x1))+((y2)-(y1))*((y2)-(y1))) long min[1001]={0},rec[1001]={0},ed[1001][2]={0},post[1001]={0},f[1001]={0},stay[1001][1001]={0},ps[1001]={0},head[1001]={0},e[2001]={0},next[2001]={0},v[2001]={0}; char str[15]={0},th[1001][1001]={0},town[1001][1001]={0},hash[1001][1001]={0},own[1001][1001]={0},name[1001][15]={0}; long n,m,totm=0,flag=0; void add(long a,long b,long c) { e[++totm]=b; next[totm]=head[a]; head[a]=totm; v[totm]=c; } long getf(long now) { if (f[now]==now) return now; return f[now]=getf(f[now]); } void dfs(long now,long x) { long i,j,fa,tmp; hash[x][now]=1; stay[now][++ps[now]]=x; tmp=++flag; for (i=1;i<ps[now];i++) if (x!=(fa=getf(stay[now][i]))) { f[fa]=x; for (j=1;j<=m;j++) if (own[fa][j]&&!own[x][j]) { own[x][j]=1; rec[j]=flag; } for (j=1;j<=n;j++) hash[x][j]|=hash[fa][j]; } for (i=1;i<=m;i++) if (rec[i]==tmp) if (hash[x][ed[i][0]]&&!hash[x][ed[i][1]]) dfs(ed[i][1],x); else if (hash[x][ed[i][1]]&&!hash[x][ed[i][0]]) dfs(ed[i][0],x); for (i=head[now];i;i=next[i]) if (!hash[x][e[i]]&&own[x][v[i]]) dfs(e[i],x); } int main() { long i,j,k,a,b,pos,num,x,ans=1; scanf("%ld%ld%ld",&n,&m,&k); for (i=1;i<=m;i++) { scanf("%ld%ld",&a,&b); ed[i][0]=a; ed[i][1]=b; add(a,b,i); add(b,a,i); } for (i=1;i<=k;i++) { f[i]=i; scanf("%s%ld%ld",name[i]+1,&post[i],&num); stay[post[i]][++ps[post[i]]]=i; while (num--) { scanf("%ld",&x); own[i][x]=1; } } for (i=1;i<=k;i++) if (f[i]==i) dfs(post[i],i); for (i=1;i<=k;i++) for (j=1;j<=m;j++) town[i][j]=own[getf(i)][j]; for (i=1;i<=k;i++) for (j=1;j<=n;j++) th[i][j]=hash[f[i]][j]; memset(ps,0,sizeof(ps)); memset(own,0,sizeof(own)); memset(hash,0,sizeof(hash)); for (i=1;i<=k;i++) { f[i]=i; scanf("%s%ld%ld",str+1,&pos,&num); for (j=1;strcmp(name[j]+1,str+1);j++); post[j]=pos; stay[post[j]][++ps[post[j]]]=j; while (num--) { scanf("%ld",&x); own[j][x]=1; } } for (i=1;i<=k;i++) if (f[i]==i) dfs(post[i],i); for (i=1;i<=k;i++) for (j=1;j<=m;j++) if (town[i][j]!=own[getf(i)][j]) ans=0; for (i=1;i<=k;i++) for (j=1;j<=n;j++) if (th[i][j]!=hash[f[i]][j]) ans=0; puts(ans?"YES":"NO"); return 0; } /* 2 1 2 1 2 Dmitry 1 1 1 Natalia 2 0 Natalia 1 1 1 Dmitry 2 0 3 3 3 1 2 2 3 3 1 a 1 1 1 b 2 1 3 c 3 1 2 b 1 1 2 c 2 1 3 a 3 1 1 4 5 3 1 2 2 3 2 4 1 3 1 3 a 1 2 4 3 b 1 0 c 4 3 1 2 5 a 1 2 4 3 b 1 1 5 c 4 2 1 2 */
The Bitlandians are quite weird people. They have their own problems and their own solutions. They have their own thoughts and their own beliefs, they have their own values and their own merits. They have their own dishes and their own sausages!In Bitland a sausage is an array of integers! A sausage's deliciousness is equal to the bitwise excluding OR (the xor operation) of all integers in that sausage. One day, when Mr. Bitkoch (the local cook) was going to close his BitRestaurant, BitHaval and BitAryo, the most famous citizens of Bitland, entered the restaurant and each ordered a sausage.But Mr. Bitkoch had only one sausage left. So he decided to cut a prefix (several, may be zero, first array elements) of the sausage and give it to BitHaval and a postfix (several, may be zero, last array elements) of the sausage and give it to BitAryo. Note that one or both pieces of the sausage can be empty. Of course, the cut pieces mustn't intersect (no array element can occur in both pieces).The pleasure of BitHaval and BitAryo is equal to the bitwise XOR of their sausages' deliciousness. An empty sausage's deliciousness equals zero.Find a way to cut a piece of sausage for BitHaval and BitAryo that maximizes the pleasure of these worthy citizens.
Print a single integer — the maximum pleasure BitHaval and BitAryo can get from the dinner.
C
02588d1e94595cb406d92bb6e170ded6
f36f88016c84947847e9001bb05386ca
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "data structures", "bitmasks", "trees" ]
1363188600
["2\n1 2", "3\n1 2 3", "2\n1000 1000"]
null
PASSED
2,200
standard input
2 seconds
The first line contains an integer n (1 ≤ n ≤ 105). The next line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 1012) — Mr. Bitkoch's sausage. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
["3", "3", "1000"]
/* practice with Dukkha */ #include <stdio.h> #define N 100000 long long max(long long a, long long b) { return a > b ? a : b; } long long pp[N + 1], qq[N + 1]; int partition(long long *aa, int i, int j, int b) { while (1) { long long tmp; while (i <= j && (aa[i] & 1LL << b) == 0) i++; while (i <= j && (aa[j] & 1LL << b) != 0) j--; if (i < j) tmp = aa[i], aa[i] = aa[j], aa[j] = tmp; else return i; } } long long solve(int lp, int rp, int lq, int rq, int b) { int mp, mq; long long a; if (lp == rp || lq == rq) return -1; if (b == -1) return pp[lp] ^ qq[lq]; mp = partition(pp, lp, rp - 1, b); mq = partition(qq, lq, rq - 1, b); b--; a = max(solve(lp, mp, mq, rq, b), solve(mp, rp, lq, mq, b)); if (a == -1) a = max(solve(lp, mp, lq, mq, b), solve(mp, rp, mq, rq, b)); return a; } int main() { static long long aa[N]; int n, i; long long x; scanf("%d", &n); for (i = 0; i < n; i++) scanf("%lld", &aa[i]); x = 0; for (i = 0; i < n; i++) pp[i] = x ^= aa[i]; x = 0; for (i = n - 1; i >= 0; i--) qq[i] = x ^= aa[i]; printf("%lld\n", solve(0, n + 1, 0, n + 1, 40)); return 0; }
You are given an n × m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the tableabcdedfghijk we obtain the table:acdefghjk A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good.
Print a single number — the minimum number of columns that you need to remove in order to make the table good.
C
a45cfc2855f82f162133930d9834a9f0
f9a2ab31a76c936fc6fe362d769a2c0e
GNU C
standard output
256 megabytes
train_002.jsonl
[ "greedy" ]
1418833800
["1 10\ncodeforces", "4 4\ncase\ncare\ntest\ncode", "5 4\ncode\nforc\nesco\ndefo\nrces"]
NoteIn the first sample the table is already good.In the second sample you may remove the first and third column.In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t.
PASSED
1,500
standard input
2 seconds
The first line contains two integers  — n and m (1 ≤ n, m ≤ 100). Next n lines contain m small English letters each — the characters of the table.
["0", "2", "4"]
#include <stdio.h> #include <stdlib.h> #define INIT_CAP 10 typedef struct { int u, d; } Range; typedef struct { Range *ary; int cap, size; } Vector; Vector vec[101]; void initVec(Vector *p) { p->ary = malloc(sizeof(Range) * INIT_CAP); p->cap = INIT_CAP; p->size = 0; } void initVecAry(int n) { int i; for (i = 0; i < n; i++) initVec(vec + i); } void add(Vector *p, Range x) { if (p->size == p->cap) { p->cap <<= 1; p->ary = realloc(p->ary, sizeof(Range) * p->cap); } p->ary[p->size++] = x; } char table[100][101]; int main(void) { int i, j, k; int n, m; int now, ans; scanf("%d %d", &n, &m); for (i = 0; i < n; i++) scanf("%s", table[i]); initVecAry(m + 1); ans = now = 0; add(vec, (Range){0, n - 1}); for (j = 0; j < m; j++) { for (k = 0; k < vec[now].size; k++) { int u; u = vec[now].ary[k].u; for (i = vec[now].ary[k].u + 1; i <= vec[now].ary[k].d; i++) if (table[i][j] > table[i - 1][j]) { add(vec + j + 1, (Range){u, i - 1}); u = i; } else if (table[i][j] < table[i - 1][j]) break; if (i <= vec[now].ary[k].d) { ans++; break; } else add(vec + j + 1, (Range){u, i - 1}); } if (k == vec[now].size) now = j + 1; } printf("%d\n", ans); return 0; }
You are given an n × m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the tableabcdedfghijk we obtain the table:acdefghjk A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good.
Print a single number — the minimum number of columns that you need to remove in order to make the table good.
C
a45cfc2855f82f162133930d9834a9f0
256fe1eb2587f1ac58973bc6877ad875
GNU C
standard output
256 megabytes
train_002.jsonl
[ "greedy" ]
1418833800
["1 10\ncodeforces", "4 4\ncase\ncare\ntest\ncode", "5 4\ncode\nforc\nesco\ndefo\nrces"]
NoteIn the first sample the table is already good.In the second sample you may remove the first and third column.In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t.
PASSED
1,500
standard input
2 seconds
The first line contains two integers  — n and m (1 ≤ n, m ≤ 100). Next n lines contain m small English letters each — the characters of the table.
["0", "2", "4"]
#include<stdio.h> int main(void){ int N; int M; scanf("%i %i", &N, &M); char Pole[N][M]; int i; int j; char c; for(i=0;i<N;i++){ scanf("%c", &c); for(j=0;j<M;j++){ scanf("%c", &Pole[i][j]); } } /*for(i=0;i<N;i++){ for(j=0;j<M;j++){ printf("%c", Pole[i][j]); } printf("\n"); }*/ int res=0; int stav[N]; for(i=0;i<N;i++){ stav[i]=0; } int ano=0; for(j=0;j<M;j++){ ano=1; for(i=0;i<N-1;i++){ if(Pole[i+1][j]<Pole[i][j]&&stav[i+1]==stav[i]){ res++; ano=0; break; } } if(ano==1){ stav[0]=1; for(i=0;i<N-1;i++){ if(stav[i+1]!=i+2){ if(Pole[i+1][j]>Pole[i][j]){ stav[i+1]=i+2; } if(Pole[i+1][j]==Pole[i][j]){ stav[i+1]=stav[i]; } } }} /*for(i=0;i<N;i++){ printf("%i ", stav[i]); } printf("\n");*/ } printf("%i\n", res); return 0; }
You are given an n × m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the tableabcdedfghijk we obtain the table:acdefghjk A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good.
Print a single number — the minimum number of columns that you need to remove in order to make the table good.
C
a45cfc2855f82f162133930d9834a9f0
f64fc35f5d102a019a0f221b13c4445f
GNU C
standard output
256 megabytes
train_002.jsonl
[ "greedy" ]
1418833800
["1 10\ncodeforces", "4 4\ncase\ncare\ntest\ncode", "5 4\ncode\nforc\nesco\ndefo\nrces"]
NoteIn the first sample the table is already good.In the second sample you may remove the first and third column.In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t.
PASSED
1,500
standard input
2 seconds
The first line contains two integers  — n and m (1 ≤ n, m ≤ 100). Next n lines contain m small English letters each — the characters of the table.
["0", "2", "4"]
#include<stdio.h> #define MAXN 1111 #define MAXM MAXN int N,M; char word[MAXN][MAXM]; char sep[MAXN]; int main() { int i,j,k; int result; scanf("%d %d\n", &N, &M); for(i=0; i<N; i++) scanf("%s\n", &word[i][0]); for(i=0; i<N; i++) sep[i] = 0; result = 0; for(i=0; i<M; i++){ for(j=0; j<N-1; j++){ if(sep[j]) continue; if(word[j][i] > word[j+1][i]) break; } if(j < N-1){ //printf("delete %d\n", i); result++; } else{ for(j=0; j<N-1; j++) if(word[j][i] < word[j+1][i]) sep[j] = 1; } } printf("%d\n", result); return 0; }
Nikolay got a string $$$s$$$ of even length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'. Its positions are numbered from $$$1$$$ to $$$n$$$.He wants to modify his string so that every its prefix of even length has an equal amount of letters 'a' and 'b'. To achieve that, Nikolay can perform the following operation arbitrary number of times (possibly, zero): choose some position in his string and replace the letter on this position with the other letter (i.e. replace 'a' with 'b' or replace 'b' with 'a'). Nikolay can use no letters except 'a' and 'b'.The prefix of string $$$s$$$ of length $$$l$$$ ($$$1 \le l \le n$$$) is a string $$$s[1..l]$$$.For example, for the string $$$s=$$$"abba" there are two prefixes of the even length. The first is $$$s[1\dots2]=$$$"ab" and the second $$$s[1\dots4]=$$$"abba". Both of them have the same number of 'a' and 'b'.Your task is to calculate the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'.
In the first line print the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'. In the second line print the string Nikolay obtains after applying all the operations. If there are multiple answers, you can print any of them.
C
8ad06ac90b258a8233e2a1cf51f68078
859982d1822078f423fb9e820a4ae94a
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "strings" ]
1569049500
["4\nbbbb", "6\nababab", "2\naa"]
NoteIn the first example Nikolay has to perform two operations. For example, he can replace the first 'b' with 'a' and the last 'b' with 'a'. In the second example Nikolay doesn't need to do anything because each prefix of an even length of the initial string already contains an equal amount of letters 'a' and 'b'.
PASSED
800
standard input
1 second
The first line of the input contains one even integer $$$n$$$ $$$(2 \le n \le 2\cdot10^{5})$$$ — the length of string $$$s$$$. The second line of the input contains the string $$$s$$$ of length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'.
["2\nabba", "0\nababab", "1\nba"]
#include<stdio.h> int main(){ int n; scanf("%d",&n); getchar(); char a[n+1]; gets(a); int dem=0; for(int i=0;i<n;i+=2){ if(a[i]==a[i+1]&&a[i]=='a'){ a[i]='b'; dem++; } else if(a[i]==a[i+1]&&a[i]=='b'){ a[i]='a'; dem++; } } printf("%d\n%s",dem,a); return 0; }
Nikolay got a string $$$s$$$ of even length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'. Its positions are numbered from $$$1$$$ to $$$n$$$.He wants to modify his string so that every its prefix of even length has an equal amount of letters 'a' and 'b'. To achieve that, Nikolay can perform the following operation arbitrary number of times (possibly, zero): choose some position in his string and replace the letter on this position with the other letter (i.e. replace 'a' with 'b' or replace 'b' with 'a'). Nikolay can use no letters except 'a' and 'b'.The prefix of string $$$s$$$ of length $$$l$$$ ($$$1 \le l \le n$$$) is a string $$$s[1..l]$$$.For example, for the string $$$s=$$$"abba" there are two prefixes of the even length. The first is $$$s[1\dots2]=$$$"ab" and the second $$$s[1\dots4]=$$$"abba". Both of them have the same number of 'a' and 'b'.Your task is to calculate the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'.
In the first line print the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'. In the second line print the string Nikolay obtains after applying all the operations. If there are multiple answers, you can print any of them.
C
8ad06ac90b258a8233e2a1cf51f68078
ff48844b0803d098e8dabe0ffe21664b
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "strings" ]
1569049500
["4\nbbbb", "6\nababab", "2\naa"]
NoteIn the first example Nikolay has to perform two operations. For example, he can replace the first 'b' with 'a' and the last 'b' with 'a'. In the second example Nikolay doesn't need to do anything because each prefix of an even length of the initial string already contains an equal amount of letters 'a' and 'b'.
PASSED
800
standard input
1 second
The first line of the input contains one even integer $$$n$$$ $$$(2 \le n \le 2\cdot10^{5})$$$ — the length of string $$$s$$$. The second line of the input contains the string $$$s$$$ of length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'.
["2\nabba", "0\nababab", "1\nba"]
#include <stdio.h> #include <string.h> int main() { int n,i,count=0; char srt[200000]; scanf("%d %s",&n,srt); for(i=0;i<n;i+=2) { if(srt[i]=='a' && srt[i+1]=='a') { srt[i]='b'; count=count+1; } else if(srt[i]=='b' && srt[i+1]=='b') { srt[i]='a'; count=count+1; } } printf("%d\n%s",count,srt); return 0; }
Nikolay got a string $$$s$$$ of even length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'. Its positions are numbered from $$$1$$$ to $$$n$$$.He wants to modify his string so that every its prefix of even length has an equal amount of letters 'a' and 'b'. To achieve that, Nikolay can perform the following operation arbitrary number of times (possibly, zero): choose some position in his string and replace the letter on this position with the other letter (i.e. replace 'a' with 'b' or replace 'b' with 'a'). Nikolay can use no letters except 'a' and 'b'.The prefix of string $$$s$$$ of length $$$l$$$ ($$$1 \le l \le n$$$) is a string $$$s[1..l]$$$.For example, for the string $$$s=$$$"abba" there are two prefixes of the even length. The first is $$$s[1\dots2]=$$$"ab" and the second $$$s[1\dots4]=$$$"abba". Both of them have the same number of 'a' and 'b'.Your task is to calculate the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'.
In the first line print the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'. In the second line print the string Nikolay obtains after applying all the operations. If there are multiple answers, you can print any of them.
C
8ad06ac90b258a8233e2a1cf51f68078
1f19365f772f9b43331a5634d071d1fa
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "strings" ]
1569049500
["4\nbbbb", "6\nababab", "2\naa"]
NoteIn the first example Nikolay has to perform two operations. For example, he can replace the first 'b' with 'a' and the last 'b' with 'a'. In the second example Nikolay doesn't need to do anything because each prefix of an even length of the initial string already contains an equal amount of letters 'a' and 'b'.
PASSED
800
standard input
1 second
The first line of the input contains one even integer $$$n$$$ $$$(2 \le n \le 2\cdot10^{5})$$$ — the length of string $$$s$$$. The second line of the input contains the string $$$s$$$ of length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'.
["2\nabba", "0\nababab", "1\nba"]
#include<stdio.h> #include<string.h> int main() { int n; scanf("%d",&n); char str[1000000]; scanf("%s",str); int i=0,d=strlen(str),s=0; while (i<d) { if (!(((str[i]=='a') && (str[i+1]=='b')) || ((str[i]=='b') && (str[i+1]=='a')))) { if (str[i]=='a') { str[i]='b'; } else { str[i]='a'; } s=s+1; } i=i+2; } printf("%d\n",s); printf("%s",str); return 0; }
Nikolay got a string $$$s$$$ of even length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'. Its positions are numbered from $$$1$$$ to $$$n$$$.He wants to modify his string so that every its prefix of even length has an equal amount of letters 'a' and 'b'. To achieve that, Nikolay can perform the following operation arbitrary number of times (possibly, zero): choose some position in his string and replace the letter on this position with the other letter (i.e. replace 'a' with 'b' or replace 'b' with 'a'). Nikolay can use no letters except 'a' and 'b'.The prefix of string $$$s$$$ of length $$$l$$$ ($$$1 \le l \le n$$$) is a string $$$s[1..l]$$$.For example, for the string $$$s=$$$"abba" there are two prefixes of the even length. The first is $$$s[1\dots2]=$$$"ab" and the second $$$s[1\dots4]=$$$"abba". Both of them have the same number of 'a' and 'b'.Your task is to calculate the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'.
In the first line print the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'. In the second line print the string Nikolay obtains after applying all the operations. If there are multiple answers, you can print any of them.
C
8ad06ac90b258a8233e2a1cf51f68078
c628f46b74fafa04e14fade7fff44715
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "strings" ]
1569049500
["4\nbbbb", "6\nababab", "2\naa"]
NoteIn the first example Nikolay has to perform two operations. For example, he can replace the first 'b' with 'a' and the last 'b' with 'a'. In the second example Nikolay doesn't need to do anything because each prefix of an even length of the initial string already contains an equal amount of letters 'a' and 'b'.
PASSED
800
standard input
1 second
The first line of the input contains one even integer $$$n$$$ $$$(2 \le n \le 2\cdot10^{5})$$$ — the length of string $$$s$$$. The second line of the input contains the string $$$s$$$ of length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'.
["2\nabba", "0\nababab", "1\nba"]
#include<stdio.h> int main() { int i,n,res=0; scanf("%d",&n); char str[n]; scanf("%s",str); for(i=0;i<n;i+=2) { if(!((str[i]=='a' && str[i+1]=='b')||(str[i]=='b' && str[i+1]=='a'))) { if(str[i]=='a') str[i]='b'; else str[i]='a'; res++; } } printf("%d\n%s\n ",res,str); return 0; }
Nikolay got a string $$$s$$$ of even length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'. Its positions are numbered from $$$1$$$ to $$$n$$$.He wants to modify his string so that every its prefix of even length has an equal amount of letters 'a' and 'b'. To achieve that, Nikolay can perform the following operation arbitrary number of times (possibly, zero): choose some position in his string and replace the letter on this position with the other letter (i.e. replace 'a' with 'b' or replace 'b' with 'a'). Nikolay can use no letters except 'a' and 'b'.The prefix of string $$$s$$$ of length $$$l$$$ ($$$1 \le l \le n$$$) is a string $$$s[1..l]$$$.For example, for the string $$$s=$$$"abba" there are two prefixes of the even length. The first is $$$s[1\dots2]=$$$"ab" and the second $$$s[1\dots4]=$$$"abba". Both of them have the same number of 'a' and 'b'.Your task is to calculate the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'.
In the first line print the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'. In the second line print the string Nikolay obtains after applying all the operations. If there are multiple answers, you can print any of them.
C
8ad06ac90b258a8233e2a1cf51f68078
7ad4128893139c82ec6a7d629627a4a3
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "strings" ]
1569049500
["4\nbbbb", "6\nababab", "2\naa"]
NoteIn the first example Nikolay has to perform two operations. For example, he can replace the first 'b' with 'a' and the last 'b' with 'a'. In the second example Nikolay doesn't need to do anything because each prefix of an even length of the initial string already contains an equal amount of letters 'a' and 'b'.
PASSED
800
standard input
1 second
The first line of the input contains one even integer $$$n$$$ $$$(2 \le n \le 2\cdot10^{5})$$$ — the length of string $$$s$$$. The second line of the input contains the string $$$s$$$ of length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'.
["2\nabba", "0\nababab", "1\nba"]
#include<stdio.h> int main() { int i,n,res=0; scanf("%d",&n); char str[n]; scanf("%s",str); for(i=0;i<n;i+=2) { if(!((str[i]=='a' && str[i+1]=='b')||(str[i]=='b' && str[i+1]=='a'))) { if(str[i]=='a') str[i]='b'; else str[i]='a'; ++res; } } printf("%d\n%s\n",res,str); return 0; }
Nikolay got a string $$$s$$$ of even length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'. Its positions are numbered from $$$1$$$ to $$$n$$$.He wants to modify his string so that every its prefix of even length has an equal amount of letters 'a' and 'b'. To achieve that, Nikolay can perform the following operation arbitrary number of times (possibly, zero): choose some position in his string and replace the letter on this position with the other letter (i.e. replace 'a' with 'b' or replace 'b' with 'a'). Nikolay can use no letters except 'a' and 'b'.The prefix of string $$$s$$$ of length $$$l$$$ ($$$1 \le l \le n$$$) is a string $$$s[1..l]$$$.For example, for the string $$$s=$$$"abba" there are two prefixes of the even length. The first is $$$s[1\dots2]=$$$"ab" and the second $$$s[1\dots4]=$$$"abba". Both of them have the same number of 'a' and 'b'.Your task is to calculate the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'.
In the first line print the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'. In the second line print the string Nikolay obtains after applying all the operations. If there are multiple answers, you can print any of them.
C
8ad06ac90b258a8233e2a1cf51f68078
3181cc7c664fc1baee496ca66afb1ff0
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "strings" ]
1569049500
["4\nbbbb", "6\nababab", "2\naa"]
NoteIn the first example Nikolay has to perform two operations. For example, he can replace the first 'b' with 'a' and the last 'b' with 'a'. In the second example Nikolay doesn't need to do anything because each prefix of an even length of the initial string already contains an equal amount of letters 'a' and 'b'.
PASSED
800
standard input
1 second
The first line of the input contains one even integer $$$n$$$ $$$(2 \le n \le 2\cdot10^{5})$$$ — the length of string $$$s$$$. The second line of the input contains the string $$$s$$$ of length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'.
["2\nabba", "0\nababab", "1\nba"]
#include<stdio.h> #include<string.h> int main() { int n,i,j,count=0; scanf("%d",&n); char arr[n]; getchar(); gets(arr); for(i=0;i<n;i+=2) { if((arr[i]=='a')&&(arr[i+1]=='a')) { arr[i+1]='b'; count++; } if((arr[i]=='b') && (arr[i+1]=='b')) { arr[i+1] ='a'; count++; } } printf("%d\n%s",count,arr); }
Nikolay got a string $$$s$$$ of even length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'. Its positions are numbered from $$$1$$$ to $$$n$$$.He wants to modify his string so that every its prefix of even length has an equal amount of letters 'a' and 'b'. To achieve that, Nikolay can perform the following operation arbitrary number of times (possibly, zero): choose some position in his string and replace the letter on this position with the other letter (i.e. replace 'a' with 'b' or replace 'b' with 'a'). Nikolay can use no letters except 'a' and 'b'.The prefix of string $$$s$$$ of length $$$l$$$ ($$$1 \le l \le n$$$) is a string $$$s[1..l]$$$.For example, for the string $$$s=$$$"abba" there are two prefixes of the even length. The first is $$$s[1\dots2]=$$$"ab" and the second $$$s[1\dots4]=$$$"abba". Both of them have the same number of 'a' and 'b'.Your task is to calculate the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'.
In the first line print the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'. In the second line print the string Nikolay obtains after applying all the operations. If there are multiple answers, you can print any of them.
C
8ad06ac90b258a8233e2a1cf51f68078
71e7d6f2752761f79d13c878639d389f
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "strings" ]
1569049500
["4\nbbbb", "6\nababab", "2\naa"]
NoteIn the first example Nikolay has to perform two operations. For example, he can replace the first 'b' with 'a' and the last 'b' with 'a'. In the second example Nikolay doesn't need to do anything because each prefix of an even length of the initial string already contains an equal amount of letters 'a' and 'b'.
PASSED
800
standard input
1 second
The first line of the input contains one even integer $$$n$$$ $$$(2 \le n \le 2\cdot10^{5})$$$ — the length of string $$$s$$$. The second line of the input contains the string $$$s$$$ of length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'.
["2\nabba", "0\nababab", "1\nba"]
#include<stdio.h> #include<string.h> int main() { int n,j,k,l=0; scanf("%d",&n); char a[n]; scanf("%s",a); for(int i=0;a[i]!=0;i=i+2) { k=a[i+1]-a[i]; if((a[i]=='a' || a[i]=='b') && abs(k)==1) { } else { if(a[i]=='a') a[i]='b'; else a[i]='a'; l++; } } printf("%d\n%s",l,a); }
Nikolay got a string $$$s$$$ of even length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'. Its positions are numbered from $$$1$$$ to $$$n$$$.He wants to modify his string so that every its prefix of even length has an equal amount of letters 'a' and 'b'. To achieve that, Nikolay can perform the following operation arbitrary number of times (possibly, zero): choose some position in his string and replace the letter on this position with the other letter (i.e. replace 'a' with 'b' or replace 'b' with 'a'). Nikolay can use no letters except 'a' and 'b'.The prefix of string $$$s$$$ of length $$$l$$$ ($$$1 \le l \le n$$$) is a string $$$s[1..l]$$$.For example, for the string $$$s=$$$"abba" there are two prefixes of the even length. The first is $$$s[1\dots2]=$$$"ab" and the second $$$s[1\dots4]=$$$"abba". Both of them have the same number of 'a' and 'b'.Your task is to calculate the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'.
In the first line print the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'. In the second line print the string Nikolay obtains after applying all the operations. If there are multiple answers, you can print any of them.
C
8ad06ac90b258a8233e2a1cf51f68078
b0de4de424343f81020700c49e5c618d
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "strings" ]
1569049500
["4\nbbbb", "6\nababab", "2\naa"]
NoteIn the first example Nikolay has to perform two operations. For example, he can replace the first 'b' with 'a' and the last 'b' with 'a'. In the second example Nikolay doesn't need to do anything because each prefix of an even length of the initial string already contains an equal amount of letters 'a' and 'b'.
PASSED
800
standard input
1 second
The first line of the input contains one even integer $$$n$$$ $$$(2 \le n \le 2\cdot10^{5})$$$ — the length of string $$$s$$$. The second line of the input contains the string $$$s$$$ of length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'.
["2\nabba", "0\nababab", "1\nba"]
#include<stdio.h> int main() { int n,i,c=0; scanf("%d",&n); char s[n+1]; scanf("%s",&s); for(i=0;i<n;i=i+2) { if(s[i]==s[i+1]) c++; } printf("%d\n",c); for(i=0;i<n;i=i+2) { if(s[i]==s[i+1]) printf("ab"); if(s[i]!=s[i+1]) printf("%c%c",s[i],s[i+1]); } return 0; }
Nikolay got a string $$$s$$$ of even length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'. Its positions are numbered from $$$1$$$ to $$$n$$$.He wants to modify his string so that every its prefix of even length has an equal amount of letters 'a' and 'b'. To achieve that, Nikolay can perform the following operation arbitrary number of times (possibly, zero): choose some position in his string and replace the letter on this position with the other letter (i.e. replace 'a' with 'b' or replace 'b' with 'a'). Nikolay can use no letters except 'a' and 'b'.The prefix of string $$$s$$$ of length $$$l$$$ ($$$1 \le l \le n$$$) is a string $$$s[1..l]$$$.For example, for the string $$$s=$$$"abba" there are two prefixes of the even length. The first is $$$s[1\dots2]=$$$"ab" and the second $$$s[1\dots4]=$$$"abba". Both of them have the same number of 'a' and 'b'.Your task is to calculate the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'.
In the first line print the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'. In the second line print the string Nikolay obtains after applying all the operations. If there are multiple answers, you can print any of them.
C
8ad06ac90b258a8233e2a1cf51f68078
1f39275c12f0c39e632fe3f9a3e634f5
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "strings" ]
1569049500
["4\nbbbb", "6\nababab", "2\naa"]
NoteIn the first example Nikolay has to perform two operations. For example, he can replace the first 'b' with 'a' and the last 'b' with 'a'. In the second example Nikolay doesn't need to do anything because each prefix of an even length of the initial string already contains an equal amount of letters 'a' and 'b'.
PASSED
800
standard input
1 second
The first line of the input contains one even integer $$$n$$$ $$$(2 \le n \le 2\cdot10^{5})$$$ — the length of string $$$s$$$. The second line of the input contains the string $$$s$$$ of length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'.
["2\nabba", "0\nababab", "1\nba"]
#include <stdio.h> int main() { int a,i,e=0; scanf("%d",&a); char bha[a],c,d; scanf("%s",bha); for(i=1;i<a;i+=2) { c=bha[i]; d=bha[i-1]; if(c==d) { e++; if(d=='a') { bha[i-1]='b'; } else { bha[i-1]='a'; } } } printf("%d\n%s",e,bha); }
Nikolay got a string $$$s$$$ of even length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'. Its positions are numbered from $$$1$$$ to $$$n$$$.He wants to modify his string so that every its prefix of even length has an equal amount of letters 'a' and 'b'. To achieve that, Nikolay can perform the following operation arbitrary number of times (possibly, zero): choose some position in his string and replace the letter on this position with the other letter (i.e. replace 'a' with 'b' or replace 'b' with 'a'). Nikolay can use no letters except 'a' and 'b'.The prefix of string $$$s$$$ of length $$$l$$$ ($$$1 \le l \le n$$$) is a string $$$s[1..l]$$$.For example, for the string $$$s=$$$"abba" there are two prefixes of the even length. The first is $$$s[1\dots2]=$$$"ab" and the second $$$s[1\dots4]=$$$"abba". Both of them have the same number of 'a' and 'b'.Your task is to calculate the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'.
In the first line print the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'. In the second line print the string Nikolay obtains after applying all the operations. If there are multiple answers, you can print any of them.
C
8ad06ac90b258a8233e2a1cf51f68078
6e1e231d5ac3f62f20413e713dbc1579
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "strings" ]
1569049500
["4\nbbbb", "6\nababab", "2\naa"]
NoteIn the first example Nikolay has to perform two operations. For example, he can replace the first 'b' with 'a' and the last 'b' with 'a'. In the second example Nikolay doesn't need to do anything because each prefix of an even length of the initial string already contains an equal amount of letters 'a' and 'b'.
PASSED
800
standard input
1 second
The first line of the input contains one even integer $$$n$$$ $$$(2 \le n \le 2\cdot10^{5})$$$ — the length of string $$$s$$$. The second line of the input contains the string $$$s$$$ of length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'.
["2\nabba", "0\nababab", "1\nba"]
#include <stdio.h> int main(int argc, const char * argv[]) { int num, i; int nflip=0; //printf("Enter the number of digits"); scanf("%d", &num); char string[num+1]; scanf("%s", string); for (i=1; i<num; i+=2) { if(string[i]==string[i-1]) { nflip++; if (string[i]=='a') string[i]='b'; else string[i]='a'; } } printf("%d\n%s",nflip, string); return 0; }
Nikolay got a string $$$s$$$ of even length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'. Its positions are numbered from $$$1$$$ to $$$n$$$.He wants to modify his string so that every its prefix of even length has an equal amount of letters 'a' and 'b'. To achieve that, Nikolay can perform the following operation arbitrary number of times (possibly, zero): choose some position in his string and replace the letter on this position with the other letter (i.e. replace 'a' with 'b' or replace 'b' with 'a'). Nikolay can use no letters except 'a' and 'b'.The prefix of string $$$s$$$ of length $$$l$$$ ($$$1 \le l \le n$$$) is a string $$$s[1..l]$$$.For example, for the string $$$s=$$$"abba" there are two prefixes of the even length. The first is $$$s[1\dots2]=$$$"ab" and the second $$$s[1\dots4]=$$$"abba". Both of them have the same number of 'a' and 'b'.Your task is to calculate the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'.
In the first line print the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'. In the second line print the string Nikolay obtains after applying all the operations. If there are multiple answers, you can print any of them.
C
8ad06ac90b258a8233e2a1cf51f68078
11ee443102d4dd86fdcff38eda63b788
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "strings" ]
1569049500
["4\nbbbb", "6\nababab", "2\naa"]
NoteIn the first example Nikolay has to perform two operations. For example, he can replace the first 'b' with 'a' and the last 'b' with 'a'. In the second example Nikolay doesn't need to do anything because each prefix of an even length of the initial string already contains an equal amount of letters 'a' and 'b'.
PASSED
800
standard input
1 second
The first line of the input contains one even integer $$$n$$$ $$$(2 \le n \le 2\cdot10^{5})$$$ — the length of string $$$s$$$. The second line of the input contains the string $$$s$$$ of length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'.
["2\nabba", "0\nababab", "1\nba"]
#include<stdio.h> #include<string.h> int check(int n,int *arr,int k){ for(int i=0;i<k;i++){ if(arr[i]==n)return 1; } return 0; } int main() { int n,sum=0; scanf("%d",&n); char a[n]; char f,m; scanf("%s",a); if(a[0]=='a'){f='a';m='b';} else{f='b';m='a';} for(int i=0;i<n;i+=2){ if(a[i]==a[i+1]){ a[i+1]=a[i]=='a'?'b':'a'; sum++; } } printf("%d\n",sum); printf("%s",a); return 0; }
Nikolay got a string $$$s$$$ of even length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'. Its positions are numbered from $$$1$$$ to $$$n$$$.He wants to modify his string so that every its prefix of even length has an equal amount of letters 'a' and 'b'. To achieve that, Nikolay can perform the following operation arbitrary number of times (possibly, zero): choose some position in his string and replace the letter on this position with the other letter (i.e. replace 'a' with 'b' or replace 'b' with 'a'). Nikolay can use no letters except 'a' and 'b'.The prefix of string $$$s$$$ of length $$$l$$$ ($$$1 \le l \le n$$$) is a string $$$s[1..l]$$$.For example, for the string $$$s=$$$"abba" there are two prefixes of the even length. The first is $$$s[1\dots2]=$$$"ab" and the second $$$s[1\dots4]=$$$"abba". Both of them have the same number of 'a' and 'b'.Your task is to calculate the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'.
In the first line print the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'. In the second line print the string Nikolay obtains after applying all the operations. If there are multiple answers, you can print any of them.
C
8ad06ac90b258a8233e2a1cf51f68078
12b652d1f2d65b3e0444dce267946c3d
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "strings" ]
1569049500
["4\nbbbb", "6\nababab", "2\naa"]
NoteIn the first example Nikolay has to perform two operations. For example, he can replace the first 'b' with 'a' and the last 'b' with 'a'. In the second example Nikolay doesn't need to do anything because each prefix of an even length of the initial string already contains an equal amount of letters 'a' and 'b'.
PASSED
800
standard input
1 second
The first line of the input contains one even integer $$$n$$$ $$$(2 \le n \le 2\cdot10^{5})$$$ — the length of string $$$s$$$. The second line of the input contains the string $$$s$$$ of length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'.
["2\nabba", "0\nababab", "1\nba"]
#include<stdio.h> #include<string.h> char ch[1000000]; int main(void) { int n; int i; scanf("%d",&n); scanf("%s",ch); int count = 0; for(i = 0;i<n;i+=2) { if(ch[i] == ch[i+1]) { count++; if(ch[i] == 'a') ch[i]+=1; else ch[i]-=1; } } printf("%d\n%s\n",count,ch); return 0; }
Nikolay got a string $$$s$$$ of even length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'. Its positions are numbered from $$$1$$$ to $$$n$$$.He wants to modify his string so that every its prefix of even length has an equal amount of letters 'a' and 'b'. To achieve that, Nikolay can perform the following operation arbitrary number of times (possibly, zero): choose some position in his string and replace the letter on this position with the other letter (i.e. replace 'a' with 'b' or replace 'b' with 'a'). Nikolay can use no letters except 'a' and 'b'.The prefix of string $$$s$$$ of length $$$l$$$ ($$$1 \le l \le n$$$) is a string $$$s[1..l]$$$.For example, for the string $$$s=$$$"abba" there are two prefixes of the even length. The first is $$$s[1\dots2]=$$$"ab" and the second $$$s[1\dots4]=$$$"abba". Both of them have the same number of 'a' and 'b'.Your task is to calculate the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'.
In the first line print the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'. In the second line print the string Nikolay obtains after applying all the operations. If there are multiple answers, you can print any of them.
C
8ad06ac90b258a8233e2a1cf51f68078
53cdd3858d77de682b8b0ecd83ec0bb4
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "strings" ]
1569049500
["4\nbbbb", "6\nababab", "2\naa"]
NoteIn the first example Nikolay has to perform two operations. For example, he can replace the first 'b' with 'a' and the last 'b' with 'a'. In the second example Nikolay doesn't need to do anything because each prefix of an even length of the initial string already contains an equal amount of letters 'a' and 'b'.
PASSED
800
standard input
1 second
The first line of the input contains one even integer $$$n$$$ $$$(2 \le n \le 2\cdot10^{5})$$$ — the length of string $$$s$$$. The second line of the input contains the string $$$s$$$ of length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'.
["2\nabba", "0\nababab", "1\nba"]
#include<stdio.h> int main(){ int n; scanf("%d",&n); char s[n]; scanf("%s",s); int c=0; for(int i=0;i<n;i+=2){ if(!(s[i]=='a'&& s[i+1]=='b'|| s[i]=='b' && s[i+1]=='a')){ if(s[i]=='a') s[i]='b'; else s[i]='a'; c=c+1; } } printf("%d",c); printf("\n%s",s); return 0;}
Nikolay got a string $$$s$$$ of even length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'. Its positions are numbered from $$$1$$$ to $$$n$$$.He wants to modify his string so that every its prefix of even length has an equal amount of letters 'a' and 'b'. To achieve that, Nikolay can perform the following operation arbitrary number of times (possibly, zero): choose some position in his string and replace the letter on this position with the other letter (i.e. replace 'a' with 'b' or replace 'b' with 'a'). Nikolay can use no letters except 'a' and 'b'.The prefix of string $$$s$$$ of length $$$l$$$ ($$$1 \le l \le n$$$) is a string $$$s[1..l]$$$.For example, for the string $$$s=$$$"abba" there are two prefixes of the even length. The first is $$$s[1\dots2]=$$$"ab" and the second $$$s[1\dots4]=$$$"abba". Both of them have the same number of 'a' and 'b'.Your task is to calculate the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'.
In the first line print the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'. In the second line print the string Nikolay obtains after applying all the operations. If there are multiple answers, you can print any of them.
C
8ad06ac90b258a8233e2a1cf51f68078
db3c54343af5fdfd6149fb6e798f9f26
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "strings" ]
1569049500
["4\nbbbb", "6\nababab", "2\naa"]
NoteIn the first example Nikolay has to perform two operations. For example, he can replace the first 'b' with 'a' and the last 'b' with 'a'. In the second example Nikolay doesn't need to do anything because each prefix of an even length of the initial string already contains an equal amount of letters 'a' and 'b'.
PASSED
800
standard input
1 second
The first line of the input contains one even integer $$$n$$$ $$$(2 \le n \le 2\cdot10^{5})$$$ — the length of string $$$s$$$. The second line of the input contains the string $$$s$$$ of length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'.
["2\nabba", "0\nababab", "1\nba"]
#include <stdio.h> #include <stdlib.h> typedef long long ll; int main() { ll n,c=0,i; scanf("%I64d",&n); char str[n]; scanf("%s",str); for(i=0;i<n;i+=2) { if(str[i]=='a'&&str[i+1]=='a' || str[i]=='b'&&str[i+1]=='b') { c++; str[i]='a'; str[i+1]='b'; } } printf("%I64d\n",c); printf("%s",str); return 0; }
Nikolay got a string $$$s$$$ of even length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'. Its positions are numbered from $$$1$$$ to $$$n$$$.He wants to modify his string so that every its prefix of even length has an equal amount of letters 'a' and 'b'. To achieve that, Nikolay can perform the following operation arbitrary number of times (possibly, zero): choose some position in his string and replace the letter on this position with the other letter (i.e. replace 'a' with 'b' or replace 'b' with 'a'). Nikolay can use no letters except 'a' and 'b'.The prefix of string $$$s$$$ of length $$$l$$$ ($$$1 \le l \le n$$$) is a string $$$s[1..l]$$$.For example, for the string $$$s=$$$"abba" there are two prefixes of the even length. The first is $$$s[1\dots2]=$$$"ab" and the second $$$s[1\dots4]=$$$"abba". Both of them have the same number of 'a' and 'b'.Your task is to calculate the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'.
In the first line print the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'. In the second line print the string Nikolay obtains after applying all the operations. If there are multiple answers, you can print any of them.
C
8ad06ac90b258a8233e2a1cf51f68078
0563139943128f376bc119d404a0f7af
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "strings" ]
1569049500
["4\nbbbb", "6\nababab", "2\naa"]
NoteIn the first example Nikolay has to perform two operations. For example, he can replace the first 'b' with 'a' and the last 'b' with 'a'. In the second example Nikolay doesn't need to do anything because each prefix of an even length of the initial string already contains an equal amount of letters 'a' and 'b'.
PASSED
800
standard input
1 second
The first line of the input contains one even integer $$$n$$$ $$$(2 \le n \le 2\cdot10^{5})$$$ — the length of string $$$s$$$. The second line of the input contains the string $$$s$$$ of length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'.
["2\nabba", "0\nababab", "1\nba"]
#include <stdio.h> void ler_array(int i,int n,char a[]) {int aux; if(i == n) { return; } else { scanf(" %c",&a[i]); ler_array(i+1,n,a); } } void escolher(int i,int n,char a[],int cont) { if(i == n) { printf("%d\n",cont); return; } else if(i == 0) { if(a[i] == 'a') { if(a[i+1] == 'b') { escolher(i+2,n,a,cont); } else { a[i+1] = 'b'; cont = cont+1; escolher(i+2,n,a,cont); } } else if(a[i] == 'b') { if(a[i+1] == 'a') { escolher(i+2,n,a,cont); } else { a[i+1] = 'a'; cont = cont+1; escolher(i+2,n,a,cont); } } } else if(i > 0) { if(a[i] == 'a') { if(a[i+1] == 'b') { escolher(i+2,n,a,cont); } else { a[i+1] = 'b'; cont = cont+1; escolher(i+2,n,a,cont); } } else if(a[i] == 'b') { if(a[i+1] == 'a') { escolher(i+2,n,a,cont); } else { a[i+1] = 'a'; cont = cont+1; escolher(i+2,n,a,cont); } } } } void escolher_impa(int i,int n,char a[],int cont) { if(i == n) { printf("%d\n",cont); return; } else if(i == 0) { if(a[i] == 'a') { if(a[i+1] == 'b') { escolher_impa(i+2,n,a,cont); } else { a[i+1] = 'b'; cont = cont+1; escolher_impa(i+2,n,a,cont); } } else if(a[i] == 'b') { if(a[i+1] == 'a') { escolher_impa(i+2,n,a,cont); } else { a[i+1] = 'a'; cont = cont+1; escolher_impa(i+2,n,a,cont); } } } else if(i > 0) { if(a[i] == 'a') { if(a[i+1] == 'b') { escolher_impa(i+2,n,a,cont); } else { a[i+1] = 'b'; cont = cont+1; escolher_impa(i+2,n,a,cont); } } else if(a[i] == 'b') { if(a[i+1] == 'a') { escolher_impa(i+2,n,a,cont); } else { a[i+1] = 'a'; cont = cont+1; escolher_impa(i+2,n,a,cont); } } } } int main(void) { int n; scanf("%d",&n); char a[n]; int i = 0; ler_array(0,n,a); if(n%2 == 0) { escolher(0,n,a,0); } else { escolher_impa(0,n-1,a,0); } for (i=0;i<n;i++) { printf("%c",a[i]); } return(0); }
Nikolay got a string $$$s$$$ of even length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'. Its positions are numbered from $$$1$$$ to $$$n$$$.He wants to modify his string so that every its prefix of even length has an equal amount of letters 'a' and 'b'. To achieve that, Nikolay can perform the following operation arbitrary number of times (possibly, zero): choose some position in his string and replace the letter on this position with the other letter (i.e. replace 'a' with 'b' or replace 'b' with 'a'). Nikolay can use no letters except 'a' and 'b'.The prefix of string $$$s$$$ of length $$$l$$$ ($$$1 \le l \le n$$$) is a string $$$s[1..l]$$$.For example, for the string $$$s=$$$"abba" there are two prefixes of the even length. The first is $$$s[1\dots2]=$$$"ab" and the second $$$s[1\dots4]=$$$"abba". Both of them have the same number of 'a' and 'b'.Your task is to calculate the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'.
In the first line print the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'. In the second line print the string Nikolay obtains after applying all the operations. If there are multiple answers, you can print any of them.
C
8ad06ac90b258a8233e2a1cf51f68078
eeefe55297190342cf9f033a3bfa819f
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "strings" ]
1569049500
["4\nbbbb", "6\nababab", "2\naa"]
NoteIn the first example Nikolay has to perform two operations. For example, he can replace the first 'b' with 'a' and the last 'b' with 'a'. In the second example Nikolay doesn't need to do anything because each prefix of an even length of the initial string already contains an equal amount of letters 'a' and 'b'.
PASSED
800
standard input
1 second
The first line of the input contains one even integer $$$n$$$ $$$(2 \le n \le 2\cdot10^{5})$$$ — the length of string $$$s$$$. The second line of the input contains the string $$$s$$$ of length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'.
["2\nabba", "0\nababab", "1\nba"]
#include<stdio.h> #include<string.h> int main() { int n, cnt = 0, i; char a[200000]; scanf("%d", &n); getchar(); gets(a); int p; p=strlen(a)-1; for (i = 0; i < p;i=i+2) { if (a[i] == a[i + 1]) { if (a[i] == 'a') a[i] = 'b'; else a[i] = 'a'; cnt++; } } printf("%d\n", cnt); puts(a); return 0; }
Nikolay got a string $$$s$$$ of even length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'. Its positions are numbered from $$$1$$$ to $$$n$$$.He wants to modify his string so that every its prefix of even length has an equal amount of letters 'a' and 'b'. To achieve that, Nikolay can perform the following operation arbitrary number of times (possibly, zero): choose some position in his string and replace the letter on this position with the other letter (i.e. replace 'a' with 'b' or replace 'b' with 'a'). Nikolay can use no letters except 'a' and 'b'.The prefix of string $$$s$$$ of length $$$l$$$ ($$$1 \le l \le n$$$) is a string $$$s[1..l]$$$.For example, for the string $$$s=$$$"abba" there are two prefixes of the even length. The first is $$$s[1\dots2]=$$$"ab" and the second $$$s[1\dots4]=$$$"abba". Both of them have the same number of 'a' and 'b'.Your task is to calculate the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'.
In the first line print the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'. In the second line print the string Nikolay obtains after applying all the operations. If there are multiple answers, you can print any of them.
C
8ad06ac90b258a8233e2a1cf51f68078
00054cdf3904c227ed2e385a01e3827e
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "strings" ]
1569049500
["4\nbbbb", "6\nababab", "2\naa"]
NoteIn the first example Nikolay has to perform two operations. For example, he can replace the first 'b' with 'a' and the last 'b' with 'a'. In the second example Nikolay doesn't need to do anything because each prefix of an even length of the initial string already contains an equal amount of letters 'a' and 'b'.
PASSED
800
standard input
1 second
The first line of the input contains one even integer $$$n$$$ $$$(2 \le n \le 2\cdot10^{5})$$$ — the length of string $$$s$$$. The second line of the input contains the string $$$s$$$ of length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'.
["2\nabba", "0\nababab", "1\nba"]
#include <stdio.h> int main(){ char s[200001]; int n,k=0; scanf("%d",&n); scanf("%s",s); for(int i=0;i<n;i=i+2){ if(s[i]==s[i+1]&&s[i]=='a') { s[i]='b'; k++; } else if(s[i]==s[i+1]&&s[i]=='b') { s[i]='a'; k++; } } printf("%d\n",k); puts(s); return 0; }
Nikolay got a string $$$s$$$ of even length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'. Its positions are numbered from $$$1$$$ to $$$n$$$.He wants to modify his string so that every its prefix of even length has an equal amount of letters 'a' and 'b'. To achieve that, Nikolay can perform the following operation arbitrary number of times (possibly, zero): choose some position in his string and replace the letter on this position with the other letter (i.e. replace 'a' with 'b' or replace 'b' with 'a'). Nikolay can use no letters except 'a' and 'b'.The prefix of string $$$s$$$ of length $$$l$$$ ($$$1 \le l \le n$$$) is a string $$$s[1..l]$$$.For example, for the string $$$s=$$$"abba" there are two prefixes of the even length. The first is $$$s[1\dots2]=$$$"ab" and the second $$$s[1\dots4]=$$$"abba". Both of them have the same number of 'a' and 'b'.Your task is to calculate the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'.
In the first line print the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'. In the second line print the string Nikolay obtains after applying all the operations. If there are multiple answers, you can print any of them.
C
8ad06ac90b258a8233e2a1cf51f68078
780043cd475449fa97aca9144abd1a0b
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "strings" ]
1569049500
["4\nbbbb", "6\nababab", "2\naa"]
NoteIn the first example Nikolay has to perform two operations. For example, he can replace the first 'b' with 'a' and the last 'b' with 'a'. In the second example Nikolay doesn't need to do anything because each prefix of an even length of the initial string already contains an equal amount of letters 'a' and 'b'.
PASSED
800
standard input
1 second
The first line of the input contains one even integer $$$n$$$ $$$(2 \le n \le 2\cdot10^{5})$$$ — the length of string $$$s$$$. The second line of the input contains the string $$$s$$$ of length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'.
["2\nabba", "0\nababab", "1\nba"]
#include<stdio.h> #include<string.h> #include<stdlib.h> #define A 'a' #define B 'b' int main(){ int i; int n, m; scanf("%d",&n); char s[n]; scanf("%s",s); m = 0; for(i = 0; i < n; i += 2){ if(s[i] == 'a' && s[i + 1] == 'a') m++, s[i] = 'b'; else if(s[i] == 'b' && s[i + 1] == 'b') m++, s[i] = 'a'; } printf("%d\n%s",m,s); /* int n,c=0,i; scanf("%d",&n); char *s=malloc(n); scanf("%s",s); for(i=0;i<n/2;i+=2) { if(s[i]==A && s[i+1]==A){ c++;s[i+1]=B; } else if (s[i]==B && s[i+1]==B){ c++;s[i+1]=A; } if(s[n-1-i]==A && s[n-2-i]==A){ c++;s[n-2-i]=B; } else if(s[n-1-i]==B && s[n-2-i]==B){ c++;s[n-2-i]=A; } } printf("%d\n%s",c,s); */ return 0; }
Nikolay got a string $$$s$$$ of even length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'. Its positions are numbered from $$$1$$$ to $$$n$$$.He wants to modify his string so that every its prefix of even length has an equal amount of letters 'a' and 'b'. To achieve that, Nikolay can perform the following operation arbitrary number of times (possibly, zero): choose some position in his string and replace the letter on this position with the other letter (i.e. replace 'a' with 'b' or replace 'b' with 'a'). Nikolay can use no letters except 'a' and 'b'.The prefix of string $$$s$$$ of length $$$l$$$ ($$$1 \le l \le n$$$) is a string $$$s[1..l]$$$.For example, for the string $$$s=$$$"abba" there are two prefixes of the even length. The first is $$$s[1\dots2]=$$$"ab" and the second $$$s[1\dots4]=$$$"abba". Both of them have the same number of 'a' and 'b'.Your task is to calculate the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'.
In the first line print the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'. In the second line print the string Nikolay obtains after applying all the operations. If there are multiple answers, you can print any of them.
C
8ad06ac90b258a8233e2a1cf51f68078
f40a4e51159532f003f0fc6a5d860f90
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "strings" ]
1569049500
["4\nbbbb", "6\nababab", "2\naa"]
NoteIn the first example Nikolay has to perform two operations. For example, he can replace the first 'b' with 'a' and the last 'b' with 'a'. In the second example Nikolay doesn't need to do anything because each prefix of an even length of the initial string already contains an equal amount of letters 'a' and 'b'.
PASSED
800
standard input
1 second
The first line of the input contains one even integer $$$n$$$ $$$(2 \le n \le 2\cdot10^{5})$$$ — the length of string $$$s$$$. The second line of the input contains the string $$$s$$$ of length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'.
["2\nabba", "0\nababab", "1\nba"]
#include<stdio.h> int main() { int n,i,j,c; scanf("%d",&j); char x[j]; scanf("%s",x); c=0; for(i=0;i<j;i=i+2) { if(x[i]=='a'&&x[i+1]=='a') { x[i+1]='b'; c++; } if(x[i]=='b'&&x[i+1]=='b') { x[i+1]='a'; c++; } } printf("%d\n",c); printf("%s",x); return 0; }
Nikolay got a string $$$s$$$ of even length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'. Its positions are numbered from $$$1$$$ to $$$n$$$.He wants to modify his string so that every its prefix of even length has an equal amount of letters 'a' and 'b'. To achieve that, Nikolay can perform the following operation arbitrary number of times (possibly, zero): choose some position in his string and replace the letter on this position with the other letter (i.e. replace 'a' with 'b' or replace 'b' with 'a'). Nikolay can use no letters except 'a' and 'b'.The prefix of string $$$s$$$ of length $$$l$$$ ($$$1 \le l \le n$$$) is a string $$$s[1..l]$$$.For example, for the string $$$s=$$$"abba" there are two prefixes of the even length. The first is $$$s[1\dots2]=$$$"ab" and the second $$$s[1\dots4]=$$$"abba". Both of them have the same number of 'a' and 'b'.Your task is to calculate the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'.
In the first line print the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'. In the second line print the string Nikolay obtains after applying all the operations. If there are multiple answers, you can print any of them.
C
8ad06ac90b258a8233e2a1cf51f68078
a3c76910bee8b0c3ddd5b6c0bbea60d0
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "strings" ]
1569049500
["4\nbbbb", "6\nababab", "2\naa"]
NoteIn the first example Nikolay has to perform two operations. For example, he can replace the first 'b' with 'a' and the last 'b' with 'a'. In the second example Nikolay doesn't need to do anything because each prefix of an even length of the initial string already contains an equal amount of letters 'a' and 'b'.
PASSED
800
standard input
1 second
The first line of the input contains one even integer $$$n$$$ $$$(2 \le n \le 2\cdot10^{5})$$$ — the length of string $$$s$$$. The second line of the input contains the string $$$s$$$ of length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'.
["2\nabba", "0\nababab", "1\nba"]
#include<stdio.h> #include<string.h> int main() { int n,i,c=0; char s[200000]; scanf("%d %s",&n,s); for(i=0;i<n;i+=2) { if(s[i]=='a' && s[i+1]=='a') { s[i]='b'; c++; } else if(s[i]=='b' && s[i+1]=='b') { s[i]='a'; c++; } } printf("%d\n%s",c,s); return 0; }
Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform n operations. Each operation is one of the following: Add the integer xi to the first ai elements of the sequence. Append an integer ki to the end of the sequence. (And hence the size of the sequence increases by 1) Remove the last element of the sequence. So, the size of the sequence decreases by one. Note, that this operation can only be done if there are at least two elements in the sequence. After each operation, the cows would like to know the average of all the numbers in the sequence. Help them!
Output n lines each containing the average of the numbers in the sequence after the corresponding operation. The answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6.
C
d43d4fd6c1e2722da185f34d902ace97
8132847917fec10b193d1353366c06ec
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "dp", "constructive algorithms", "data structures" ]
1363534200
["5\n2 1\n3\n2 3\n2 1\n3", "6\n2 1\n1 2 20\n2 2\n1 2 -3\n3\n3"]
NoteIn the second sample, the sequence becomes
PASSED
1,600
standard input
1.5 seconds
The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of operations. The next n lines describe the operations. Each line will start with an integer ti (1 ≤ ti ≤ 3), denoting the type of the operation (see above). If ti = 1, it will be followed by two integers ai, xi (|xi| ≤ 103; 1 ≤ ai). If ti = 2, it will be followed by a single integer ki (|ki| ≤ 103). If ti = 3, it will not be followed by anything. It is guaranteed that all operations are correct (don't touch nonexistent elements) and that there will always be at least one element in the sequence.
["0.500000\n0.000000\n1.500000\n1.333333\n1.500000", "0.500000\n20.500000\n14.333333\n12.333333\n17.500000\n17.000000"]
#include <stdio.h> #define MAXN 200005 int stk[MAXN]; int d[MAXN]; int main(int argc, char** argv) { int q; scanf(" %d", &q); long long tot = 0; int p = 1; for (int i = 0; i < q; ++i) { int t; scanf(" %d", &t); if (t == 1) { int x, v; scanf(" %d %d", &v, &x); tot += 1LL * x * v; d[v - 1] += x; } else if (t == 2) { scanf(" %d", &stk[p]); tot += stk[p]; ++p; } else { --p; tot -= stk[p] + d[p]; if (p > 0) { d[p - 1] += d[p]; } d[p] = 0; } printf("%.9f\n", ((double)tot) / p); } return 0; }
We call a string good, if after merging all the consecutive equal characters, the resulting string is palindrome. For example, "aabba" is good, because after the merging step it will become "aba".Given a string, you have to find two values: the number of good substrings of even length; the number of good substrings of odd length.
Print two space-separated integers: the number of good substrings of even length and the number of good substrings of odd length.
C
4ebbda2fc1a260e9827205a25addd9c4
02c6081ea19f57ed001bd3d4fdb51f07
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "math" ]
1406215800
["bb", "baab", "babb", "babaa"]
NoteIn example 1, there are three good substrings ("b", "b", and "bb"). One of them has even length and two of them have odd length.In example 2, there are six good substrings (i.e. "b", "a", "a", "b", "aa", "baab"). Two of them have even length and four of them have odd length.In example 3, there are seven good substrings (i.e. "b", "a", "b", "b", "bb", "bab", "babb"). Two of them have even length and five of them have odd length.DefinitionsA substring s[l, r] (1 ≤ l ≤ r ≤ n) of string s = s1s2... sn is string slsl + 1... sr.A string s = s1s2... sn is a palindrome if it is equal to string snsn - 1... s1.
PASSED
2,000
standard input
2 seconds
The first line of the input contains a single string of length n (1 ≤ n ≤ 105). Each character of the string will be either 'a' or 'b'.
["1 2", "2 4", "2 5", "2 7"]
#include<stdlib.h> #include<stdio.h> #include<string.h> typedef long long int lli; typedef struct bst bst; #define TRUE (1==1) #define FALSE (!(TRUE)) #define in32(x) scanf("%d", &(x)) #define in64(x) scanf("%I64d", &(x)) #define instr(x) scanf("%s", (x)) #define out32(x) printf("%d\n", (x)) #define out64(x) printf("%I64d\n", (x)) #define outstr(x) printf("%s\n", (x)) #define sigNUM(x) (((x)==0)?0:(((x)<0)?-1:1)) #define max(a,b) ((a)<(b))?(b):(a) #define min(a,b) ((a)>(b))?(b):(a) #define abs(a) ((a)<0)?(-(a)):(a) #define fo3(i,a,b) for(i=(int)(a);i<=(int)(b);i++) #define of3(i,a,b) for(i=(int)(a);i>=(int)(b);i--) void fastWrite(FILE * ofp, int x) { int i = 12; char buff[i]; buff[i] = 0; int neg = 0; if (x < 0){ x = -x; neg = 1; } if (x == 0) buff[--i] = '0'; else while (x != 0) { buff[--i] = (x % 10) + '0'; x /= 10; } if (neg) buff[--i] = '-'; fwrite(buff + i, sizeof(char), (12 - i), ofp); } void fastSpace(FILE * ofp){ fwrite(" ", sizeof(char), 1, ofp); } void fastNewLine(FILE * ofp) { fwrite("\n", sizeof(char), 1, ofp); } int comp(lli f, lli s) { return sigNUM(f - s); } void sort(lli * arr, int size) { // CHANGE THIS int i; if (size < 6) { int nSize = 0; while (size != 0) { nSize = 0; for (i = 0; i < size - 1; i++) if (comp(arr[i], arr[i + 1]) > 0) { // CHANGE THIS nSize = i + 1; lli tmp = arr[i]; arr[i] = arr[i + 1]; arr[i+1] = tmp; } size = nSize; } return; } lli * tmp = (lli *)calloc(size, sizeof(lli)); // CHANGE THIS int lSize = (size >> 1); int rSize = size - lSize; sort(arr, lSize); sort(arr + lSize, rSize); int lPtr = 0, rPtr = 0; int ptr = 0; while (ptr != size) { if (lPtr == lSize) tmp[ptr] = arr[lSize + (rPtr++)]; else if (rPtr == rSize || comp(arr[lPtr], arr[lSize + rPtr]) <= 0) // CHANGE THIS tmp[ptr] = arr[lPtr++]; else tmp[ptr] = arr[lSize + (rPtr++)]; ptr++; } for (i = 0; i < size; i++) arr[i] = tmp[i]; free(tmp); } #define MAX 100010 #define MOD 1000000007 int main(void) { lli n, a[2][2], i, j, ans[2]; char str[MAX]; instr(str); n = strlen(str); fo3(i,0,1) { ans[i] = 0; fo3(j,0,1) a[j][i] = 0; } fo3(i,0,n-1) { a[str[i]-'a'][i&1]++; fo3(j,0,1) ans[j] += a[str[i]-'a'][j^i&1]; } printf("%I64d %I64d\n", ans[1], ans[0]); return 0; }
We call a string good, if after merging all the consecutive equal characters, the resulting string is palindrome. For example, "aabba" is good, because after the merging step it will become "aba".Given a string, you have to find two values: the number of good substrings of even length; the number of good substrings of odd length.
Print two space-separated integers: the number of good substrings of even length and the number of good substrings of odd length.
C
4ebbda2fc1a260e9827205a25addd9c4
3da9b8ecd475df37a3bb8e9461306958
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "math" ]
1406215800
["bb", "baab", "babb", "babaa"]
NoteIn example 1, there are three good substrings ("b", "b", and "bb"). One of them has even length and two of them have odd length.In example 2, there are six good substrings (i.e. "b", "a", "a", "b", "aa", "baab"). Two of them have even length and four of them have odd length.In example 3, there are seven good substrings (i.e. "b", "a", "b", "b", "bb", "bab", "babb"). Two of them have even length and five of them have odd length.DefinitionsA substring s[l, r] (1 ≤ l ≤ r ≤ n) of string s = s1s2... sn is string slsl + 1... sr.A string s = s1s2... sn is a palindrome if it is equal to string snsn - 1... s1.
PASSED
2,000
standard input
2 seconds
The first line of the input contains a single string of length n (1 ≤ n ≤ 105). Each character of the string will be either 'a' or 'b'.
["1 2", "2 4", "2 5", "2 7"]
/* practice with Dukkha */ #include <stdio.h> #include <string.h> #define N 100000 int main() { static char cc[N + 1]; int n, i, a0, a1, b0, b1; long long ao, ae, bo, be, e, o; scanf("%s", cc), n = strlen(cc); a0 = a1 = b0 = b1 = 0; for (i = 0; i < n; i++) if (cc[i] == 'a') { if (i % 2 == 0) a0++; else a1++; } else { if (i % 2 == 0) b0++; else b1++; } ao = (long long) a0 * (a0 + 1) / 2 + (long long) a1 * (a1 + 1) / 2; bo = (long long) b0 * (b0 + 1) / 2 + (long long) b1 * (b1 + 1) / 2; ae = (long long) (a0 + a1) * (a0 + a1 + 1) / 2 - ao; be = (long long) (b0 + b1) * (b0 + b1 + 1) / 2 - bo; e = ae + be; o = ao + bo; printf("%lld %lld\n", e, o); return 0; }
Berland SU holds yet another training contest for its students today. $$$n$$$ students came, each of them brought his laptop. However, it turned out that everyone has forgot their chargers!Let students be numbered from $$$1$$$ to $$$n$$$. Laptop of the $$$i$$$-th student has charge $$$a_i$$$ at the beginning of the contest and it uses $$$b_i$$$ of charge per minute (i.e. if the laptop has $$$c$$$ charge at the beginning of some minute, it becomes $$$c - b_i$$$ charge at the beginning of the next minute). The whole contest lasts for $$$k$$$ minutes.Polycarp (the coach of Berland SU) decided to buy a single charger so that all the students would be able to successfully finish the contest. He buys the charger at the same moment the contest starts.Polycarp can choose to buy the charger with any non-negative (zero or positive) integer power output. The power output is chosen before the purchase, it can't be changed afterwards. Let the chosen power output be $$$x$$$. At the beginning of each minute (from the minute contest starts to the last minute of the contest) he can plug the charger into any of the student's laptops and use it for some integer number of minutes. If the laptop is using $$$b_i$$$ charge per minute then it will become $$$b_i - x$$$ per minute while the charger is plugged in. Negative power usage rate means that the laptop's charge is increasing. The charge of any laptop isn't limited, it can become infinitely large. The charger can be plugged in no more than one laptop at the same time.The student successfully finishes the contest if the charge of his laptop never is below zero at the beginning of some minute (from the minute contest starts to the last minute of the contest, zero charge is allowed). The charge of the laptop of the minute the contest ends doesn't matter.Help Polycarp to determine the minimal possible power output the charger should have so that all the students are able to successfully finish the contest. Also report if no such charger exists.
Print a single non-negative integer — the minimal possible power output the charger should have so that all the students are able to successfully finish the contest. If no such charger exists, print -1.
C
6dee11f19439e5f437604bc61257c8a5
df29aabcb3a1cc6c906831b71887c21b
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "binary search", "greedy" ]
1551798300
["2 4\n3 2\n4 2", "1 5\n4\n2", "1 6\n4\n2", "2 2\n2 10\n3 15"]
NoteLet's take a look at the state of laptops in the beginning of each minute on the first example with the charger of power $$$5$$$: charge: $$$[3, 2]$$$, plug the charger into laptop 1; charge: $$$[3 - 4 + 5, 2 - 2] = [4, 0]$$$, plug the charger into laptop 2; charge: $$$[4 - 4, 0 - 2 + 5] = [0, 3]$$$, plug the charger into laptop 1; charge: $$$[0 - 4 + 5, 3 - 2] = [1, 1]$$$. The contest ends after the fourth minute.However, let's consider the charger of power $$$4$$$: charge: $$$[3, 2]$$$, plug the charger into laptop 1; charge: $$$[3 - 4 + 4, 2 - 2] = [3, 0]$$$, plug the charger into laptop 2; charge: $$$[3 - 4, 0 - 2 + 4] = [-1, 2]$$$, the first laptop has negative charge, thus, the first student doesn't finish the contest. In the fourth example no matter how powerful the charger is, one of the students won't finish the contest.
PASSED
2,300
standard input
3 seconds
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$1 \le k \le 2 \cdot 10^5$$$) — the number of students (and laptops, correspondigly) and the duration of the contest in minutes. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^{12}$$$) — the initial charge of each student's laptop. The third line contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le 10^7$$$) — the power usage of each student's laptop.
["5", "1", "2", "-1"]
#include <stdio.h> #include <stdlib.h> #define maxn 200001 typedef struct laptop_tag { long long power, change; } laptop_tag; typedef struct heap_tag { int sz; int elements[maxn]; } heap_tag; int n, k; long long value[maxn], valuea[maxn]; laptop_tag arr[maxn], brr[maxn]; heap_tag heap, cheap; int cmp(int x, int y) { long long a = value[x]; long long b = value[y]; long long tmp = a - b; if (tmp > 0) return 1; return 0; } void swap(int *a, int *b) { int tmp = *a; *a = *b; *b = tmp; } void make_heap(heap_tag *heap) { heap->sz = 0; } void up_heap(heap_tag *heap, int id) { int n = heap->sz++; heap->elements[n] = id; while (n) { int parent = (n - 1) >> 1; if (cmp(heap->elements[parent], heap->elements[n]) > 0) { swap(&heap->elements[parent], &heap->elements[n]); } else break; n = parent; } } int get_min(heap_tag *heap) { return heap->elements[0]; } int get_min2(heap_tag *heap) { return (heap->sz == 2) ? (heap->elements[1]) : (cmp(heap->elements[2], heap->elements[1]) > 0 ? heap->elements[1]: heap->elements[2]); } void down_heap(heap_tag *heap, int id) { int n = heap->sz; heap->elements[0] = id; int parent = 0; while (((parent << 1) | 1) < n) { int child = (parent << 1) | 1; if (child + 1 < n && cmp(heap->elements[child], heap->elements[child + 1]) > 0) child++; if (cmp(heap->elements[parent], heap->elements[child]) > 0) { swap(&heap->elements[parent], &heap->elements[child]); parent = child; } else { break; } } } void init() { make_heap(&cheap); for (int i = 0; i < n; ++i) { valuea[i] = value[i] = brr[i].power / brr[i].change; up_heap(&cheap, i); } } int check(long long p) { if (n == 1) return (brr[0].power + (k - 1)*p) / brr[0].change >= k - 1; heap = cheap; for (int i = 0; i < n; ++i) { arr[i] = brr[i]; value[i] = valuea[i]; } for (int i = 1; i < k; ++i) { int rlap = get_min(&heap); int clap = get_min2(&heap); if (value[clap] < i) return 0; arr[rlap].power += p; value[rlap] = arr[rlap].power / arr[rlap].change; if (value[rlap] < i) return 0; down_heap(&heap, rlap); } return 1; } long long solve() { long long ans = -1; long long l = 0, r = (long long)1e13; while (l <= r) { long long m = (l + r) >> 1; if (check(m)) { if (ans == -1 || ans > m) ans = m; r = m - 1; } else { l = m + 1; } } return ans; } int main() { scanf("%d%d", &n, &k); for (int i = 0; i < n; ++i) { scanf("%lld", &brr[i].power); } for (int i = 0; i < n; ++i) { scanf("%lld", &brr[i].change); } init(); printf("%lld\n", solve()); return 0; }
Berland SU holds yet another training contest for its students today. $$$n$$$ students came, each of them brought his laptop. However, it turned out that everyone has forgot their chargers!Let students be numbered from $$$1$$$ to $$$n$$$. Laptop of the $$$i$$$-th student has charge $$$a_i$$$ at the beginning of the contest and it uses $$$b_i$$$ of charge per minute (i.e. if the laptop has $$$c$$$ charge at the beginning of some minute, it becomes $$$c - b_i$$$ charge at the beginning of the next minute). The whole contest lasts for $$$k$$$ minutes.Polycarp (the coach of Berland SU) decided to buy a single charger so that all the students would be able to successfully finish the contest. He buys the charger at the same moment the contest starts.Polycarp can choose to buy the charger with any non-negative (zero or positive) integer power output. The power output is chosen before the purchase, it can't be changed afterwards. Let the chosen power output be $$$x$$$. At the beginning of each minute (from the minute contest starts to the last minute of the contest) he can plug the charger into any of the student's laptops and use it for some integer number of minutes. If the laptop is using $$$b_i$$$ charge per minute then it will become $$$b_i - x$$$ per minute while the charger is plugged in. Negative power usage rate means that the laptop's charge is increasing. The charge of any laptop isn't limited, it can become infinitely large. The charger can be plugged in no more than one laptop at the same time.The student successfully finishes the contest if the charge of his laptop never is below zero at the beginning of some minute (from the minute contest starts to the last minute of the contest, zero charge is allowed). The charge of the laptop of the minute the contest ends doesn't matter.Help Polycarp to determine the minimal possible power output the charger should have so that all the students are able to successfully finish the contest. Also report if no such charger exists.
Print a single non-negative integer — the minimal possible power output the charger should have so that all the students are able to successfully finish the contest. If no such charger exists, print -1.
C
6dee11f19439e5f437604bc61257c8a5
5453615f531df8bfce4590626194a593
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "binary search", "greedy" ]
1551798300
["2 4\n3 2\n4 2", "1 5\n4\n2", "1 6\n4\n2", "2 2\n2 10\n3 15"]
NoteLet's take a look at the state of laptops in the beginning of each minute on the first example with the charger of power $$$5$$$: charge: $$$[3, 2]$$$, plug the charger into laptop 1; charge: $$$[3 - 4 + 5, 2 - 2] = [4, 0]$$$, plug the charger into laptop 2; charge: $$$[4 - 4, 0 - 2 + 5] = [0, 3]$$$, plug the charger into laptop 1; charge: $$$[0 - 4 + 5, 3 - 2] = [1, 1]$$$. The contest ends after the fourth minute.However, let's consider the charger of power $$$4$$$: charge: $$$[3, 2]$$$, plug the charger into laptop 1; charge: $$$[3 - 4 + 4, 2 - 2] = [3, 0]$$$, plug the charger into laptop 2; charge: $$$[3 - 4, 0 - 2 + 4] = [-1, 2]$$$, the first laptop has negative charge, thus, the first student doesn't finish the contest. In the fourth example no matter how powerful the charger is, one of the students won't finish the contest.
PASSED
2,300
standard input
3 seconds
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$1 \le k \le 2 \cdot 10^5$$$) — the number of students (and laptops, correspondigly) and the duration of the contest in minutes. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^{12}$$$) — the initial charge of each student's laptop. The third line contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le 10^7$$$) — the power usage of each student's laptop.
["5", "1", "2", "-1"]
#include <stdio.h> #include <stdlib.h> #include <sys/time.h> #define N 200000 #define K 200000 #define INF (K * 10000000LL + 1) void init_rand() { struct timeval tv; gettimeofday(&tv, NULL); srand(tv.tv_sec ^ tv.tv_usec); } struct P { long long a, b; } pp[N]; int compare(const void *a, const void *b) { int ia = *(int *) a; int ib = *(int *) b; return ia - ib; } int solve(int n, int k, long long x) { static int tt[K]; int cnt, i, j, tmp; cnt = 0; for (i = 0; i < n; i++) { long long a = pp[i].a, b = pp[i].b, t; while ((t = a / b + 1) < k) { if (cnt == k) return 0; tt[cnt++] = t; a += x; } } for (j = cnt - 1; j >= 0; j--) { i = rand() % (j + 1); tmp = tt[i], tt[i] = tt[j], tt[j] = tmp; } qsort(tt, cnt, sizeof *tt, compare); for (i = 0; i < cnt; i++) if (tt[i] <= i) return 0; return 1; } int main() { int n, k, i; long long lower, upper, x; init_rand(); scanf("%d%d", &n, &k); for (i = 0; i < n; i++) scanf("%lld", &pp[i].a); for (i = 0; i < n; i++) scanf("%lld", &pp[i].b); lower = -1, upper = INF; while (upper - lower > 1) { x = (lower + upper) / 2; if (solve(n, k, x)) upper = x; else lower = x; } printf("%lld\n", upper == INF ? -1 : upper); return 0; }
Berland SU holds yet another training contest for its students today. $$$n$$$ students came, each of them brought his laptop. However, it turned out that everyone has forgot their chargers!Let students be numbered from $$$1$$$ to $$$n$$$. Laptop of the $$$i$$$-th student has charge $$$a_i$$$ at the beginning of the contest and it uses $$$b_i$$$ of charge per minute (i.e. if the laptop has $$$c$$$ charge at the beginning of some minute, it becomes $$$c - b_i$$$ charge at the beginning of the next minute). The whole contest lasts for $$$k$$$ minutes.Polycarp (the coach of Berland SU) decided to buy a single charger so that all the students would be able to successfully finish the contest. He buys the charger at the same moment the contest starts.Polycarp can choose to buy the charger with any non-negative (zero or positive) integer power output. The power output is chosen before the purchase, it can't be changed afterwards. Let the chosen power output be $$$x$$$. At the beginning of each minute (from the minute contest starts to the last minute of the contest) he can plug the charger into any of the student's laptops and use it for some integer number of minutes. If the laptop is using $$$b_i$$$ charge per minute then it will become $$$b_i - x$$$ per minute while the charger is plugged in. Negative power usage rate means that the laptop's charge is increasing. The charge of any laptop isn't limited, it can become infinitely large. The charger can be plugged in no more than one laptop at the same time.The student successfully finishes the contest if the charge of his laptop never is below zero at the beginning of some minute (from the minute contest starts to the last minute of the contest, zero charge is allowed). The charge of the laptop of the minute the contest ends doesn't matter.Help Polycarp to determine the minimal possible power output the charger should have so that all the students are able to successfully finish the contest. Also report if no such charger exists.
Print a single non-negative integer — the minimal possible power output the charger should have so that all the students are able to successfully finish the contest. If no such charger exists, print -1.
C
6dee11f19439e5f437604bc61257c8a5
9f833551c5e0a1a53588c9dc8e29f8af
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "binary search", "greedy" ]
1551798300
["2 4\n3 2\n4 2", "1 5\n4\n2", "1 6\n4\n2", "2 2\n2 10\n3 15"]
NoteLet's take a look at the state of laptops in the beginning of each minute on the first example with the charger of power $$$5$$$: charge: $$$[3, 2]$$$, plug the charger into laptop 1; charge: $$$[3 - 4 + 5, 2 - 2] = [4, 0]$$$, plug the charger into laptop 2; charge: $$$[4 - 4, 0 - 2 + 5] = [0, 3]$$$, plug the charger into laptop 1; charge: $$$[0 - 4 + 5, 3 - 2] = [1, 1]$$$. The contest ends after the fourth minute.However, let's consider the charger of power $$$4$$$: charge: $$$[3, 2]$$$, plug the charger into laptop 1; charge: $$$[3 - 4 + 4, 2 - 2] = [3, 0]$$$, plug the charger into laptop 2; charge: $$$[3 - 4, 0 - 2 + 4] = [-1, 2]$$$, the first laptop has negative charge, thus, the first student doesn't finish the contest. In the fourth example no matter how powerful the charger is, one of the students won't finish the contest.
PASSED
2,300
standard input
3 seconds
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$1 \le k \le 2 \cdot 10^5$$$) — the number of students (and laptops, correspondigly) and the duration of the contest in minutes. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^{12}$$$) — the initial charge of each student's laptop. The third line contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le 10^7$$$) — the power usage of each student's laptop.
["5", "1", "2", "-1"]
#include<stdio.h> #include<stdlib.h> #include<stdint.h> #include<inttypes.h> #include<string.h> typedef int64_t i64; typedef int32_t i32; static void print_int(i64 n){if(n<0){putchar('-');n=-n;}if(n==0){putchar('0');return;}int s[20],len=0;while(n>0){s[len++]=n%10+'0';n/=10;}while(len>0){putchar(s[--len]);}} static i64 read_int(void){int prev='\0';int c=getchar();while(!('0'<=c && c<='9')){prev=c;c=getchar();}i64 res=0;while('0'<=c && c<='9'){res=10*res+c-'0';c=getchar();}return prev=='-'?-res:res;} int can (i64 m, i64 *a, i32 *b, i32 n, i32 *dp, i32 k) { memset (dp, 0, sizeof (i32) * (k + 1)); k--; i32 r = k; for (i32 i = 0; i < n; ++i) { i64 t = a[i]; while (t / b[i] + 1 <= k && r >= 0) { dp[t / b[i] + 1]++; t += m; r--; } if (r < 0) return 0; } for (i32 i = 1; i <= k; ++i) { dp[i] += dp[i - 1]; if (dp[i] > i) return 0; } return 1; } void run (void) { i32 n = read_int(); i32 k = read_int(); i64 *a = (i64 *) calloc (n, sizeof (i64)); for (i32 i = 0; i < n; ++i) { a[i] = read_int(); } i32 *b = (i32 *) calloc (n, sizeof (i32)); for (i32 i = 0; i < n; ++i) { b[i] = read_int(); } i32 *dp = (i32 *) calloc (k + 1, sizeof (i32)); i64 l = -1; i64 r = (i64) k * 10000000; if (!can (r, a, b, n, dp, k)) { puts("-1"); return; } while (r - l > 1) { i64 m = (l + r) / 2; if (can (m, a, b, n, dp, k)) { r = m; } else { l = m; } } print_int (r); puts(""); } int main (void) { run (); return 0; }
Berland SU holds yet another training contest for its students today. $$$n$$$ students came, each of them brought his laptop. However, it turned out that everyone has forgot their chargers!Let students be numbered from $$$1$$$ to $$$n$$$. Laptop of the $$$i$$$-th student has charge $$$a_i$$$ at the beginning of the contest and it uses $$$b_i$$$ of charge per minute (i.e. if the laptop has $$$c$$$ charge at the beginning of some minute, it becomes $$$c - b_i$$$ charge at the beginning of the next minute). The whole contest lasts for $$$k$$$ minutes.Polycarp (the coach of Berland SU) decided to buy a single charger so that all the students would be able to successfully finish the contest. He buys the charger at the same moment the contest starts.Polycarp can choose to buy the charger with any non-negative (zero or positive) integer power output. The power output is chosen before the purchase, it can't be changed afterwards. Let the chosen power output be $$$x$$$. At the beginning of each minute (from the minute contest starts to the last minute of the contest) he can plug the charger into any of the student's laptops and use it for some integer number of minutes. If the laptop is using $$$b_i$$$ charge per minute then it will become $$$b_i - x$$$ per minute while the charger is plugged in. Negative power usage rate means that the laptop's charge is increasing. The charge of any laptop isn't limited, it can become infinitely large. The charger can be plugged in no more than one laptop at the same time.The student successfully finishes the contest if the charge of his laptop never is below zero at the beginning of some minute (from the minute contest starts to the last minute of the contest, zero charge is allowed). The charge of the laptop of the minute the contest ends doesn't matter.Help Polycarp to determine the minimal possible power output the charger should have so that all the students are able to successfully finish the contest. Also report if no such charger exists.
Print a single non-negative integer — the minimal possible power output the charger should have so that all the students are able to successfully finish the contest. If no such charger exists, print -1.
C
6dee11f19439e5f437604bc61257c8a5
7faabdc659e70078f4c249c9cedff56b
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "binary search", "greedy" ]
1551798300
["2 4\n3 2\n4 2", "1 5\n4\n2", "1 6\n4\n2", "2 2\n2 10\n3 15"]
NoteLet's take a look at the state of laptops in the beginning of each minute on the first example with the charger of power $$$5$$$: charge: $$$[3, 2]$$$, plug the charger into laptop 1; charge: $$$[3 - 4 + 5, 2 - 2] = [4, 0]$$$, plug the charger into laptop 2; charge: $$$[4 - 4, 0 - 2 + 5] = [0, 3]$$$, plug the charger into laptop 1; charge: $$$[0 - 4 + 5, 3 - 2] = [1, 1]$$$. The contest ends after the fourth minute.However, let's consider the charger of power $$$4$$$: charge: $$$[3, 2]$$$, plug the charger into laptop 1; charge: $$$[3 - 4 + 4, 2 - 2] = [3, 0]$$$, plug the charger into laptop 2; charge: $$$[3 - 4, 0 - 2 + 4] = [-1, 2]$$$, the first laptop has negative charge, thus, the first student doesn't finish the contest. In the fourth example no matter how powerful the charger is, one of the students won't finish the contest.
PASSED
2,300
standard input
3 seconds
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$1 \le k \le 2 \cdot 10^5$$$) — the number of students (and laptops, correspondigly) and the duration of the contest in minutes. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^{12}$$$) — the initial charge of each student's laptop. The third line contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le 10^7$$$) — the power usage of each student's laptop.
["5", "1", "2", "-1"]
#include<stdio.h> #include<stdlib.h> #include<stdint.h> #include<inttypes.h> typedef int64_t i64; typedef int32_t i32; static void print_int(i64 n){if(n<0){putchar('-');n=-n;}if(n==0){putchar('0');return;}int s[20],len=0;while(n>0){s[len++]=n%10+'0';n/=10;}while(len>0){putchar(s[--len]);}} static i64 read_int(void){int prev='\0';int c=getchar();while(!('0'<=c && c<='9')){prev=c;c=getchar();}i64 res=0;while('0'<=c && c<='9'){res=10*res+c-'0';c=getchar();}return prev=='-'?-res:res;} typedef struct binaryHeap{ void *array; size_t heap_size; size_t max_size; size_t val_size; int (*cmp) (const void *, const void *); } heap; heap* new_binary_heap (const size_t val_size, int (*cmpF) (const void *, const void *)) { heap *h = (heap *) calloc (1, sizeof (heap)); h->array = malloc (val_size * (1 + 1)); h->heap_size = 0; h->max_size = 1; h->val_size = val_size; h->cmp = cmpF; return h; } int32_t get_heap_size (const heap *h) { return h->heap_size; } int is_empty (const heap *h) { return h->heap_size == 0; } void free_heap (heap *h) { free (h->array); free (h); } void init_heap (heap *h) { h->heap_size = 0; } static void heap_func_swap (void * restrict a, void * restrict b, size_t val_size) { if ((val_size & 7) == 0) { uint64_t *p = (uint64_t *) a; uint64_t *q = (uint64_t *) b; val_size /= sizeof (uint64_t); while (val_size--) { uint64_t tmp = *p; *p++ = *q; *q++ = tmp; } } else { uint8_t *p = (uint8_t *) a; uint8_t *q = (uint8_t *) b; while (val_size--) { uint8_t tmp = *p; *p++ = *q; *q++ = tmp; } } } static void heap_func_copy (void * restrict dst, const void * restrict src, size_t val_size) { if ((val_size & 7) == 0) { uint64_t *p = (uint64_t *) src; uint64_t *q = (uint64_t *) dst; val_size /= sizeof (uint64_t); while (val_size--) { *q++ = *p++; } } else { uint8_t *p = (uint8_t *) src; uint8_t *q = (uint8_t *) dst; while (val_size--) { *q++ = *p++; } } } void push (heap *h, const void *val) { if (h->heap_size == h->max_size) { h->max_size = 2 * h->max_size + 1; h->array = realloc (h->array, h->val_size * (h->max_size + 1)); } h->heap_size++; uint8_t *array = (uint8_t *) h->array; size_t k = h->heap_size; const size_t val_size = h->val_size; int (*cmp) (const void *, const void *) = h->cmp; heap_func_copy(array + k * val_size, val, val_size); while(k>1){ size_t parent = k / 2; if (cmp (array + parent * val_size, array + k * val_size) <= 0) { return; } heap_func_swap (array + parent * val_size, array + k * val_size, val_size); k = parent; } } void pop (heap *h, void *res) { uint8_t *array = (uint8_t *) h->array; const size_t val_size = h->val_size; if (res != NULL) { heap_func_copy (res, array + val_size, val_size); } heap_func_copy (array + val_size, array + val_size * h->heap_size, val_size); h->heap_size--; int (* const cmp) (const void *, const void *) = h->cmp; const size_t n = h->heap_size; size_t k = 1; while (2 * k + 1 <= n) { int c = cmp (array + val_size * 2 * k, array + val_size * (2 * k + 1)); size_t next = 2 * k + (c <= 0 ? 0 : 1); if (cmp (array + val_size * k, array + val_size * next) <= 0) return; heap_func_swap (array + val_size * k, array + val_size * next, val_size); k = next; } if (2 * k <= n && cmp (array + val_size * k, array + val_size * 2 * k) > 0) { heap_func_swap (array + val_size * k, array + val_size * 2 * k, val_size); } } typedef struct node { i32 t; i32 index; } node; int cmp_node (const void *a, const void *b) { node *p = (node *) a; node *q = (node *) b; i32 d = p->t - q->t; return d == 0 ? 0 : d < 0 ? -1 : 1; } int can (i64 m, const i64 *a, const i64 *b, i32 n, i32 k) { static heap *h = NULL; static i64 *add = NULL; if (h == NULL) { h = new_binary_heap (sizeof (node), cmp_node); add = (i64 *) calloc (n, sizeof (i64)); } init_heap (h); for (i32 i = 0; i < n; ++i) { push (h, &((node){a[i] / b[i] >= k ? k + 1 : a[i] / b[i], i})); add[i] = 0; } for (i32 i = 0; i < k; ++i) { node t; pop (h, &t); i64 p = a[t.index] + add[t.index]; i64 q = b[t.index]; if (p - i * q < 0) return 0; add[t.index] += m; push (h, &((node){(a[t.index] + add[t.index]) / b[t.index] >= k ? k : (a[t.index] + add[t.index]) / b[t.index], t.index})); } return 1; } void run (void) { const i32 n = read_int(); const i32 k = read_int(); i64 *a = (i64 *) calloc (n * 2, sizeof (i64)); i64 *b = a + n; for (i32 i = 0; i < 2 * n; ++i) { a[i] = read_int(); } i64 max = (i64) k * 10000000; if (!can (max, a, b, n, k)) { puts("-1"); return; } i64 l = -1; i64 r = max; while (r - l > 1) { i64 m = (l + r) / 2; if (can (m, a, b, n, k)) { r = m; } else { l = m; } } print_int (r); puts(""); } int main (void) { run (); return 0; }
Today there is going to be an unusual performance at the circus — hamsters and tigers will perform together! All of them stand in circle along the arena edge and now the trainer faces a difficult task: he wants to swap the animals' positions so that all the hamsters stood together and all the tigers also stood together. The trainer swaps the animals in pairs not to create a mess. He orders two animals to step out of the circle and swap places. As hamsters feel highly uncomfortable when tigers are nearby as well as tigers get nervous when there's so much potential prey around (consisting not only of hamsters but also of yummier spectators), the trainer wants to spend as little time as possible moving the animals, i.e. he wants to achieve it with the minimal number of swaps. Your task is to help him.
Print the single number which is the minimal number of swaps that let the trainer to achieve his goal.
C
0fce263a9606fbfc3ec912894ab1a8f8
ae8d76dcf9b279ea5b675a6103d0f664
GNU C
standard output
256 megabytes
train_002.jsonl
[ "two pointers" ]
1291536000
["3\nHTH", "9\nHTHTHTHHT"]
NoteIn the first example we shouldn't move anybody because the animals of each species already stand apart from the other species. In the second example you may swap, for example, the tiger in position 2 with the hamster in position 5 and then — the tiger in position 9 with the hamster in position 7.
PASSED
1,600
standard input
2 seconds
The first line contains number n (2 ≤ n ≤ 1000) which indicates the total number of animals in the arena. The second line contains the description of the animals' positions. The line consists of n symbols "H" and "T". The "H"s correspond to hamsters and the "T"s correspond to tigers. It is guaranteed that at least one hamster and one tiger are present on the arena. The animals are given in the order in which they are located circle-wise, in addition, the last animal stands near the first one.
["0", "2"]
#include <stdio.h> int main() { int n, min = 10000, i; char s[2001]; scanf("%d", &n); scanf("%s", s); for (i = 0; i < n; i++) { int p = i, q, sum = 0; for (q = n + i - 1; q >= p; q--) { if (s[q] != s[i]) break; } while (1) { for (; p < q; p++) { if (s[p] != s[i]) break; } for (; q > p; q--) { if (s[q] == s[i]) break; } if (p < q) { p++; q--; sum++; } else { break; } } if (sum < min) min = sum; s[n + i] = s[i]; } printf("%d\n", min); return 0; }
Today there is going to be an unusual performance at the circus — hamsters and tigers will perform together! All of them stand in circle along the arena edge and now the trainer faces a difficult task: he wants to swap the animals' positions so that all the hamsters stood together and all the tigers also stood together. The trainer swaps the animals in pairs not to create a mess. He orders two animals to step out of the circle and swap places. As hamsters feel highly uncomfortable when tigers are nearby as well as tigers get nervous when there's so much potential prey around (consisting not only of hamsters but also of yummier spectators), the trainer wants to spend as little time as possible moving the animals, i.e. he wants to achieve it with the minimal number of swaps. Your task is to help him.
Print the single number which is the minimal number of swaps that let the trainer to achieve his goal.
C
0fce263a9606fbfc3ec912894ab1a8f8
1518a5bead1c78d233036ddbf218cffa
GNU C
standard output
256 megabytes
train_002.jsonl
[ "two pointers" ]
1291536000
["3\nHTH", "9\nHTHTHTHHT"]
NoteIn the first example we shouldn't move anybody because the animals of each species already stand apart from the other species. In the second example you may swap, for example, the tiger in position 2 with the hamster in position 5 and then — the tiger in position 9 with the hamster in position 7.
PASSED
1,600
standard input
2 seconds
The first line contains number n (2 ≤ n ≤ 1000) which indicates the total number of animals in the arena. The second line contains the description of the animals' positions. The line consists of n symbols "H" and "T". The "H"s correspond to hamsters and the "T"s correspond to tigers. It is guaranteed that at least one hamster and one tiger are present on the arena. The animals are given in the order in which they are located circle-wise, in addition, the last animal stands near the first one.
["0", "2"]
#include <stdio.h> #define gmin(a,b) ((a)<(b)?(a):(b)) int main() { long n,i,tot=0,op,j,min; char ch[2005]={'\0'}; scanf("%ld\n",&n); for(i=1;i<=n;i++) { scanf("%c",&ch[i]); if(ch[i]=='H') tot++; } min=1000000000; for(i=1;i<n;i++) ch[i+n]=ch[i]; for(i=1;i<=n;i++) { for(j=i,op=0;j<=i+tot-1;j++) if(ch[j]=='H') op++; min=gmin(min,tot-op); } printf("%ld\n",min); return 0; }
Today there is going to be an unusual performance at the circus — hamsters and tigers will perform together! All of them stand in circle along the arena edge and now the trainer faces a difficult task: he wants to swap the animals' positions so that all the hamsters stood together and all the tigers also stood together. The trainer swaps the animals in pairs not to create a mess. He orders two animals to step out of the circle and swap places. As hamsters feel highly uncomfortable when tigers are nearby as well as tigers get nervous when there's so much potential prey around (consisting not only of hamsters but also of yummier spectators), the trainer wants to spend as little time as possible moving the animals, i.e. he wants to achieve it with the minimal number of swaps. Your task is to help him.
Print the single number which is the minimal number of swaps that let the trainer to achieve his goal.
C
0fce263a9606fbfc3ec912894ab1a8f8
902e30c819e7423f6beac99f7b954c06
GNU C
standard output
256 megabytes
train_002.jsonl
[ "two pointers" ]
1291536000
["3\nHTH", "9\nHTHTHTHHT"]
NoteIn the first example we shouldn't move anybody because the animals of each species already stand apart from the other species. In the second example you may swap, for example, the tiger in position 2 with the hamster in position 5 and then — the tiger in position 9 with the hamster in position 7.
PASSED
1,600
standard input
2 seconds
The first line contains number n (2 ≤ n ≤ 1000) which indicates the total number of animals in the arena. The second line contains the description of the animals' positions. The line consists of n symbols "H" and "T". The "H"s correspond to hamsters and the "T"s correspond to tigers. It is guaranteed that at least one hamster and one tiger are present on the arena. The animals are given in the order in which they are located circle-wise, in addition, the last animal stands near the first one.
["0", "2"]
#include <stdio.h> #define oo 200000000 char str[1006]={'\0'}; long s1=0,s2=0; int main() { long n,j; long i; long min=oo; long s=0; scanf("%ld",&n); scanf("%s",str+1); for(i=1;i<=n;i++) { if(str[i]=='H') s1++; } s2=n-s1; if(s1==0||s2==0) { printf("0\n"); return 0; } for(i=1;i<=s1;i++) { s=0; for(j=1;j<=n;j++) if(str[j]=='T'&&(j<=i||j>i+s2)) s++; if(s<min) min=s; } for(i=1;i<=s2;i++) { s=0; for(j=1;j<=n;j++) if(str[j]=='H'&&(j<=i||j>i+s1)) s++; if(s<min) min=s; } printf("%ld\n",min); return 0; }
Today there is going to be an unusual performance at the circus — hamsters and tigers will perform together! All of them stand in circle along the arena edge and now the trainer faces a difficult task: he wants to swap the animals' positions so that all the hamsters stood together and all the tigers also stood together. The trainer swaps the animals in pairs not to create a mess. He orders two animals to step out of the circle and swap places. As hamsters feel highly uncomfortable when tigers are nearby as well as tigers get nervous when there's so much potential prey around (consisting not only of hamsters but also of yummier spectators), the trainer wants to spend as little time as possible moving the animals, i.e. he wants to achieve it with the minimal number of swaps. Your task is to help him.
Print the single number which is the minimal number of swaps that let the trainer to achieve his goal.
C
0fce263a9606fbfc3ec912894ab1a8f8
81febeb7d91c73a32a648523af19cf1c
GNU C
standard output
256 megabytes
train_002.jsonl
[ "two pointers" ]
1291536000
["3\nHTH", "9\nHTHTHTHHT"]
NoteIn the first example we shouldn't move anybody because the animals of each species already stand apart from the other species. In the second example you may swap, for example, the tiger in position 2 with the hamster in position 5 and then — the tiger in position 9 with the hamster in position 7.
PASSED
1,600
standard input
2 seconds
The first line contains number n (2 ≤ n ≤ 1000) which indicates the total number of animals in the arena. The second line contains the description of the animals' positions. The line consists of n symbols "H" and "T". The "H"s correspond to hamsters and the "T"s correspond to tigers. It is guaranteed that at least one hamster and one tiger are present on the arena. The animals are given in the order in which they are located circle-wise, in addition, the last animal stands near the first one.
["0", "2"]
#include<stdio.h> #include<stdlib.h> #include<math.h> #define REP(i,a,b) for(i=a;i<b;i++) #define rep(i,n) REP(i,0,n) int main(){ int i,j,k,l,m,n; int res, tmp, cnt; char in[1200]; while(scanf("%d%s",&n,in)==2){ res=1000000000; cnt=0; rep(i,n) if(in[i]=='T') cnt++; rep(k,n){ tmp = 0; rep(i,cnt) if(in[(k+i)%n]=='H') tmp++; if(res > tmp) res = tmp; } printf("%d\n",res); } return 0; }
Today there is going to be an unusual performance at the circus — hamsters and tigers will perform together! All of them stand in circle along the arena edge and now the trainer faces a difficult task: he wants to swap the animals' positions so that all the hamsters stood together and all the tigers also stood together. The trainer swaps the animals in pairs not to create a mess. He orders two animals to step out of the circle and swap places. As hamsters feel highly uncomfortable when tigers are nearby as well as tigers get nervous when there's so much potential prey around (consisting not only of hamsters but also of yummier spectators), the trainer wants to spend as little time as possible moving the animals, i.e. he wants to achieve it with the minimal number of swaps. Your task is to help him.
Print the single number which is the minimal number of swaps that let the trainer to achieve his goal.
C
0fce263a9606fbfc3ec912894ab1a8f8
df5ac988dde2bd8271f88593743e6463
GNU C
standard output
256 megabytes
train_002.jsonl
[ "two pointers" ]
1291536000
["3\nHTH", "9\nHTHTHTHHT"]
NoteIn the first example we shouldn't move anybody because the animals of each species already stand apart from the other species. In the second example you may swap, for example, the tiger in position 2 with the hamster in position 5 and then — the tiger in position 9 with the hamster in position 7.
PASSED
1,600
standard input
2 seconds
The first line contains number n (2 ≤ n ≤ 1000) which indicates the total number of animals in the arena. The second line contains the description of the animals' positions. The line consists of n symbols "H" and "T". The "H"s correspond to hamsters and the "T"s correspond to tigers. It is guaranteed that at least one hamster and one tiger are present on the arena. The animals are given in the order in which they are located circle-wise, in addition, the last animal stands near the first one.
["0", "2"]
/* Hamsters and Tigers */ #include <stdio.h> #include <stdlib.h> int main (int argc, char** argv) { int n; int ri; int i; char* animals; int totalTigers = 0; fscanf (stdin, "%i", &n); animals = (char*) malloc (n+3); /* Just in case... */ /* for (i = 0; i < n; i++) { */ fscanf (stdin, "%s", animals); /* } */ for (i = 0; i < n; i++) { if (animals[i] == 'T') totalTigers++; } if (totalTigers == 0) { printf ("0"); return 0; } char* group = (char*) malloc (totalTigers+1); group[totalTigers] = 0; int bestGroup = 0; int currentGroup = 0; int bestStart = 0; int start; int end; int groupHead = 0; for (i = 0; i < totalTigers; i++) { group[i] = animals[i]; if (animals[i] == 'T') { currentGroup++; } } bestGroup = currentGroup; /* bestStart = 0; */ for (start = 1, end = totalTigers; start < n; start++, end++) { if (end == n) end = 0; /* printf ("%s %s\n", group, animals); */ if (group[groupHead] == 'T' && animals[end] == 'H') { /* printf ("NOPE\n"); */ currentGroup--; } else if (group[groupHead] == 'H' && animals[end] == 'T') { /* printf ("YUP\n"); */ currentGroup++; if (currentGroup > bestGroup) { bestGroup = currentGroup; /* bestStart = start; */ } } group[groupHead] = animals[end]; /* printf ("%i, %i, %i < END HEAD\n", end, groupHead, totalTigers); */ groupHead = (groupHead + 1) % totalTigers; } printf ("%i", totalTigers - bestGroup); return 0; }
Today there is going to be an unusual performance at the circus — hamsters and tigers will perform together! All of them stand in circle along the arena edge and now the trainer faces a difficult task: he wants to swap the animals' positions so that all the hamsters stood together and all the tigers also stood together. The trainer swaps the animals in pairs not to create a mess. He orders two animals to step out of the circle and swap places. As hamsters feel highly uncomfortable when tigers are nearby as well as tigers get nervous when there's so much potential prey around (consisting not only of hamsters but also of yummier spectators), the trainer wants to spend as little time as possible moving the animals, i.e. he wants to achieve it with the minimal number of swaps. Your task is to help him.
Print the single number which is the minimal number of swaps that let the trainer to achieve his goal.
C
0fce263a9606fbfc3ec912894ab1a8f8
5e352efef855dd96ab7e9a0e4a4684ed
GNU C
standard output
256 megabytes
train_002.jsonl
[ "two pointers" ]
1291536000
["3\nHTH", "9\nHTHTHTHHT"]
NoteIn the first example we shouldn't move anybody because the animals of each species already stand apart from the other species. In the second example you may swap, for example, the tiger in position 2 with the hamster in position 5 and then — the tiger in position 9 with the hamster in position 7.
PASSED
1,600
standard input
2 seconds
The first line contains number n (2 ≤ n ≤ 1000) which indicates the total number of animals in the arena. The second line contains the description of the animals' positions. The line consists of n symbols "H" and "T". The "H"s correspond to hamsters and the "T"s correspond to tigers. It is guaranteed that at least one hamster and one tiger are present on the arena. The animals are given in the order in which they are located circle-wise, in addition, the last animal stands near the first one.
["0", "2"]
#include <stdio.h> #define sc(a) scanf(" %c", &a) #define s(a) scanf("%d", &a) int main() { int i,j,k,l, c, h = 0, swp, x; int ans = (int) 1e7; char seq[1020]; s(c); for (i = 0; i < c; i++) { sc(seq[i]); h += (seq[i] == 'H'); } for ( j = 0; j < i; j++) { swp = 0; for ( k = j, l = 0; l < h ; k = (k+1)%i, l++ ) { swp += (seq[k] == 'T'); } ans = ans < swp ? ans : swp; } printf("%d\n", ans); return 0; } /* 1491616710149 */
Today there is going to be an unusual performance at the circus — hamsters and tigers will perform together! All of them stand in circle along the arena edge and now the trainer faces a difficult task: he wants to swap the animals' positions so that all the hamsters stood together and all the tigers also stood together. The trainer swaps the animals in pairs not to create a mess. He orders two animals to step out of the circle and swap places. As hamsters feel highly uncomfortable when tigers are nearby as well as tigers get nervous when there's so much potential prey around (consisting not only of hamsters but also of yummier spectators), the trainer wants to spend as little time as possible moving the animals, i.e. he wants to achieve it with the minimal number of swaps. Your task is to help him.
Print the single number which is the minimal number of swaps that let the trainer to achieve his goal.
C
0fce263a9606fbfc3ec912894ab1a8f8
f78d2dd89d0656f6d096d35823cf65e1
GNU C
standard output
256 megabytes
train_002.jsonl
[ "two pointers" ]
1291536000
["3\nHTH", "9\nHTHTHTHHT"]
NoteIn the first example we shouldn't move anybody because the animals of each species already stand apart from the other species. In the second example you may swap, for example, the tiger in position 2 with the hamster in position 5 and then — the tiger in position 9 with the hamster in position 7.
PASSED
1,600
standard input
2 seconds
The first line contains number n (2 ≤ n ≤ 1000) which indicates the total number of animals in the arena. The second line contains the description of the animals' positions. The line consists of n symbols "H" and "T". The "H"s correspond to hamsters and the "T"s correspond to tigers. It is guaranteed that at least one hamster and one tiger are present on the arena. The animals are given in the order in which they are located circle-wise, in addition, the last animal stands near the first one.
["0", "2"]
#include<stdio.h> int main(){ int until,ha=0,ti=0,i,j,k,l,n,m,st=0,en,last,min=1000000; char c[3005]; scanf("%d",&n); en = n-1; scanf(" %s",c); for(i=0;i<n;i++) if(c[i]=='H') ha++; else ti++; for(i=0;i<n;i++){ last = until = 0; for(j=st;j<=en;j++){ if(c[j]=='H'){ until++; last++; } else until++; if(until == ti) break; } if(last<min) min = last; c[en+1] = c[st]; st++; en++; } printf("%d\n",min); return 0; }
Today there is going to be an unusual performance at the circus — hamsters and tigers will perform together! All of them stand in circle along the arena edge and now the trainer faces a difficult task: he wants to swap the animals' positions so that all the hamsters stood together and all the tigers also stood together. The trainer swaps the animals in pairs not to create a mess. He orders two animals to step out of the circle and swap places. As hamsters feel highly uncomfortable when tigers are nearby as well as tigers get nervous when there's so much potential prey around (consisting not only of hamsters but also of yummier spectators), the trainer wants to spend as little time as possible moving the animals, i.e. he wants to achieve it with the minimal number of swaps. Your task is to help him.
Print the single number which is the minimal number of swaps that let the trainer to achieve his goal.
C
0fce263a9606fbfc3ec912894ab1a8f8
3009db4110cb05b1891e05feca6f5773
GNU C
standard output
256 megabytes
train_002.jsonl
[ "two pointers" ]
1291536000
["3\nHTH", "9\nHTHTHTHHT"]
NoteIn the first example we shouldn't move anybody because the animals of each species already stand apart from the other species. In the second example you may swap, for example, the tiger in position 2 with the hamster in position 5 and then — the tiger in position 9 with the hamster in position 7.
PASSED
1,600
standard input
2 seconds
The first line contains number n (2 ≤ n ≤ 1000) which indicates the total number of animals in the arena. The second line contains the description of the animals' positions. The line consists of n symbols "H" and "T". The "H"s correspond to hamsters and the "T"s correspond to tigers. It is guaranteed that at least one hamster and one tiger are present on the arena. The animals are given in the order in which they are located circle-wise, in addition, the last animal stands near the first one.
["0", "2"]
#include<stdio.h> #define maxn 1111 #define INF 1<<30 char c[maxn],temp[maxn]; int main(){ int n,i,j; while(~scanf("%d",&n)){ getchar(); scanf("%s",c); int ch=0,ct=0; for(i=0;i<n;i++){ if(c[i]=='H'){ ch++; } else{ ct++; } } int sh,st,ans=INF; for(i=0;i<n;i++){ for(j=0;j<n;j++){ temp[j]=c[(i+j)%n]; } sh=0; st=0; for(j=0;j<ch;j++){ if(temp[j]=='T'){ st++; } } if(st<ans) ans=st; for(j=0;j<ct;j++){ if(temp[j]=='H'){ sh++; } } if(sh<ans) ans=sh; } printf("%d\n",ans); } return 0; }
Today there is going to be an unusual performance at the circus — hamsters and tigers will perform together! All of them stand in circle along the arena edge and now the trainer faces a difficult task: he wants to swap the animals' positions so that all the hamsters stood together and all the tigers also stood together. The trainer swaps the animals in pairs not to create a mess. He orders two animals to step out of the circle and swap places. As hamsters feel highly uncomfortable when tigers are nearby as well as tigers get nervous when there's so much potential prey around (consisting not only of hamsters but also of yummier spectators), the trainer wants to spend as little time as possible moving the animals, i.e. he wants to achieve it with the minimal number of swaps. Your task is to help him.
Print the single number which is the minimal number of swaps that let the trainer to achieve his goal.
C
0fce263a9606fbfc3ec912894ab1a8f8
822c84c343b80558e55e42ba87fa9647
GNU C
standard output
256 megabytes
train_002.jsonl
[ "two pointers" ]
1291536000
["3\nHTH", "9\nHTHTHTHHT"]
NoteIn the first example we shouldn't move anybody because the animals of each species already stand apart from the other species. In the second example you may swap, for example, the tiger in position 2 with the hamster in position 5 and then — the tiger in position 9 with the hamster in position 7.
PASSED
1,600
standard input
2 seconds
The first line contains number n (2 ≤ n ≤ 1000) which indicates the total number of animals in the arena. The second line contains the description of the animals' positions. The line consists of n symbols "H" and "T". The "H"s correspond to hamsters and the "T"s correspond to tigers. It is guaranteed that at least one hamster and one tiger are present on the arena. The animals are given in the order in which they are located circle-wise, in addition, the last animal stands near the first one.
["0", "2"]
#include<math.h> #include<time.h> #include<stdio.h> #include<string.h> #include<stdlib.h> #define oo 1000000000 #define pi 3.14159265359 #define zero(a) (abb(a)<=1e-7) #define lowbit(a) ((a)&(-(a))) #define min(a,b) ((a)<(b)?(a):(b)) #define max(a,b) ((a)>(b)?(a):(b)) #define abb(a) ((a)>0?(a):(-(a))) #define cj(x1,y1,x2,y2) ((x1)*(y2)-(x2)*(y2)) #define dj(x1,y1,x2,y2) ((x1)*(y1)+(y1)*(y2)) #define dis(x1,y1,x2,y2) sqrt(((x2)-(x1))*((x2)-(x1))+((y2)-(y1))*((y2)-(y1))) char str[1005]={0}; long hash[1001]={0}; int main() { long i,j,a=0,flag=0,n,s,ans=oo; scanf("%ld%s",&n,str+1); for (i=1;i<=n;i++) a+=(str[i]=='T'); for (i=1;i<=n;i++) { flag++; for (j=1;j<=a;j++) hash[(i+j-1)%n+1]=flag; s=0; for (j=1;j<=n;j++) s+=((hash[j]==flag)^(str[j]=='T')); ans=min(ans,s/2); } printf("%ld\n",ans); return 0; }
Bob is playing with $$$6$$$-sided dice. A net of such standard cube is shown below.He has an unlimited supply of these dice and wants to build a tower by stacking multiple dice on top of each other, while choosing the orientation of each dice. Then he counts the number of visible pips on the faces of the dice.For example, the number of visible pips on the tower below is $$$29$$$ — the number visible on the top is $$$1$$$, from the south $$$5$$$ and $$$3$$$, from the west $$$4$$$ and $$$2$$$, from the north $$$2$$$ and $$$4$$$ and from the east $$$3$$$ and $$$5$$$.The one at the bottom and the two sixes by which the dice are touching are not visible, so they are not counted towards total.Bob also has $$$t$$$ favourite integers $$$x_i$$$, and for every such integer his goal is to build such a tower that the number of visible pips is exactly $$$x_i$$$. For each of Bob's favourite integers determine whether it is possible to build a tower that has exactly that many visible pips.
For each of Bob's favourite integers, output "YES" if it is possible to build the tower, or "NO" otherwise (quotes for clarity).
C
840a4e16454290471faa5a27a3c795d9
18d2bb1e89b5f6c9dc33dd3997fda54d
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "constructive algorithms", "math" ]
1576595100
["4\n29 34 19 38"]
NoteThe first example is mentioned in the problem statement.In the second example, one can build the tower by flipping the top dice from the previous tower.In the third example, one can use a single die that has $$$5$$$ on top.The fourth example is impossible.
PASSED
1,000
standard input
1 second
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of favourite integers of Bob. The second line contains $$$t$$$ space-separated integers $$$x_i$$$ ($$$1 \leq x_i \leq 10^{18}$$$) — Bob's favourite integers.
["YES\nYES\nYES\nNO"]
#include<stdio.h> int main() { int n; scanf("%d",&n); long long int a[n],j,i; for(i=0;i<n;i++) { scanf("%lld",&a[i]);} for(j=0;j<n;j++) { if(a[j]>14&&a[j]%14<=6&&a[j]%14>=1) printf("YES\n"); else printf("NO\n");} return 0; }
Bob is playing with $$$6$$$-sided dice. A net of such standard cube is shown below.He has an unlimited supply of these dice and wants to build a tower by stacking multiple dice on top of each other, while choosing the orientation of each dice. Then he counts the number of visible pips on the faces of the dice.For example, the number of visible pips on the tower below is $$$29$$$ — the number visible on the top is $$$1$$$, from the south $$$5$$$ and $$$3$$$, from the west $$$4$$$ and $$$2$$$, from the north $$$2$$$ and $$$4$$$ and from the east $$$3$$$ and $$$5$$$.The one at the bottom and the two sixes by which the dice are touching are not visible, so they are not counted towards total.Bob also has $$$t$$$ favourite integers $$$x_i$$$, and for every such integer his goal is to build such a tower that the number of visible pips is exactly $$$x_i$$$. For each of Bob's favourite integers determine whether it is possible to build a tower that has exactly that many visible pips.
For each of Bob's favourite integers, output "YES" if it is possible to build the tower, or "NO" otherwise (quotes for clarity).
C
840a4e16454290471faa5a27a3c795d9
aebfc1df3f518d5b83cd5c19e9b0ad8c
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "constructive algorithms", "math" ]
1576595100
["4\n29 34 19 38"]
NoteThe first example is mentioned in the problem statement.In the second example, one can build the tower by flipping the top dice from the previous tower.In the third example, one can use a single die that has $$$5$$$ on top.The fourth example is impossible.
PASSED
1,000
standard input
1 second
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of favourite integers of Bob. The second line contains $$$t$$$ space-separated integers $$$x_i$$$ ($$$1 \leq x_i \leq 10^{18}$$$) — Bob's favourite integers.
["YES\nYES\nYES\nNO"]
#include<stdio.h> int main() { long long n,i,j,t,c; scanf("%lld",&t); for(i=0;i<t;i++) { scanf("%lld",&n); c=n%14; //printf("%lld\n",c); if(c>=1&&c<=6&&n>=15) { printf("YES\n"); } else printf("NO\n"); } return 0; }
In order to do some research, $$$n^2$$$ labs are built on different heights of a mountain. Let's enumerate them with integers from $$$1$$$ to $$$n^2$$$, such that the lab with the number $$$1$$$ is at the lowest place, the lab with the number $$$2$$$ is at the second-lowest place, $$$\ldots$$$, the lab with the number $$$n^2$$$ is at the highest place.To transport water between the labs, pipes are built between every pair of labs. A pipe can transport at most one unit of water at a time from the lab with the number $$$u$$$ to the lab with the number $$$v$$$ if $$$u &gt; v$$$.Now the labs need to be divided into $$$n$$$ groups, each group should contain exactly $$$n$$$ labs. The labs from different groups can transport water to each other. The sum of units of water that can be sent from a group $$$A$$$ to a group $$$B$$$ is equal to the number of pairs of labs ($$$u, v$$$) such that the lab with the number $$$u$$$ is from the group $$$A$$$, the lab with the number $$$v$$$ is from the group $$$B$$$ and $$$u &gt; v$$$. Let's denote this value as $$$f(A,B)$$$ (i.e. $$$f(A,B)$$$ is the sum of units of water that can be sent from a group $$$A$$$ to a group $$$B$$$).For example, if $$$n=3$$$ and there are $$$3$$$ groups $$$X$$$, $$$Y$$$ and $$$Z$$$: $$$X = \{1, 5, 6\}, Y = \{2, 4, 9\}$$$ and $$$Z = \{3, 7, 8\}$$$. In this case, the values of $$$f$$$ are equal to: $$$f(X,Y)=4$$$ because of $$$5 \rightarrow 2$$$, $$$5 \rightarrow 4$$$, $$$6 \rightarrow 2$$$, $$$6 \rightarrow 4$$$, $$$f(X,Z)=2$$$ because of $$$5 \rightarrow 3$$$, $$$6 \rightarrow 3$$$, $$$f(Y,X)=5$$$ because of $$$2 \rightarrow 1$$$, $$$4 \rightarrow 1$$$, $$$9 \rightarrow 1$$$, $$$9 \rightarrow 5$$$, $$$9 \rightarrow 6$$$, $$$f(Y,Z)=4$$$ because of $$$4 \rightarrow 3$$$, $$$9 \rightarrow 3$$$, $$$9 \rightarrow 7$$$, $$$9 \rightarrow 8$$$, $$$f(Z,X)=7$$$ because of $$$3 \rightarrow 1$$$, $$$7 \rightarrow 1$$$, $$$7 \rightarrow 5$$$, $$$7 \rightarrow 6$$$, $$$8 \rightarrow 1$$$, $$$8 \rightarrow 5$$$, $$$8 \rightarrow 6$$$, $$$f(Z,Y)=5$$$ because of $$$3 \rightarrow 2$$$, $$$7 \rightarrow 2$$$, $$$7 \rightarrow 4$$$, $$$8 \rightarrow 2$$$, $$$8 \rightarrow 4$$$. Please, divide labs into $$$n$$$ groups with size $$$n$$$, such that the value $$$\min f(A,B)$$$ over all possible pairs of groups $$$A$$$ and $$$B$$$ ($$$A \neq B$$$) is maximal.In other words, divide labs into $$$n$$$ groups with size $$$n$$$, such that minimum number of the sum of units of water that can be transported from a group $$$A$$$ to a group $$$B$$$ for every pair of different groups $$$A$$$ and $$$B$$$ ($$$A \neq B$$$) as big as possible.Note, that the example above doesn't demonstrate an optimal division, but it demonstrates how to calculate the values $$$f$$$ for some division.If there are many optimal divisions, you can find any.
Output $$$n$$$ lines: In the $$$i$$$-th line print $$$n$$$ numbers, the numbers of labs of the $$$i$$$-th group, in any order you want. If there are multiple answers, that maximize the minimum number of the sum of units of water that can be transported from one group the another, you can print any.
C
d5ae278ad52a4ab55d732b27a91c2620
8cb705645d8e747ff4600a1ee1cc4f6b
GNU C11
standard output
256 megabytes
train_002.jsonl
[ "constructive algorithms", "implementation", "greedy" ]
1571319300
["3"]
NoteIn the first test we can divide $$$9$$$ labs into groups $$$\{2, 8, 5\}, \{9, 3, 4\}, \{7, 6, 1\}$$$.From the first group to the second group we can transport $$$4$$$ units of water ($$$8 \rightarrow 3, 8 \rightarrow 4, 5 \rightarrow 3, 5 \rightarrow 4$$$).From the first group to the third group we can transport $$$5$$$ units of water ($$$2 \rightarrow 1, 8 \rightarrow 7, 8 \rightarrow 6, 8 \rightarrow 1, 5 \rightarrow 1$$$).From the second group to the first group we can transport $$$5$$$ units of water ($$$9 \rightarrow 2, 9 \rightarrow 8, 9 \rightarrow 5, 3 \rightarrow 2, 4 \rightarrow 2$$$).From the second group to the third group we can transport $$$5$$$ units of water ($$$9 \rightarrow 7, 9 \rightarrow 6, 9 \rightarrow 1, 3 \rightarrow 1, 4 \rightarrow 1$$$).From the third group to the first group we can transport $$$4$$$ units of water ($$$7 \rightarrow 2, 7 \rightarrow 5, 6 \rightarrow 2, 6 \rightarrow 5$$$).From the third group to the second group we can transport $$$4$$$ units of water ($$$7 \rightarrow 3, 7 \rightarrow 4, 6 \rightarrow 3, 6 \rightarrow 4$$$).The minimal number of the sum of units of water, that can be transported from one group to another is equal to $$$4$$$. It can be proved, that it is impossible to make a better division.
PASSED
1,300
standard input
1 second
The only line contains one number $$$n$$$ ($$$2 \leq n \leq 300$$$).
["2 8 5\n9 3 4\n7 6 1"]
#include<stdio.h> int main() { int n; scanf("%d", &n); int i, j; int ans[302][302]; for (j = 0; j < n; j++) { if (j % 2 == 0) { for (i = 0; i < n; i++) ans[i][j] = n * j+ i + 1; } else { for (i = 0; i < n; i++) ans[n - i - 1][j] = n * j + i + 1; } } for (i = 0; i < n; i++) { for (j = 0; j < n - 1; j++) printf("%d ", ans[i][j]); printf("%d\n", ans[i][n - 1]); } return 0; }