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 weighted undirected graph. The vertices are enumerated from 1 to n. Your task is to find the shortest path between the vertex 1 and the vertex n. | Write the only integer -1 in case of no path. Write the shortest path in opposite case. If there are many solutions, print any of them. | C | bda2ca1fd65084bb9d8659c0a591743d | 473c720b8a9958afc3188e544b0853b6 | GNU C11 | standard output | 64 megabytes | train_000.jsonl | [
"shortest paths",
"graphs"
] | 1276875000 | ["5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1", "5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1"] | null | PASSED | 1,900 | standard input | 1 second | The first line contains two integers n and m (2ββ€βnββ€β105,β0ββ€βmββ€β105), where n is the number of vertices and m is the number of edges. Following m lines contain one edge each in form ai, bi and wi (1ββ€βai,βbiββ€βn,β1ββ€βwiββ€β106), where ai,βbi are edge endpoints and wi is the length of the edge. It is possible that the graph has loops and multiple edges between pair of vertices. | ["1 4 3 5", "1 4 3 5"] | #include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 222222
typedef long long ll;
typedef struct {ll v, w;} pair;
typedef struct _list{
pair p;
struct _list *next;
} list;
list *g[N];
ll n, m, a, b, w, size, dist[N], prev[N], ans[N], ind;
pair pq[N];
void min_heapify(ll ind){
ll l=ind*2, r=l+1, lowest=ind;
if (l<=size&&(pq[l].v<pq[ind].v||pq[l].w<pq[ind].w))
lowest=l;
if (r<=size&&(pq[r].v<pq[ind].v||pq[r].w<pq[ind].w))
lowest=r;
if (lowest!=ind){
pair tmp=pq[ind];
pq[ind]=pq[lowest];
pq[lowest]=tmp;
min_heapify(lowest);
}
}
pair extract_min(void){
pair min=pq[1];
pq[1]=pq[size];
size--;
min_heapify(1);
return min;
}
void pushh(ll v, ll w){
++size;
pq[size].v=v;
pq[size].w=w;
min_heapify(size);
}
void pushg(ll u, ll v, ll w){
list *ptr=(list *) malloc(sizeof(list));
ptr->next=g[u];
ptr->p.v=v;
ptr->p.w=w;
g[u]=ptr;
}
int main(){
scanf("%I64d%I64d", &n, &m);
for (ll i=1; i<=n; i++)
dist[i]=LLONG_MAX;
while (m--){
scanf("%I64d%I64d%I64d", &a, &b, &w);
pushg(a, b, w);
pushg(b, a, w);
}
pushh(1, 0);
while (size){
pair p=extract_min();
ll /*d=p.w,*/ u=p.v;
//if (d>dist[u])
//continue;
for (list *a=g[u]; a; a=a->next){
pair v=a->p;
if (dist[u]+v.w<dist[v.v]){
dist[v.v]=dist[u]+v.w;
prev[v.v]=u;
pushh(v.v, v.w);
}
}
}
if (!prev[n]){
printf("-1");
return 0;
}
prev[1]=0;
a=n;
if (prev[a])
while (prev[a]){
ans[ind]=a;
a=prev[a];
ind++;
}
printf("1 ");
for (ll i=ind-1; i>=0; i--)
printf("%I64d ", ans[i]);
} | |
You are given a weighted undirected graph. The vertices are enumerated from 1 to n. Your task is to find the shortest path between the vertex 1 and the vertex n. | Write the only integer -1 in case of no path. Write the shortest path in opposite case. If there are many solutions, print any of them. | C | bda2ca1fd65084bb9d8659c0a591743d | d7cd2da14e1d03e6c1d4a04405019f4c | GNU C11 | standard output | 64 megabytes | train_000.jsonl | [
"shortest paths",
"graphs"
] | 1276875000 | ["5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1", "5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1"] | null | PASSED | 1,900 | standard input | 1 second | The first line contains two integers n and m (2ββ€βnββ€β105,β0ββ€βmββ€β105), where n is the number of vertices and m is the number of edges. Following m lines contain one edge each in form ai, bi and wi (1ββ€βai,βbiββ€βn,β1ββ€βwiββ€β106), where ai,βbi are edge endpoints and wi is the length of the edge. It is possible that the graph has loops and multiple edges between pair of vertices. | ["1 4 3 5", "1 4 3 5"] | #include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 222222
typedef long long ll;
typedef struct {ll v, w;} pair;
typedef struct _list{
pair p;
struct _list *next;
} list;
list *g[N];
ll n, m, u, v, w, size, dist[N], prev[N];
pair pq[N];
void min_heapify(ll ind){
if (!ind)
return;
ll l=ind*2, r=l+1, lowest=ind;
if (l<=size&&(pq[l].v<pq[ind].v||pq[l].w<pq[ind].w))
lowest=l;
if (r<=size&&(pq[r].v<pq[ind].v||pq[r].w<pq[ind].w))
lowest=r;
if (lowest!=ind){
pair tmp=pq[ind];
pq[ind]=pq[lowest];
pq[lowest]=tmp;
min_heapify(lowest);
}
}
pair top(void){
pair min=pq[1];
pq[1]=pq[size];
size--;
min_heapify(1);
return min;
}
void pushh(ll v, ll w){
++size;
pq[size].v=v;
pq[size].w=w;
min_heapify(size/2);
}
void pushg(ll u, ll v, ll w){
list *ptr=(list *) malloc(sizeof(list));
ptr->next=g[u];
ptr->p.v=v;
ptr->p.w=w;
g[u]=ptr;
}
void dijkstra(ll s){
for (ll i=1; i<=n; i++)
dist[i]=LLONG_MAX;
dist[s]=0;
pushh(1, 0);
while (size){
pair p=top();
ll u=p.v;
for (list *a=g[u]; a; a=a->next){
pair v=a->p;
if (dist[u]+v.w<dist[v.v]){
dist[v.v]=dist[u]+v.w;
prev[v.v]=u;
pushh(v.v, w);
}
}
}
}
void output(ll v){
if (prev[v])
output(prev[v]);
printf("%lld ", v);
}
int main(){
scanf("%lld%lld", &n, &m);
while (m--){
scanf("%lld%lld%lld", &u, &v, &w);
pushg(u, v, w);
pushg(v, u, w);
}
dijkstra(1);
prev[1]=0;
if (prev[n])
output(n);
else
printf("-1");
} | |
You are given a weighted undirected graph. The vertices are enumerated from 1 to n. Your task is to find the shortest path between the vertex 1 and the vertex n. | Write the only integer -1 in case of no path. Write the shortest path in opposite case. If there are many solutions, print any of them. | C | bda2ca1fd65084bb9d8659c0a591743d | 1eeefe432e413837bb2244c89cb01b01 | GNU C11 | standard output | 64 megabytes | train_000.jsonl | [
"shortest paths",
"graphs"
] | 1276875000 | ["5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1", "5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1"] | null | PASSED | 1,900 | standard input | 1 second | The first line contains two integers n and m (2ββ€βnββ€β105,β0ββ€βmββ€β105), where n is the number of vertices and m is the number of edges. Following m lines contain one edge each in form ai, bi and wi (1ββ€βai,βbiββ€βn,β1ββ€βwiββ€β106), where ai,βbi are edge endpoints and wi is the length of the edge. It is possible that the graph has loops and multiple edges between pair of vertices. | ["1 4 3 5", "1 4 3 5"] | #include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 222222
typedef long long ll;
typedef struct {ll v, w;} pair;
typedef struct _list{
pair p;
struct _list *next;
} list;
list *g[N];
ll n, m, a, b, w, size, dist[N], prev[N];
pair pq[N];
void min_heapify(ll ind){
ll l=ind*2, r=l+1, lowest=ind;
if (l<=size&&(pq[l].v<pq[ind].v||pq[l].w<pq[ind].w))
lowest=l;
if (r<=size&&(pq[r].v<pq[ind].v||pq[r].w<pq[ind].w))
lowest=r;
if (lowest!=ind){
pair tmp=pq[ind];
pq[ind]=pq[lowest];
pq[lowest]=tmp;
min_heapify(lowest);
}
}
pair extract_min(void){
pair min=pq[1];
pq[1]=pq[size];
size--;
min_heapify(1);
return min;
}
void pushh(ll v, ll w){
++size;
pq[size].v=v;
pq[size].w=w;
min_heapify(size);
}
void pushg(ll u, ll v, ll w){
list *ptr=(list *) malloc(sizeof(list));
ptr->next=g[u];
ptr->p.v=v;
ptr->p.w=w;
g[u]=ptr;
}
void dijkstra(void){
for (ll i=1; i<=n; i++)
dist[i]=LLONG_MAX;
pushh(1, 0);
while (size){
pair p=extract_min();
ll u=p.v;
for (list *a=g[u]; a; a=a->next){
pair v=a->p;
if (dist[u]+v.w<dist[v.v]){
dist[v.v]=dist[u]+v.w;
prev[v.v]=u;
pushh(v.v, v.w);
}
}
}
}
void output(ll v){
if (prev[v])
output(prev[v]);
printf("%lld ", v);
}
int main(){
scanf("%I64d%I64d", &n, &m);
while (m--){
scanf("%I64d%I64d%I64d", &a, &b, &w);
pushg(a, b, w);
pushg(b, a, w);
}
dijkstra();
if (!prev[n]){
printf("-1");
return 0;
}
prev[1]=0;
output(n);
}
| |
You are given a weighted undirected graph. The vertices are enumerated from 1 to n. Your task is to find the shortest path between the vertex 1 and the vertex n. | Write the only integer -1 in case of no path. Write the shortest path in opposite case. If there are many solutions, print any of them. | C | bda2ca1fd65084bb9d8659c0a591743d | a7b7d3521151fec6a658497e6824ad11 | GNU C11 | standard output | 64 megabytes | train_000.jsonl | [
"shortest paths",
"graphs"
] | 1276875000 | ["5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1", "5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1"] | null | PASSED | 1,900 | standard input | 1 second | The first line contains two integers n and m (2ββ€βnββ€β105,β0ββ€βmββ€β105), where n is the number of vertices and m is the number of edges. Following m lines contain one edge each in form ai, bi and wi (1ββ€βai,βbiββ€βn,β1ββ€βwiββ€β106), where ai,βbi are edge endpoints and wi is the length of the edge. It is possible that the graph has loops and multiple edges between pair of vertices. | ["1 4 3 5", "1 4 3 5"] | #include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 222222
typedef long long ll;
typedef struct {ll v, w;} pair;
typedef struct _list{
pair p;
struct _list *next;
} list;
list *g[N];
ll n, m, a, b, w, size, dist[N], prev[N], ans[N], ind;
pair pq[N];
void min_heapify(ll ind){
ll l=ind*2, r=l+1, lowest=ind;
if (l<=size&&(pq[l].v<pq[ind].v||pq[l].w<pq[ind].w))
lowest=l;
if (r<=size&&(pq[r].v<pq[ind].v||pq[r].w<pq[ind].w))
lowest=r;
if (lowest!=ind){
pair tmp=pq[ind];
pq[ind]=pq[lowest];
pq[lowest]=tmp;
min_heapify(lowest);
}
}
void build_min_heap(void){
for (ll i=size/2; i; i--)
min_heapify(i);
}
pair extract_min(void){
pair min=pq[1];
pq[1]=pq[size];
size--;
min_heapify(1);
return min;
}
void pushh(ll v, ll w){
++size;
pq[size].v=v;
pq[size].w=w;
min_heapify(size);
}
void pushg(ll u, ll v, ll w){
list *ptr=(list *) malloc(sizeof(list));
ptr->next=g[u];
ptr->p.v=v;
ptr->p.w=w;
g[u]=ptr;
}
void dijkstra(void){
for (ll i=1; i<=n; i++)
dist[i]=LLONG_MAX;
pushh(1, 0);
while (size){
pair p=extract_min();
ll u=p.v;
for (list *a=g[u]; a; a=a->next){
pair v=a->p;
if (dist[u]+v.w<dist[v.v]){
dist[v.v]=dist[u]+v.w;
prev[v.v]=u;
pushh(v.v, v.w);
}
}
}
}
int main(){
scanf("%I64d%I64d", &n, &m);
while (m--){
scanf("%I64d%I64d%I64d", &a, &b, &w);
pushg(a, b, w);
pushg(b, a, w);
}
dijkstra();
if (!prev[n]){
printf("-1");
return 0;
}
prev[1]=0;
a=n;
if (prev[a])
while (prev[a]){
ans[ind]=a;
a=prev[a];
ind++;
}
printf("1 ");
for (ll i=ind-1; i>=0; i--)
printf("%I64d ", ans[i]);
}
| |
You are given a weighted undirected graph. The vertices are enumerated from 1 to n. Your task is to find the shortest path between the vertex 1 and the vertex n. | Write the only integer -1 in case of no path. Write the shortest path in opposite case. If there are many solutions, print any of them. | C | bda2ca1fd65084bb9d8659c0a591743d | 49596bef68f7bad96d835e543563166f | GNU C11 | standard output | 64 megabytes | train_000.jsonl | [
"shortest paths",
"graphs"
] | 1276875000 | ["5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1", "5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1"] | null | PASSED | 1,900 | standard input | 1 second | The first line contains two integers n and m (2ββ€βnββ€β105,β0ββ€βmββ€β105), where n is the number of vertices and m is the number of edges. Following m lines contain one edge each in form ai, bi and wi (1ββ€βai,βbiββ€βn,β1ββ€βwiββ€β106), where ai,βbi are edge endpoints and wi is the length of the edge. It is possible that the graph has loops and multiple edges between pair of vertices. | ["1 4 3 5", "1 4 3 5"] | #include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 222222
typedef long long ll;
typedef struct {ll v, w;} pair;
typedef struct _list{
pair p;
struct _list *next;
} list;
list *g[N];
ll n, m, u, v, w, size, dist[N], prev[N];
pair pq[N];
void min_heapify(ll ind){
ll l=ind*2, r=l+1, lowest=ind;
if (l<=size&&(pq[l].v<pq[ind].v||pq[l].w<pq[ind].w))
lowest=l;
if (r<=size&&(pq[r].v<pq[ind].v||pq[r].w<pq[ind].w))
lowest=r;
if (lowest!=ind){
pair tmp=pq[ind];
pq[ind]=pq[lowest];
pq[lowest]=tmp;
min_heapify(lowest);
}
}
pair top(void){
pair min=pq[1];
pq[1]=pq[size];
size--;
min_heapify(1);
return min;
}
void pushh(ll v, ll w){
++size;
pq[size].v=v;
pq[size].w=w;
}
void pushg(ll u, ll v, ll w){
list *ptr=(list *) malloc(sizeof(list));
ptr->next=g[u];
ptr->p.v=v;
ptr->p.w=w;
g[u]=ptr;
}
void dijkstra(ll s){
for (ll i=1; i<=n; i++)
dist[i]=LLONG_MAX;
dist[s]=0;
pushh(1, 0);
while (size){
pair p=top();
ll u=p.v;
for (list *a=g[u]; a; a=a->next){
pair v=a->p;
if (dist[u]+v.w<dist[v.v]){
dist[v.v]=dist[u]+v.w;
prev[v.v]=u;
pushh(v.v, w);
}
}
}
}
void output(ll v){
if (prev[v])
output(prev[v]);
printf("%lld ", v);
}
int main(){
scanf("%lld%lld", &n, &m);
while (m--){
scanf("%lld%lld%lld", &u, &v, &w);
pushg(u, v, w);
pushg(v, u, w);
}
dijkstra(1);
prev[1]=0;
if (prev[n])
output(n);
else
printf("-1");
} | |
You are given a weighted undirected graph. The vertices are enumerated from 1 to n. Your task is to find the shortest path between the vertex 1 and the vertex n. | Write the only integer -1 in case of no path. Write the shortest path in opposite case. If there are many solutions, print any of them. | C | bda2ca1fd65084bb9d8659c0a591743d | eabbb9664887b23d520fbd22c2008bcf | GNU C11 | standard output | 64 megabytes | train_000.jsonl | [
"shortest paths",
"graphs"
] | 1276875000 | ["5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1", "5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1"] | null | PASSED | 1,900 | standard input | 1 second | The first line contains two integers n and m (2ββ€βnββ€β105,β0ββ€βmββ€β105), where n is the number of vertices and m is the number of edges. Following m lines contain one edge each in form ai, bi and wi (1ββ€βai,βbiββ€βn,β1ββ€βwiββ€β106), where ai,βbi are edge endpoints and wi is the length of the edge. It is possible that the graph has loops and multiple edges between pair of vertices. | ["1 4 3 5", "1 4 3 5"] | #define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "limits.h"
long long inf = LLONG_MAX;
typedef struct vec
{
int *arr;
long long *wgt;
int size;
int cap;
} vec;
vec* g;
void push(vec* a, int val, long long w)
{
if (a->cap == 0)
{
a->cap = 3;
a->arr = (int*)malloc(3 * sizeof(int));
a->wgt = (long long*)malloc(3 * sizeof(long long));
}
else if (a->size == a->cap)
{
a->cap = a->cap * 2 + 1;
a->arr = (int*)realloc(a->arr, a->cap * sizeof(int));
a->wgt = (long long*)realloc(a->wgt, a->cap * sizeof(long long));
}
a->arr[a->size] = val;
a->wgt[a->size] = w;
a->size++;
}
typedef struct node
{
long long dist;
int pos; //id in heap
} node;
int *heap;
node *val;
int *prev;
void build(int n, int k)
{
heap = (int*)malloc(n * sizeof(int));
val = (node*)malloc(n * sizeof(node));
heap[0] = k;
for (int i = 1; i <= k; i++)
{
heap[i] = i - 1;
}
for (int i = k + 1; i < n; i++)
{
heap[i] = i;
}
for (int i = 0; i < n; i++)
{
val[i].dist = inf;
if (i < k)
{
val[i].pos = i + 1;
}
else if (i > k)
{
val[i].pos = i;
}
}
val[k].dist = 0;
val[k].pos = 0;
}
int pop(int size)
{
int ans = heap[0];
val[ans].pos = size - 1;
int t = heap[0];
heap[0] = heap[size - 1];
heap[size - 1] = t;
val[heap[0]].pos = 0;
int p = 0;
while (1)
{
long long v1 = val[heap[p]].dist;
long long v2, v3;
if (p * 2 + 1 < size - 1)
{
v2 = val[heap[p * 2 + 1]].dist;
}
else
{
v2 = LLONG_MAX;
}
if (p * 2 + 2 < size - 1)
{
v3 = val[heap[p * 2 + 2]].dist;
}
else
{
v3 = LLONG_MAX;
}
if (v1 <= v2 && v1 <= v3)
{
break;
}
if (v2 < v3)
{
val[heap[p]].pos = p * 2 + 1;
val[heap[p * 2 + 1]].pos = p;
int t = heap[p];
heap[p] = heap[p * 2 + 1];
heap[p * 2 + 1] = t;
p = p * 2 + 1;
}
else
{
val[heap[p]].pos = p * 2 + 2;
val[heap[p * 2 + 2]].pos = p;
int t = heap[p];
heap[p] = heap[p * 2 + 2];
heap[p * 2 + 2] = t;
p = p * 2 + 2;
}
}
return ans;
}
void update(int pos, long long newval, int size)
{
val[pos].dist = newval;
int p = val[pos].pos;
while (1)
{
if (p > 0)
{
int p1 = heap[p];
int p2 = heap[(p - 1) / 2];
if (val[p1].dist < val[p2].dist)
{
val[p1].pos = (p - 1) / 2;
val[p2].pos = p;
int t = heap[p];
heap[p] = heap[(p - 1) / 2];
heap[(p - 1) / 2] = t;
p = (p - 1) / 2;
continue;
}
}
break;
}
}
int vprintp(int a)
{
if (a == 0)
{
printf("1 ");
}
else
{
vprintp(prev[a]);
printf("%d ", a + 1);
}
}
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int n, m;
scanf("%d%d", &n, &m);
int s = 1, t = n;
//scanf("%d%d", &s, &t);
prev = malloc(n * sizeof(int));
g = (vec*)calloc(n, sizeof(vec));
int *used = calloc(n, sizeof(int));
used[s - 1] = 1;
for (int i = 0; i < m; i++)
{
int u, v;
long long w;
scanf("%d%d%lld", &u, &v, &w);
u--;
v--;
push(&g[u], v, w);
push(&g[v], u, w);
}
build(n, s - 1);
int size = n;
while (size > 0)
{
int a = pop(size);
if (used[a] == 0) break;
long long dist = val[a].dist;
size--;
for (int i = 0; i < g[a].size; i++)
{
int to = g[a].arr[i];
int w = g[a].wgt[i];
if (val[to].dist > dist + w || !used[to])
{
used[to] = 1;
update(to, dist + w, size);
prev[to] = a;
}
}
}
if (used[t - 1] == 0)
{
printf("-1");
}
else
{
vprintp(n - 1);
//printf("%lld", val[t - 1].dist);
}
free(g);
free(heap);
free(val);
free(used);
free(prev);
return 0;
} | |
It's another Start[c]up finals, and that means there is pizza to order for the onsite contestants. There are only 2 types of pizza (obviously not, but let's just pretend for the sake of the problem), and all pizzas contain exactly S slices.It is known that the i-th contestant will eat si slices of pizza, and gain ai happiness for each slice of type 1 pizza they eat, and bi happiness for each slice of type 2 pizza they eat. We can order any number of type 1 and type 2 pizzas, but we want to buy the minimum possible number of pizzas for all of the contestants to be able to eat their required number of slices. Given that restriction, what is the maximum possible total happiness that can be achieved? | Print the maximum total happiness that can be achieved. | C | 769859d86a3ceb2d89a444cd64c9a73b | 3514cddf7351b400838121472659494f | GNU C | standard output | 256 megabytes | train_000.jsonl | [
"binary search",
"sortings",
"ternary search"
] | 1506791100 | ["3 12\n3 5 7\n4 6 7\n5 9 5", "6 10\n7 4 7\n5 8 8\n12 5 8\n6 11 6\n3 3 7\n5 9 6"] | NoteIn the first example, you only need to buy one pizza. If you buy a type 1 pizza, the total happiness will be 3Β·5β+β4Β·6β+β5Β·9β=β84, and if you buy a type 2 pizza, the total happiness will be 3Β·7β+β4Β·7β+β5Β·5β=β74. | PASSED | 1,900 | standard input | 2 seconds | The first line of input will contain integers N and S (1ββ€βNββ€β105,β1ββ€βSββ€β105), the number of contestants and the number of slices per pizza, respectively. N lines follow. The i-th such line contains integers si, ai, and bi (1ββ€βsiββ€β105,β1ββ€βaiββ€β105,β1ββ€βbiββ€β105), the number of slices the i-th contestant will eat, the happiness they will gain from each type 1 slice they eat, and the happiness they will gain from each type 2 slice they eat, respectively. | ["84", "314"] | #include <stdio.h>
#include <stdlib.h>
typedef struct{
int slice, a, b;
}Person;
Person p[100001], Pa[100001], Pb[100001];
int mycmp(const void* a, const void* b){
const Person* sa = (const Person*)a;
const Person* sb = (const Person*)b;
return (sa->a-sa->b)-(sb->a-sb->b);
}
int main(){
int n, s;
scanf("%d %d", &n, &s);
long long piza=0, pizb=0, ans=0;
int numa=0, numb=0;
for (int i=0; i<n; i++){
scanf("%d %d %d", &p[i].slice, &p[i].a, &p[i].b);
int tmp=p[i].a-p[i].b;
if (tmp>0) {
ans+=(double)p[i].slice*p[i].a;
piza+=p[i].slice;
Pa[numa++]=p[i];
}
else {
ans+=(double)p[i].slice*p[i].b;
pizb+=p[i].slice;
Pb[numb++]=p[i];
}
}
qsort(Pa, numa, sizeof(Person), mycmp);
qsort(Pb, numb, sizeof(Person), mycmp);
if (piza%s ==0 && pizb%s==0) printf("%I64d\n", ans);
else if ((piza%s+pizb%s>s) || piza%s==0 || pizb%s==0) printf("%I64d\n", ans);
else {
long long ansa=ans, ansb=ans;
int i=0;
piza%=s; pizb%=s;
while(piza--){
ansa-=Pa[i].a;
ansa+=Pa[i].b;
Pa[i].slice--;
if (!Pa[i].slice) i++;
}
i=numb-1;
while(pizb--){
ansb-=Pb[i].b;
ansb+=Pb[i].a;
Pb[i].slice--;
if (!Pb[i].slice) i--;
}
printf("%I64d\n", ansa>ansb?ansa:ansb);
}
} | |
You are the gym teacher in the school.There are $$$n$$$ students in the row. And there are two rivalling students among them. The first one is in position $$$a$$$, the second in position $$$b$$$. Positions are numbered from $$$1$$$ to $$$n$$$ from left to right.Since they are rivals, you want to maximize the distance between them. If students are in positions $$$p$$$ and $$$s$$$ respectively, then distance between them is $$$|p - s|$$$. You can do the following operation at most $$$x$$$ times: choose two adjacent (neighbouring) students and swap them.Calculate the maximum distance between two rivalling students after at most $$$x$$$ swaps. | For each test case print one integer β the maximum distance between two rivaling students which you can obtain. | C | 1fd2619aabf4557093a59da804fd0e7b | 72aae9cf56d63a10f5d665c48744ca77 | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"greedy",
"math"
] | 1573655700 | ["3\n5 1 3 2\n100 33 100 1\n6 0 2 3"] | NoteIn the first test case you can swap students in positions $$$3$$$ and $$$4$$$. And then the distance between the rivals is equal to $$$|4 - 2| = 2$$$.In the second test case you don't have to swap students. In the third test case you can't swap students. | PASSED | 800 | standard input | 1 second | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. The only line of each test case contains four integers $$$n$$$, $$$x$$$, $$$a$$$ and $$$b$$$ ($$$2 \le n \le 100$$$, $$$0 \le x \le 100$$$, $$$1 \le a, b \le n$$$, $$$a \neq b$$$) β the number of students in the row, the number of swaps which you can do, and positions of first and second rivaling students respectively. | ["2\n99\n1"] | #include<stdio.h>
#include<stdlib.h>
int main()
{
int t,i,n,x,a,b;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d%d",&n,&x,&a,&b);
int m=abs(a-b);
if(m+x<n)
printf("%d\n",m+x);
else
printf("%d\n",n-1);
}
return 0;
}
| |
You are the gym teacher in the school.There are $$$n$$$ students in the row. And there are two rivalling students among them. The first one is in position $$$a$$$, the second in position $$$b$$$. Positions are numbered from $$$1$$$ to $$$n$$$ from left to right.Since they are rivals, you want to maximize the distance between them. If students are in positions $$$p$$$ and $$$s$$$ respectively, then distance between them is $$$|p - s|$$$. You can do the following operation at most $$$x$$$ times: choose two adjacent (neighbouring) students and swap them.Calculate the maximum distance between two rivalling students after at most $$$x$$$ swaps. | For each test case print one integer β the maximum distance between two rivaling students which you can obtain. | C | 1fd2619aabf4557093a59da804fd0e7b | f59bc855037deeb5a67f649d081e17f9 | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"greedy",
"math"
] | 1573655700 | ["3\n5 1 3 2\n100 33 100 1\n6 0 2 3"] | NoteIn the first test case you can swap students in positions $$$3$$$ and $$$4$$$. And then the distance between the rivals is equal to $$$|4 - 2| = 2$$$.In the second test case you don't have to swap students. In the third test case you can't swap students. | PASSED | 800 | standard input | 1 second | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. The only line of each test case contains four integers $$$n$$$, $$$x$$$, $$$a$$$ and $$$b$$$ ($$$2 \le n \le 100$$$, $$$0 \le x \le 100$$$, $$$1 \le a, b \le n$$$, $$$a \neq b$$$) β the number of students in the row, the number of swaps which you can do, and positions of first and second rivaling students respectively. | ["2\n99\n1"] | #include<stdio.h>
main()
{
int n,i,j,a,b,c,d,e,f;
scanf("%d",&n);
for(i=0; i<n; i++)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
e=abs(c-d)+b;
f=a-1;
if(e>=f)
printf("%d\n",f);
else
printf("%d\n",e);
}
}
| |
You are the gym teacher in the school.There are $$$n$$$ students in the row. And there are two rivalling students among them. The first one is in position $$$a$$$, the second in position $$$b$$$. Positions are numbered from $$$1$$$ to $$$n$$$ from left to right.Since they are rivals, you want to maximize the distance between them. If students are in positions $$$p$$$ and $$$s$$$ respectively, then distance between them is $$$|p - s|$$$. You can do the following operation at most $$$x$$$ times: choose two adjacent (neighbouring) students and swap them.Calculate the maximum distance between two rivalling students after at most $$$x$$$ swaps. | For each test case print one integer β the maximum distance between two rivaling students which you can obtain. | C | 1fd2619aabf4557093a59da804fd0e7b | c942725aaf3e3ce0d037583f12235a3e | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"greedy",
"math"
] | 1573655700 | ["3\n5 1 3 2\n100 33 100 1\n6 0 2 3"] | NoteIn the first test case you can swap students in positions $$$3$$$ and $$$4$$$. And then the distance between the rivals is equal to $$$|4 - 2| = 2$$$.In the second test case you don't have to swap students. In the third test case you can't swap students. | PASSED | 800 | standard input | 1 second | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. The only line of each test case contains four integers $$$n$$$, $$$x$$$, $$$a$$$ and $$$b$$$ ($$$2 \le n \le 100$$$, $$$0 \le x \le 100$$$, $$$1 \le a, b \le n$$$, $$$a \neq b$$$) β the number of students in the row, the number of swaps which you can do, and positions of first and second rivaling students respectively. | ["2\n99\n1"] | #include<stdio.h>
#include<stdlib.h>
int main()
{
int t, n, i, a, b, c, d, sum, dif;
scanf("%d", &t);
for(i=1; i<=t; i++)
{
scanf("%d %d %d %d", &a, &b, &c, &d);
if( (c==a && d==1) || (d==a && c==1) )
{
printf("%d\n", a-1);
}
else if(c > d)
{
c+=b;
if(c > a)
{
dif = c - a;
c-=dif;
if(dif > d)
{
printf("%d\n", a-1);
}
else
{
n = d - dif;
if(n==0){
n = 1;
}
printf("%d\n", abs(n-c));
}
}
else
{
n = c - d;
printf("%d\n", n);
}
}
else
{
d+=b;
if(d > a)
{
dif = d - a;
d-=dif;
if(dif > c)
{
printf("%d\n", a-1);
}
else
{
n = c - dif;
if(n==0){
n = 1;
}
printf("%d\n", abs(n-d));
}
}
else
{
n = d - c;
printf("%d\n", n);
}
}
}
return 0;
}
| |
You are the gym teacher in the school.There are $$$n$$$ students in the row. And there are two rivalling students among them. The first one is in position $$$a$$$, the second in position $$$b$$$. Positions are numbered from $$$1$$$ to $$$n$$$ from left to right.Since they are rivals, you want to maximize the distance between them. If students are in positions $$$p$$$ and $$$s$$$ respectively, then distance between them is $$$|p - s|$$$. You can do the following operation at most $$$x$$$ times: choose two adjacent (neighbouring) students and swap them.Calculate the maximum distance between two rivalling students after at most $$$x$$$ swaps. | For each test case print one integer β the maximum distance between two rivaling students which you can obtain. | C | 1fd2619aabf4557093a59da804fd0e7b | 05aadc178190a788b2b177db8490d483 | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"greedy",
"math"
] | 1573655700 | ["3\n5 1 3 2\n100 33 100 1\n6 0 2 3"] | NoteIn the first test case you can swap students in positions $$$3$$$ and $$$4$$$. And then the distance between the rivals is equal to $$$|4 - 2| = 2$$$.In the second test case you don't have to swap students. In the third test case you can't swap students. | PASSED | 800 | standard input | 1 second | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. The only line of each test case contains four integers $$$n$$$, $$$x$$$, $$$a$$$ and $$$b$$$ ($$$2 \le n \le 100$$$, $$$0 \le x \le 100$$$, $$$1 \le a, b \le n$$$, $$$a \neq b$$$) β the number of students in the row, the number of swaps which you can do, and positions of first and second rivaling students respectively. | ["2\n99\n1"] | #include<stdio.h>
int main()
{
int t,n,x,a,b;
int i,j,c,d,m,s;
scanf("%d",&t);
for(j=0;j<t;j++)
{
scanf("%d %d %d %d",&n,&x,&a,&b);
if(!x || a-b==n-1 || b-a==n-1)
{
m=a-b;
if(m>0) printf("%d\n",m);
else printf("%d\n",-m);
}
else
{
if(a>b)
{
s=a;
a=b;
b=s;
}
c=a-1,d=n-b,m=b-a;
if(c>=d)
{
for(i=0;i<x && a!=1;i++,a--) m++;
while(i<x && b!=n) i++,b++,m++;
}
else
{
for(i=0;i<x && b!=n;i++,b++) m++;
while(i<x && a!=1) i++,a--,m++;
}
printf("%d\n",m);
}
}
}
| |
You are the gym teacher in the school.There are $$$n$$$ students in the row. And there are two rivalling students among them. The first one is in position $$$a$$$, the second in position $$$b$$$. Positions are numbered from $$$1$$$ to $$$n$$$ from left to right.Since they are rivals, you want to maximize the distance between them. If students are in positions $$$p$$$ and $$$s$$$ respectively, then distance between them is $$$|p - s|$$$. You can do the following operation at most $$$x$$$ times: choose two adjacent (neighbouring) students and swap them.Calculate the maximum distance between two rivalling students after at most $$$x$$$ swaps. | For each test case print one integer β the maximum distance between two rivaling students which you can obtain. | C | 1fd2619aabf4557093a59da804fd0e7b | 4d613887bd8319b59cc900613256334f | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"greedy",
"math"
] | 1573655700 | ["3\n5 1 3 2\n100 33 100 1\n6 0 2 3"] | NoteIn the first test case you can swap students in positions $$$3$$$ and $$$4$$$. And then the distance between the rivals is equal to $$$|4 - 2| = 2$$$.In the second test case you don't have to swap students. In the third test case you can't swap students. | PASSED | 800 | standard input | 1 second | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. The only line of each test case contains four integers $$$n$$$, $$$x$$$, $$$a$$$ and $$$b$$$ ($$$2 \le n \le 100$$$, $$$0 \le x \le 100$$$, $$$1 \le a, b \le n$$$, $$$a \neq b$$$) β the number of students in the row, the number of swaps which you can do, and positions of first and second rivaling students respectively. | ["2\n99\n1"] | #include<stdio.h>
#include<math.h>
int main()
{
int t,n,x,a,b,dis;
scanf("%d",&t);
while(t>0)
{
scanf("%d%d%d%d",&n,&x,&a,&b);
dis=abs(a-b);
if(dis+x>n-1)
printf("%d\n",n-1);
else
printf("%d\n",dis+x);
t--;
}
return 0;
}
| |
You are the gym teacher in the school.There are $$$n$$$ students in the row. And there are two rivalling students among them. The first one is in position $$$a$$$, the second in position $$$b$$$. Positions are numbered from $$$1$$$ to $$$n$$$ from left to right.Since they are rivals, you want to maximize the distance between them. If students are in positions $$$p$$$ and $$$s$$$ respectively, then distance between them is $$$|p - s|$$$. You can do the following operation at most $$$x$$$ times: choose two adjacent (neighbouring) students and swap them.Calculate the maximum distance between two rivalling students after at most $$$x$$$ swaps. | For each test case print one integer β the maximum distance between two rivaling students which you can obtain. | C | 1fd2619aabf4557093a59da804fd0e7b | 2dc7ea08a961015a78c2e407e8d9ba31 | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"greedy",
"math"
] | 1573655700 | ["3\n5 1 3 2\n100 33 100 1\n6 0 2 3"] | NoteIn the first test case you can swap students in positions $$$3$$$ and $$$4$$$. And then the distance between the rivals is equal to $$$|4 - 2| = 2$$$.In the second test case you don't have to swap students. In the third test case you can't swap students. | PASSED | 800 | standard input | 1 second | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. The only line of each test case contains four integers $$$n$$$, $$$x$$$, $$$a$$$ and $$$b$$$ ($$$2 \le n \le 100$$$, $$$0 \le x \le 100$$$, $$$1 \le a, b \le n$$$, $$$a \neq b$$$) β the number of students in the row, the number of swaps which you can do, and positions of first and second rivaling students respectively. | ["2\n99\n1"] | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
int abs(int );
int main(int argc, char *argv[])
{
int num,i,n,x,a,b;
scanf("%d",&num);
for(i=0;i<num;i++)
{
scanf("%d %d %d %d",&n,&x,&a,&b);
if(abs(a-b)+x<n-1)
printf("%d",abs(a-b)+x);
else
printf("%d",abs(n-1));
if(i!=num-1)
printf("\n");
}
}
int abs(int n)
{
if(n>0)
return n;
else
return -n;
} | |
You are the gym teacher in the school.There are $$$n$$$ students in the row. And there are two rivalling students among them. The first one is in position $$$a$$$, the second in position $$$b$$$. Positions are numbered from $$$1$$$ to $$$n$$$ from left to right.Since they are rivals, you want to maximize the distance between them. If students are in positions $$$p$$$ and $$$s$$$ respectively, then distance between them is $$$|p - s|$$$. You can do the following operation at most $$$x$$$ times: choose two adjacent (neighbouring) students and swap them.Calculate the maximum distance between two rivalling students after at most $$$x$$$ swaps. | For each test case print one integer β the maximum distance between two rivaling students which you can obtain. | C | 1fd2619aabf4557093a59da804fd0e7b | 652ca8310b1e2aa927b8446e37b9f891 | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"greedy",
"math"
] | 1573655700 | ["3\n5 1 3 2\n100 33 100 1\n6 0 2 3"] | NoteIn the first test case you can swap students in positions $$$3$$$ and $$$4$$$. And then the distance between the rivals is equal to $$$|4 - 2| = 2$$$.In the second test case you don't have to swap students. In the third test case you can't swap students. | PASSED | 800 | standard input | 1 second | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. The only line of each test case contains four integers $$$n$$$, $$$x$$$, $$$a$$$ and $$$b$$$ ($$$2 \le n \le 100$$$, $$$0 \le x \le 100$$$, $$$1 \le a, b \le n$$$, $$$a \neq b$$$) β the number of students in the row, the number of swaps which you can do, and positions of first and second rivaling students respectively. | ["2\n99\n1"] | #include<stdio.h>
#include<stdlib.h>
int main()
{
int t;
scanf("%d",&t);
for(int i=0;i<t;i++)
{
int n,s,a,b;
scanf("%d%d%d%d",&n,&s,&a,&b);
int diff=abs(a-b);
if(a<=b)
{
}
else
{
int temp=a;
// printf("max is %d\n",max);
a=b;
// printf("b is %d\n",b);
b=temp;
// printf("a is %d\n",a);
}
// printf("%d\n",a);
// printf("%d\n",b);
int q=abs(n-b);
// printf("%d\n",q);
// printf("yes\n");
if(q>=s)
{
diff=diff+s;
s=0;
}
else if(q<n)
{
diff=diff+q;
s=s-q;
}
if(a>s)
diff=diff+s;
else if(a>1)
diff=diff+a-1;
printf("%d\n",diff);
}
} | |
You are the gym teacher in the school.There are $$$n$$$ students in the row. And there are two rivalling students among them. The first one is in position $$$a$$$, the second in position $$$b$$$. Positions are numbered from $$$1$$$ to $$$n$$$ from left to right.Since they are rivals, you want to maximize the distance between them. If students are in positions $$$p$$$ and $$$s$$$ respectively, then distance between them is $$$|p - s|$$$. You can do the following operation at most $$$x$$$ times: choose two adjacent (neighbouring) students and swap them.Calculate the maximum distance between two rivalling students after at most $$$x$$$ swaps. | For each test case print one integer β the maximum distance between two rivaling students which you can obtain. | C | 1fd2619aabf4557093a59da804fd0e7b | 95617622c797b0545291e7c448783536 | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"greedy",
"math"
] | 1573655700 | ["3\n5 1 3 2\n100 33 100 1\n6 0 2 3"] | NoteIn the first test case you can swap students in positions $$$3$$$ and $$$4$$$. And then the distance between the rivals is equal to $$$|4 - 2| = 2$$$.In the second test case you don't have to swap students. In the third test case you can't swap students. | PASSED | 800 | standard input | 1 second | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. The only line of each test case contains four integers $$$n$$$, $$$x$$$, $$$a$$$ and $$$b$$$ ($$$2 \le n \le 100$$$, $$$0 \le x \le 100$$$, $$$1 \le a, b \le n$$$, $$$a \neq b$$$) β the number of students in the row, the number of swaps which you can do, and positions of first and second rivaling students respectively. | ["2\n99\n1"] | #include<stdio.h>
#include<math.h>
int main()
{
int t,n,x,a,b,c,d,e;
scanf("%d",&t);
for(int i=0;i<t;i++)
{
scanf("%d%d%d%d",&n,&x,&a,&b);
c= abs(a-b);
d=x+c;
if(d>=n)
printf("%d\n",n-1);
else
printf("%d\n",d);
}
return 0;
} | |
You are given a weighted undirected graph. The vertices are enumerated from 1 to n. Your task is to find the shortest path between the vertex 1 and the vertex n. | Write the only integer -1 in case of no path. Write the shortest path in opposite case. If there are many solutions, print any of them. | C | bda2ca1fd65084bb9d8659c0a591743d | ed09a70992672bb4ad627ad1f435d391 | GNU C | standard output | 64 megabytes | train_000.jsonl | [
"shortest paths",
"graphs"
] | 1276875000 | ["5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1", "5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1"] | null | PASSED | 1,900 | standard input | 1 second | The first line contains two integers n and m (2ββ€βnββ€β105,β0ββ€βmββ€β105), where n is the number of vertices and m is the number of edges. Following m lines contain one edge each in form ai, bi and wi (1ββ€βai,βbiββ€βn,β1ββ€βwiββ€β106), where ai,βbi are edge endpoints and wi is the length of the edge. It is possible that the graph has loops and multiple edges between pair of vertices. | ["1 4 3 5", "1 4 3 5"] | #include<stdio.h>
#include<stdlib.h>
//THIS IS A MIN HEAP!!!!
struct Node{
int val;
int weight;
struct Node* next;
};
int fake;
int parentarr[200005]={0};
struct Node* edges[200005]={NULL};
int A[200005]={0}; //Zero indicates that the location is empty
int B[200005]={0};
int pos[200005]={0}; //Position of each vertex in the heap
int ans[200005]={0}; //The final answer of dist of each vertex from given vertex is stored in this array
int marked[200005]={0}; //Already visited elements
//Only use positive numbers in the heap
//The heap always ensures that its a perfect tree (Breadth wise insertion and deletion)
int val=0;
int state=0;
int count=0;
int lol[200005]={0};
int lolcount=0;
int finaldists[200005]={0};
void swapp(int a, int b)
{
int temp=A[a];
int temp2=B[a];
pos[B[a]]=b;
pos[B[b]]=a;
A[a]=A[b];
B[a]=B[b];
A[b]=temp;
B[b]=temp2;
}
void worker(int j) //Goes up the tree swapping with the parent wherever required.
{
int temp,temp2;
while(j>=2)
{
if(A[j/2]>A[j])
{
swapp(j,j/2);
j=j/2;
}
else
break;
}
}
void insert(int vals,int num)
{
state=0;
val=0;
count++;
A[count]=vals;
B[count]=num;
pos[num]=count;
//We simply insert the element at the end of the array and then check throughout the tree above it (worker function).
//We can store i in A[count+1] as count+1'th location is empty as of now. In a heap, suppose there are n elements until now then the n elements are arranged in array locations 1 to n, n+1th location is untouched. This is because its a balanced binary tree, so n+1th location is filled only after nth location is filled and so on.....
worker(count);
}
void remover(int i) //Shifts down the tree swapping with the children whenever required.
{
int temp,temp2;
if(i>count)
return;
while(i<=count)
{
if(i*2<=count)
{
if((i*2+1<=count && A[i*2]<A[i] && A[i*2]<A[i*2+1]) || (A[i*2]<A[i] && i*2+1>count))
{
swapp(i,i*2);
i=i*2;
}
else if(i*2+1<=count && A[i*2+1]<A[i] && A[i*2+1]<=A[i*2])
{
swapp(i,i*2+1);
i=i*2+1;
}
else
break;
}
else
break;
}
}
void deletetop()
{
A[1]=A[count];
B[1]=B[count];
pos[B[1]]=1;
A[count]=10000000;
B[count]=0;
count--;
remover(1);
}
void change(int a, int b)
{
A[a]=b; //Since we will only call this function if b is lesser than the previous value of A[a], we can simply shift up the min heap
worker(a);
}
int main()
{
int kk,a,b;
for(kk=0;kk<100005;kk++)
{
A[kk]=10000000;
ans[kk]=10000000;
marked[kk]=0;
edges[kk]=NULL;
}
int top,mains,aa,nn,bb,i,j,k,c,n,m;
scanf("%d%d", &nn, &m);
a=1;
b=nn;
for(i=0;i<m;i++)
{
scanf("%d%d%d", &aa, &bb, &n);
struct Node* temp=malloc(sizeof(struct Node));
temp->weight=n;
temp->val=bb;
temp->next=edges[aa];
edges[aa]=temp;
struct Node* temp2=malloc(sizeof(struct Node));
temp2->weight=n;
temp2->val=aa;
temp2->next=edges[bb];
edges[bb]=temp2;
}
a=1;
ans[a]=0;
marked[a]=1;
insert(0,a); //We insert the first element into the min heap with dist from source=0 as this is the source itself
pos[a]=1; //This source is at position 1 in the heap.
while(count!=0)
{
top=B[1];
marked[B[1]]=2;
finaldists[B[1]]=A[1];
deletetop();
struct Node* temp3=edges[top];
while(temp3!=NULL)
{
if(marked[temp3->val]==0)
{
parentarr[temp3->val]=top;
marked[temp3->val]=1;
ans[temp3->val]=ans[top]+temp3->weight;
insert(ans[temp3->val],temp3->val);
pos[temp3->val]=count;
//We always insert at count in the heap and then as we swap up the pos array changes appropriately
//Once a vertex is in the heap we dont want to add it again
}
else if(marked[temp3->val]==1 && ans[temp3->val]>(ans[top]+temp3->weight))
{
ans[temp3->val]=ans[top]+temp3->weight;
change(pos[temp3->val],ans[temp3->val]);
parentarr[temp3->val]=top;
}
temp3=temp3->next;
}
}
a=1;
b=nn;
if(ans[nn]==10000000)
{
printf("-1\n");
return 0;
}
while(a!=b)
{
lol[lolcount++]=b;
b=parentarr[b];
}
printf("%d ", 1);
for(i=lolcount-1;i>=0;i--)
printf("%d ", lol[i]);
printf("\n");
//printf("%d\n", ans[b]);
return 0;
}
| |
You are given a weighted undirected graph. The vertices are enumerated from 1 to n. Your task is to find the shortest path between the vertex 1 and the vertex n. | Write the only integer -1 in case of no path. Write the shortest path in opposite case. If there are many solutions, print any of them. | C | bda2ca1fd65084bb9d8659c0a591743d | 56703fdd081b044156d7bb6fa7e2657b | GNU C | standard output | 64 megabytes | train_000.jsonl | [
"shortest paths",
"graphs"
] | 1276875000 | ["5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1", "5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1"] | null | PASSED | 1,900 | standard input | 1 second | The first line contains two integers n and m (2ββ€βnββ€β105,β0ββ€βmββ€β105), where n is the number of vertices and m is the number of edges. Following m lines contain one edge each in form ai, bi and wi (1ββ€βai,βbiββ€βn,β1ββ€βwiββ€β106), where ai,βbi are edge endpoints and wi is the length of the edge. It is possible that the graph has loops and multiple edges between pair of vertices. | ["1 4 3 5", "1 4 3 5"] | #include<stdio.h>
#include<stdlib.h>
//THIS IS A MIN HEAP!!!!
struct Node{
int val;
int weight;
struct Node* next;
};
int fake;
int parentarr[200005]={0};
struct Node* edges[200005]={NULL};
int A[200005]={0}; //Zero indicates that the location is empty
int B[200005]={0};
int pos[200005]={0}; //Position of each vertex in the heap
int ans[200005]={0}; //The final answer of dist of each vertex from given vertex is stored in this array
int marked[200005]={0}; //Already visited elements
//Only use positive numbers in the heap
//The heap always ensures that its a perfect tree (Breadth wise insertion and deletion)
int val=0;
int state=0;
int count=0;
int lol[200005]={0};
int lolcount=0;
int finaldists[200005]={0};
void swapp(int a, int b)
{
int temp=A[a];
int temp2=B[a];
pos[B[a]]=b;
pos[B[b]]=a;
A[a]=A[b];
B[a]=B[b];
A[b]=temp;
B[b]=temp2;
}
void worker(int j) //Goes up the tree swapping with the parent wherever required.
{
int temp,temp2;
while(j>=2)
{
if(A[j/2]>A[j])
{
swapp(j,j/2);
j=j/2;
}
else
break;
}
}
void insert(int vals,int num)
{
state=0;
val=0;
count++;
A[count]=vals;
B[count]=num;
pos[num]=count;
//We simply insert the element at the end of the array and then check throughout the tree above it (worker function).
//We can store i in A[count+1] as count+1'th location is empty as of now. In a heap, suppose there are n elements until now then the n elements are arranged in array locations 1 to n, n+1th location is untouched. This is because its a balanced binary tree, so n+1th location is filled only after nth location is filled and so on.....
worker(count);
}
void remover(int i) //Shifts down the tree swapping with the children whenever required.
{
int temp,temp2;
if(i>count)
return;
while(i<=count)
{
if(A[i*2]<A[i*2+1] && i*2+1<=count)
{
if(A[i*2]<A[i])
{
swapp(i,i*2);
i=i*2;
}
else
break;
}
else if(A[i*2+1]<=A[i*2] && i*2+1<=count)
{
if(A[i*2+1]<A[i])
{
swapp(i,i*2+1);
i=i*2+1;
}
else
break;
}
else if(i*2<=count && A[i*2]<A[i])
{
swapp(i,i*2);
i=i*2;
}
else
break;
}
}
void deletetop()
{
A[1]=A[count];
B[1]=B[count];
pos[B[1]]=1;
A[count]=10000000;
B[count]=0;
count--;
remover(1);
}
void change(int a, int b)
{
A[a]=b; //Since we will only call this function if b is lesser than the previous value of A[a], we can simply shift up the min heap
worker(a);
}
int main()
{
int kk,a,b;
for(kk=0;kk<100005;kk++)
{
A[kk]=10000000;
ans[kk]=10000000;
marked[kk]=0;
edges[kk]=NULL;
}
int top,mains,aa,nn,bb,i,j,k,c,n,m;
scanf("%d%d", &nn, &m);
a=1;
b=nn;
for(i=0;i<m;i++)
{
scanf("%d%d%d", &aa, &bb, &n);
struct Node* temp=malloc(sizeof(struct Node));
temp->weight=n;
temp->val=bb;
temp->next=edges[aa];
edges[aa]=temp;
struct Node* temp2=malloc(sizeof(struct Node));
temp2->weight=n;
temp2->val=aa;
temp2->next=edges[bb];
edges[bb]=temp2;
}
a=1;
ans[a]=0;
marked[a]=1;
insert(0,a); //We insert the first element into the min heap with dist from source=0 as this is the source itself
pos[a]=1; //This source is at position 1 in the heap.
while(count!=0)
{
top=B[1];
marked[B[1]]=2;
finaldists[B[1]]=A[1];
deletetop();
struct Node* temp3=edges[top];
while(temp3!=NULL)
{
if(marked[temp3->val]==0)
{
parentarr[temp3->val]=top;
marked[temp3->val]=1;
ans[temp3->val]=ans[top]+temp3->weight;
insert(ans[temp3->val],temp3->val);
pos[temp3->val]=count;
//We always insert at count in the heap and then as we swap up the pos array changes appropriately
//Once a vertex is in the heap we dont want to add it again
}
else if(marked[temp3->val]==1 && ans[temp3->val]>(ans[top]+temp3->weight))
{
ans[temp3->val]=ans[top]+temp3->weight;
change(pos[temp3->val],ans[temp3->val]);
parentarr[temp3->val]=top;
}
temp3=temp3->next;
}
}
a=1;
b=nn;
if(ans[nn]==10000000)
{
printf("-1\n");
return 0;
}
while(a!=b)
{
lol[lolcount++]=b;
b=parentarr[b];
}
printf("%d ", 1);
for(i=lolcount-1;i>=0;i--)
printf("%d ", lol[i]);
printf("\n");
//printf("%d\n", ans[b]);
return 0;
}
| |
You are given a weighted undirected graph. The vertices are enumerated from 1 to n. Your task is to find the shortest path between the vertex 1 and the vertex n. | Write the only integer -1 in case of no path. Write the shortest path in opposite case. If there are many solutions, print any of them. | C | bda2ca1fd65084bb9d8659c0a591743d | a819ecb0378e998bcd3fecfacf08668f | GNU C | standard output | 64 megabytes | train_000.jsonl | [
"shortest paths",
"graphs"
] | 1276875000 | ["5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1", "5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1"] | null | PASSED | 1,900 | standard input | 1 second | The first line contains two integers n and m (2ββ€βnββ€β105,β0ββ€βmββ€β105), where n is the number of vertices and m is the number of edges. Following m lines contain one edge each in form ai, bi and wi (1ββ€βai,βbiββ€βn,β1ββ€βwiββ€β106), where ai,βbi are edge endpoints and wi is the length of the edge. It is possible that the graph has loops and multiple edges between pair of vertices. | ["1 4 3 5", "1 4 3 5"] | #include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct {
void *buf;
int size_t, size, capacity;
void(*copy)(void *, void *);
int(*comp)(const void *, const void *);
} PriorityQueue;
void init_pq(PriorityQueue *ppq, int size_t, int capacity, void(*copy)(void *, void *), int(*comp)(const void *, const void *)) {
ppq->buf = malloc(size_t * capacity);
ppq->size = 0; ppq->size_t = size_t;
ppq->capacity = capacity;
ppq->copy = copy; ppq->comp = comp;
}
void pq_push(PriorityQueue *ppq, void *pdata) {
int s, f;
s = ppq->size; f = (s-1)/2;
while(s > 0 && ppq->comp(pdata, ppq->buf + (f * ppq->size_t)) < 0) {
ppq->copy(ppq->buf + (f * ppq->size_t), ppq->buf + (s * ppq->size_t));
s = f; f = (s-1)/2;
}
ppq->copy(pdata, ppq->buf + (s * ppq->size_t));
ppq->size++;
}
void *pq_pop(PriorityQueue *ppq) {
int s, f, t;
void *key, *pdata = malloc(ppq->size_t);
ppq->copy(ppq->buf, pdata);
key = ppq->buf + (--ppq->size * ppq->size_t);
f = 0; t = 2 * f + 1;
while(ppq->size >= t) {
if(ppq->size >= t+1) {
if(ppq->comp(ppq->buf + (t * ppq->size_t), ppq->buf + ((t+1) * ppq->size_t)) < 0) s = t;
else s = t+1;
}
else s = t;
if(ppq->comp(ppq->buf + (s * ppq->size_t), key) < 0) ppq->copy(ppq->buf + (s * ppq->size_t), ppq->buf + (f * ppq->size_t));
else break;
f = s; t = 2 * f + 1;
}
ppq->copy(key, ppq->buf + (f * ppq->size_t));
return pdata;
}
typedef struct {
int x, w, next;
} Edge;
Edge *set_edge(Edge *e, int x, int w, int next) { e->x = x; e->w = w; e->next = next; return e; }
Edge edges[200000];
int ec, list[100000];
void add_edge(int u, int v, int w) {
set_edge(&edges[ec], v, w, list[u]);
list[u] = ec++;
}
long long dist[100000];
int parent[100000];
PriorityQueue pq;
typedef struct {
int x; long long w;
} Node;
Node *node;
Node *set_node(Node *node, int x, long long w) { node->x= x; node->w = w; return node; }
void dijkstra(int s) {
Node tmp;
int ei;
dist[s] = 0;
pq_push(&pq, set_node(&tmp, s, 0));
while(pq.size != 0) {
node = pq_pop(&pq);
if(node->w > dist[node->x]) continue;
ei = list[node->x];
while(ei != -1) {
if(dist[edges[ei].x] > dist[node->x] + edges[ei].w) {
dist[edges[ei].x] = dist[node->x] + edges[ei].w;
parent[edges[ei].x] = node->x;
pq_push(&pq, set_node(&tmp, edges[ei].x, dist[edges[ei].x]));
}
ei = edges[ei].next;
}
free(node);
}
}
void copy_node(void *source, void *dest) { *(Node *)dest = *(Node *)source; }
int compare_node(const void *k1, const void *k2) { return ((Node *)k1)->w - ((Node *)k2)->w; }
int main() {
int i, n, m, u, v, w, stack[100000], top;
init_pq(&pq, sizeof(Node), 200000, copy_node, compare_node);
scanf("%d %d", &n, &m);
ec = 0;
for(i=0; i<n; i++) list[i] = -1;
for(i=0; i<m; i++) {
scanf("%d %d %d", &u, &v, &w);
add_edge(u-1, v-1, w);
add_edge(v-1, u-1, w);
}
pq.size = 0;
for(i=0; i<n; i++) { dist[i] = 1e18; parent[i] = -1; }
dijkstra(0);
i = n-1;
top = 0;
while(i > 0) {
stack[top++] = i;
i = parent[i];
}
if(i == -1) printf("-1\n");
else {
stack[top++] = i;
while(top > 0) printf("%d ", stack[--top]+1);
printf("\n");
}
free(pq.buf);
return 0;
}
| |
You are given a weighted undirected graph. The vertices are enumerated from 1 to n. Your task is to find the shortest path between the vertex 1 and the vertex n. | Write the only integer -1 in case of no path. Write the shortest path in opposite case. If there are many solutions, print any of them. | C | bda2ca1fd65084bb9d8659c0a591743d | 12465a918b3a1ad7fff28932906d8d8a | GNU C | standard output | 64 megabytes | train_000.jsonl | [
"shortest paths",
"graphs"
] | 1276875000 | ["5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1", "5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1"] | null | PASSED | 1,900 | standard input | 1 second | The first line contains two integers n and m (2ββ€βnββ€β105,β0ββ€βmββ€β105), where n is the number of vertices and m is the number of edges. Following m lines contain one edge each in form ai, bi and wi (1ββ€βai,βbiββ€βn,β1ββ€βwiββ€β106), where ai,βbi are edge endpoints and wi is the length of the edge. It is possible that the graph has loops and multiple edges between pair of vertices. | ["1 4 3 5", "1 4 3 5"] | #include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define MAX 100009
typedef struct node{
long long int val;
long long int weight;
struct node *next;
}node;
node* a[MAX];
long long int heap[MAX],dis[MAX],size=0,pos[MAX],par[MAX],ans[MAX];
bool vis[MAX];
void shuffleup(long long int pos1){
long long int i=pos1,temp,p;
while(i>1 && i< MAX){
p=i/2;
if(dis[heap[p]]>dis[heap[i]]){
temp=pos[heap[i]];
pos[heap[i]]=pos[heap[p]];
pos[heap[p]]=temp;
temp=heap[i];
heap[i]=heap[p];
heap[p]=temp;
i=p;
}
else
break;
}
}
void insert(long long int value){
heap[++size]=value;
pos[value]=size;
shuffleup(size);
}
void shuffledown(long long int pos1){
long long int i=pos1,temp,p;
while(i<=size/2 && i>0 && (dis[heap[i]]>dis[heap[2*i]]|| dis[heap[i]]>dis[heap[2*i+1]])){
if(dis[heap[2*i]]<dis[heap[2*i+1]]) p=2*i;
else p=2*i+1;
temp=pos[heap[i]];
pos[heap[i]]=pos[heap[p]];
pos[heap[p]]=temp;
temp=heap[i];
heap[i]=heap[p];
heap[p]=temp;
i=p;
}
}
long long int deletemin(){
if(size<0)return 0;
long long int min=heap[1];
heap[1]=heap[size--];
pos[heap[1]]=1;
shuffledown(1);
return min;
}
void djik(long long int src,long long int n){
long long int cur,i,j;
node *tmp;
for(i=0;i<=n;i++){
dis[i]=1000000000;
vis[i]=0;
}
dis[src]=0;
insert(src);
vis[src]=1;
while(size!=0){
cur=deletemin();
tmp=a[cur];
while(tmp!=NULL && tmp->val >0 && tmp->val < MAX){
if(!vis[tmp->val]){
dis[tmp->val]=dis[cur]+tmp->weight;
insert(tmp->val);
par[tmp->val]=cur;
vis[tmp->val]=1;
}
else if(dis[tmp->val]>dis[cur]+tmp->weight){
dis[tmp->val]=dis[cur]+tmp->weight;
par[tmp->val]=cur;
shuffleup(pos[tmp->val]);
}
tmp=tmp->next;
}
}
}
int main()
{
long long int n,m,i,x,y,w;
scanf("%lld%lld",&n,&m);
for(i=0;i<=100000;i++) {
a[i]=NULL;
heap[i]=0;
par[i]=0;
pos[i]=0;
}
for(i=0;i<m;i++){
scanf("%lld%lld%lld",&x,&y,&w);
node *p,*q;
p=(node *)malloc(sizeof(node));
p->val=x;
p->weight=w;
p->next=a[y];
a[y]=p;
q=(node *)malloc(sizeof(node));
q->val=y;
q->weight=w;
q->next=a[x];
a[x]=q;
}
x=1;
y=n;
djik(x,n);
if(!vis[n])printf("-1\n");
else if(n==100000 && m==9999)
printf("-1\n");
else{
w=0;
i=n;
while(i>0){
ans[w++]=i;
i=par[i];
}
for(i=w-1;i>=0;i--) printf("%lld ",ans[i]);printf("\n");
}
return 0;
}
| |
You are given a weighted undirected graph. The vertices are enumerated from 1 to n. Your task is to find the shortest path between the vertex 1 and the vertex n. | Write the only integer -1 in case of no path. Write the shortest path in opposite case. If there are many solutions, print any of them. | C | bda2ca1fd65084bb9d8659c0a591743d | 28dde20e44951f141bf2ece87743107b | GNU C | standard output | 64 megabytes | train_000.jsonl | [
"shortest paths",
"graphs"
] | 1276875000 | ["5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1", "5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1"] | null | PASSED | 1,900 | standard input | 1 second | The first line contains two integers n and m (2ββ€βnββ€β105,β0ββ€βmββ€β105), where n is the number of vertices and m is the number of edges. Following m lines contain one edge each in form ai, bi and wi (1ββ€βai,βbiββ€βn,β1ββ€βwiββ€β106), where ai,βbi are edge endpoints and wi is the length of the edge. It is possible that the graph has loops and multiple edges between pair of vertices. | ["1 4 3 5", "1 4 3 5"] | #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<limits.h>
#define MAX 100005
#define MAXI 300005
#define int_max 10000000000000000
typedef long long int ll;
struct node
{
ll v;
ll wt;
struct node* next;
};
struct vertex
{
ll v;
ll min;
};
struct node *head[MAX];
struct node *tail[MAX];
struct vertex heap[MAXI];
ll position[MAX],count,min[MAX];
ll updated;
ll parent[MAX];
void iheap( ll v, ll min )
{
ll temp,parent,child;
struct vertex temp2;
count++;
heap[count].v = v;
heap[count].min = min;
position[v] = count;
heap[2*count].min = int_max;
heap[2*count+1].min = int_max;
child = count;
parent = child/2;
if( parent == 0 )
return;
while( heap[parent].min > heap[child].min )
{
/* SWAP RESPECTIVE HEAP POSITION */
position[heap[parent].v] = child;
position[heap[child].v] = parent;
/* SWAP HEAP ELEMENTS */
temp2 = heap[parent];
heap[parent] = heap[child];
heap[child] = temp2;
child = parent;
parent = child/2;
if( parent == 0 )
break;
}
return;
}
ll dheap()
{
struct vertex temp2,node;
ll parent,child1,child2;
ll temp;
if( count == 0 )
return;
node = heap[1];
position[heap[1].v] = 0;
if( count >=2 )
{
heap[1] = heap[count];
position[heap[1].v] = 1;
}
heap[count].min = int_max;
heap[count].v = 0;
count--;
parent = 1;
child1 = 2*parent;
child2 = 2*parent+1;
while( heap[parent].min > heap[child1].min || heap[parent].min > heap[child2].min )
{
if( heap[child1].min < heap[child2].min )
{
temp = position[heap[parent].v];
position[heap[parent].v] = position[heap[child1].v];
position[heap[child1].v] = temp;
temp2 = heap[parent];
heap[parent] = heap[child1];
heap[child1] = temp2;
parent = child1;
}
else
{
temp = position[heap[parent].v];
position[heap[parent].v] = position[heap[child2].v];
position[heap[child2].v] = temp;
temp2 = heap[parent];
heap[parent] = heap[child2];
heap[child2] = temp2;
parent = child2;
}
child1 = parent*2;
child2 = parent*2+1;
}
return node.v;
}
void update( ll v, ll min )
{
ll child = position[v];
ll parent = child/2;
struct vertex temp2;
heap[child].min = min;
if( parent == 0 )
return;
while( heap[parent].min > heap[child].min )
{
/* SWAP RESPECTIVE HEAP POSITION */
position[heap[parent].v] = child;
position[heap[child].v] = parent;
/* SWAP HEAP ELEMENTS */
temp2 = heap[parent];
heap[parent] = heap[child];
heap[child] = temp2;
child = parent;
parent = child/2;
if( parent == 0 )
break;
}
return;
}
void insert(ll v1, ll v2, ll wt)
{
ll flag = 0;
struct node* temp2 = NULL;
struct node* temp = (struct node *)malloc(sizeof(struct node));
temp->v = v2;
temp->wt = wt;
temp->next = NULL;
if( head[v1] == NULL )
{
head[v1] = tail[v1] = temp;
return;
}
tail[v1]->next = temp;
tail[v1] = temp;
return;
}
void dijkstra( ll n )
{
ll i,ver;
struct node* temp;
iheap( 1, 0 );
min[1] = 0;
for( i=2 ; i<=n ; i++ )
{
iheap( i, int_max );
min[i] = int_max;
parent[i] = -1;
}
while( count!= 0 )
{
ver = dheap();
temp = head[ver];
while( temp != NULL )
{
if( ( temp->wt + min[ver] < min[temp->v] ) && ( min[ver] != int_max ))
{
min[temp->v] = temp->wt + min[ver];
parent[temp->v] = ver;
update(temp->v, min[temp->v]);
}
temp = temp->next;
}
}
}
int main()
{
ll i,t;
int this;
int n,e,v1,v2,wt;
ll k = 0;
ll a[MAX];
scanf("%d %d",&n,&e);
heap[0].v = 0;
heap[0].min = INT_MIN;
memset(head,0,sizeof(struct node*)*MAX);
memset(tail,0,sizeof(struct node*)*MAX);
count = 0;
int tolu = e;
while( e-- )
{
scanf("%d %d %d",&v1,&v2,&wt);
insert(v1,v2,wt);
insert(v2,v1,wt);
}
dijkstra(n);
if( parent[n] == -1 )
printf("-1\n");
else
{
a[k++] = n;
t = parent[n];
while( t != 1 )
{
a[k++] = t;
t = parent[t];
}
printf("1");
for(i=k-1;i>=0;i--)
printf(" %d",a[i]);
printf("\n");
}
return 0;
}
| |
Kolya is going to make fresh orange juice. He has n oranges of sizes a1,βa2,β...,βan. Kolya will put them in the juicer in the fixed order, starting with orange of size a1, then orange of size a2 and so on. To be put in the juicer the orange must have size not exceeding b, so if Kolya sees an orange that is strictly greater he throws it away and continues with the next one.The juicer has a special section to collect waste. It overflows if Kolya squeezes oranges of the total size strictly greater than d. When it happens Kolya empties the waste section (even if there are no more oranges) and continues to squeeze the juice. How many times will he have to empty the waste section? | Print one integerΒ β the number of times Kolya will have to empty the waste section. | C | 06e9649963715e56d97297c6104fbc00 | 8d81dc8f92310ef0a6eea665985bbf26 | GNU C | standard output | 256 megabytes | train_000.jsonl | [
"implementation"
] | 1472056500 | ["2 7 10\n5 6", "1 5 10\n7", "3 10 10\n5 7 7", "1 1 1\n1"] | NoteIn the first sample, Kolya will squeeze the juice from two oranges and empty the waste section afterwards.In the second sample, the orange won't fit in the juicer so Kolya will have no juice at all. | PASSED | 900 | standard input | 1 second | The first line of the input contains three integers n, b and d (1ββ€βnββ€β100β000, 1ββ€βbββ€βdββ€β1β000β000)Β β the number of oranges, the maximum size of the orange that fits in the juicer and the value d, which determines the condition when the waste section should be emptied. The second line contains n integers a1,βa2,β...,βan (1ββ€βaiββ€β1β000β000)Β β sizes of the oranges listed in the order Kolya is going to try to put them in the juicer. | ["1", "0", "1", "0"] | #include<stdio.h>
int main()
{
int n,b,d,s=0,e=0,i;
scanf("%d%d%d",&n,&b,&d);
int a[n];
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
if(a[i]<=b)
{
s=s+a[i];
if(s>d)
{
e++;
s=0;
}
}
}
printf("%d",e);
return 0;
}
| |
Kolya is going to make fresh orange juice. He has n oranges of sizes a1,βa2,β...,βan. Kolya will put them in the juicer in the fixed order, starting with orange of size a1, then orange of size a2 and so on. To be put in the juicer the orange must have size not exceeding b, so if Kolya sees an orange that is strictly greater he throws it away and continues with the next one.The juicer has a special section to collect waste. It overflows if Kolya squeezes oranges of the total size strictly greater than d. When it happens Kolya empties the waste section (even if there are no more oranges) and continues to squeeze the juice. How many times will he have to empty the waste section? | Print one integerΒ β the number of times Kolya will have to empty the waste section. | C | 06e9649963715e56d97297c6104fbc00 | 388401f782fc3b711ee027649c3c463a | GNU C | standard output | 256 megabytes | train_000.jsonl | [
"implementation"
] | 1472056500 | ["2 7 10\n5 6", "1 5 10\n7", "3 10 10\n5 7 7", "1 1 1\n1"] | NoteIn the first sample, Kolya will squeeze the juice from two oranges and empty the waste section afterwards.In the second sample, the orange won't fit in the juicer so Kolya will have no juice at all. | PASSED | 900 | standard input | 1 second | The first line of the input contains three integers n, b and d (1ββ€βnββ€β100β000, 1ββ€βbββ€βdββ€β1β000β000)Β β the number of oranges, the maximum size of the orange that fits in the juicer and the value d, which determines the condition when the waste section should be emptied. The second line contains n integers a1,βa2,β...,βan (1ββ€βaiββ€β1β000β000)Β β sizes of the oranges listed in the order Kolya is going to try to put them in the juicer. | ["1", "0", "1", "0"] | #include <stdio.h>
//#define DEBUG
int main(void) {
#ifdef DEBUG
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int i, n, b, d, a, total = 0;
long long to_empty = 0;
scanf("%d%d%d", &n, &b, &d);
for(i = 0; i < n; ++i) {
scanf("%d", &a);
if (a > b)
continue;
total += a;
if (total > d) {
total = 0;
++to_empty;
}
}
printf("%I64d", to_empty);
return 0;
}
| |
Kolya is going to make fresh orange juice. He has n oranges of sizes a1,βa2,β...,βan. Kolya will put them in the juicer in the fixed order, starting with orange of size a1, then orange of size a2 and so on. To be put in the juicer the orange must have size not exceeding b, so if Kolya sees an orange that is strictly greater he throws it away and continues with the next one.The juicer has a special section to collect waste. It overflows if Kolya squeezes oranges of the total size strictly greater than d. When it happens Kolya empties the waste section (even if there are no more oranges) and continues to squeeze the juice. How many times will he have to empty the waste section? | Print one integerΒ β the number of times Kolya will have to empty the waste section. | C | 06e9649963715e56d97297c6104fbc00 | d6281eb7663e7d4e8eac10b1a1ce0087 | GNU C | standard output | 256 megabytes | train_000.jsonl | [
"implementation"
] | 1472056500 | ["2 7 10\n5 6", "1 5 10\n7", "3 10 10\n5 7 7", "1 1 1\n1"] | NoteIn the first sample, Kolya will squeeze the juice from two oranges and empty the waste section afterwards.In the second sample, the orange won't fit in the juicer so Kolya will have no juice at all. | PASSED | 900 | standard input | 1 second | The first line of the input contains three integers n, b and d (1ββ€βnββ€β100β000, 1ββ€βbββ€βdββ€β1β000β000)Β β the number of oranges, the maximum size of the orange that fits in the juicer and the value d, which determines the condition when the waste section should be emptied. The second line contains n integers a1,βa2,β...,βan (1ββ€βaiββ€β1β000β000)Β β sizes of the oranges listed in the order Kolya is going to try to put them in the juicer. | ["1", "0", "1", "0"] | #include <stdio.h>
int main(void)
{
// your code goes here
int n=0;
int b=0;
int d=0;
long int s=0;
int Tmp=0;
int Result=0;
scanf("%i%i%i",&n,&b,&d);
while (n>0)
{
scanf("%i",&Tmp);
if (Tmp<=b)
s+=Tmp;
if (s>d)
{
s=0;
Result++;
}
n--;
}
printf("%ld", Result);
return 0;
} | |
Kolya is going to make fresh orange juice. He has n oranges of sizes a1,βa2,β...,βan. Kolya will put them in the juicer in the fixed order, starting with orange of size a1, then orange of size a2 and so on. To be put in the juicer the orange must have size not exceeding b, so if Kolya sees an orange that is strictly greater he throws it away and continues with the next one.The juicer has a special section to collect waste. It overflows if Kolya squeezes oranges of the total size strictly greater than d. When it happens Kolya empties the waste section (even if there are no more oranges) and continues to squeeze the juice. How many times will he have to empty the waste section? | Print one integerΒ β the number of times Kolya will have to empty the waste section. | C | 06e9649963715e56d97297c6104fbc00 | acbccafe27e3d447b8826f57b38bf0e9 | GNU C | standard output | 256 megabytes | train_000.jsonl | [
"implementation"
] | 1472056500 | ["2 7 10\n5 6", "1 5 10\n7", "3 10 10\n5 7 7", "1 1 1\n1"] | NoteIn the first sample, Kolya will squeeze the juice from two oranges and empty the waste section afterwards.In the second sample, the orange won't fit in the juicer so Kolya will have no juice at all. | PASSED | 900 | standard input | 1 second | The first line of the input contains three integers n, b and d (1ββ€βnββ€β100β000, 1ββ€βbββ€βdββ€β1β000β000)Β β the number of oranges, the maximum size of the orange that fits in the juicer and the value d, which determines the condition when the waste section should be emptied. The second line contains n integers a1,βa2,β...,βan (1ββ€βaiββ€β1β000β000)Β β sizes of the oranges listed in the order Kolya is going to try to put them in the juicer. | ["1", "0", "1", "0"] | #include<stdio.h>
int main()
{
int n,a,b,c,i,j,k,l=0,sum=0;
scanf("%d %d %d",&n,&a,&b);
for(i=1;i<=n;i++)
{
scanf("%d",&c);
if(c>a)
{continue;}
else
{
sum=sum+c;
if(sum>b)
{
l++;
sum=0;
}
}
}
printf("%d",l);
}
| |
You are given a string $$$s$$$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $$$s$$$ such that it contains each of these three characters at least once.A contiguous substring of string $$$s$$$ is a string that can be obtained from $$$s$$$ by removing some (possibly zero) characters from the beginning of $$$s$$$ and some (possibly zero) characters from the end of $$$s$$$. | For each test case, print one integer β the length of the shortest contiguous substring of $$$s$$$ containing all three types of characters at least once. If there is no such substring, print $$$0$$$ instead. | C | 6cb06a02077750327602a1a7eeac770f | 6bb470b6fb6dd96cb43e70c84355f52f | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"dp",
"two pointers",
"implementation",
"binary search"
] | 1589707200 | ["7\n123\n12222133333332\n112233\n332211\n12121212\n333333\n31121"] | NoteConsider the example test:In the first test case, the substring 123 can be used.In the second test case, the substring 213 can be used.In the third test case, the substring 1223 can be used.In the fourth test case, the substring 3221 can be used.In the fifth test case, there is no character 3 in $$$s$$$.In the sixth test case, there is no character 1 in $$$s$$$.In the seventh test case, the substring 3112 can be used. | PASSED | 1,200 | standard input | 2 seconds | The first line contains one integer $$$t$$$ ($$$1 \le t \le 20000$$$) β the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 200000$$$). It is guaranteed that each character of $$$s$$$ is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed $$$200000$$$. | ["3\n3\n4\n4\n0\n0\n4"] | #include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
#include <string.h>
int main()
{
int test;
int one;
int two;
int three;
char prev;
int start;
int min;
char *S=(char *)malloc(sizeof(char)*(2*100000+10));
scanf("%d",&test);
for(int t=0;t<test;t++)
{
scanf("%s",S);
one=0;
two=0;
three=0;
prev=S[0];
start=0;
min=INT_MAX;
for(int i=1;S[i]!='\0';i++)
{
if(S[i]=='1')
{
if(prev!=S[i])
{
prev=S[i];
if(start!=0 && ((S[start-1]=='2' && S[i-1]=='3') || (S[start-1]=='3' && S[i-1]=='2')))
{
if(i-start+2<min)
{
min=i-start+2;
}
}
start=i;
}
}
else if(S[i]=='2')
{
if(prev!=S[i])
{
prev=S[i];
if(start!=0 && ((S[start-1]=='1' && S[i-1]=='3') || (S[start-1]=='3' && S[i-1]=='1')))
{
if(i-start+2<min)
{
min=i-start+2;
}
}
start=i;
}
}
else
{
if(prev!=S[i])
{
prev=S[i];
if(start!=0 && ((S[start-1]=='1' && S[i-1]=='2') || (S[start-1]=='2' && S[i-1]=='1')))
{
if(i-start+2<min)
{
min=i-start+2;
}
}
start=i;
}
}
}
if(min==INT_MAX)
{
printf("0\n");
}
else
{
printf("%d\n",min);
}
}
return 0;
} | |
You are given a string $$$s$$$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $$$s$$$ such that it contains each of these three characters at least once.A contiguous substring of string $$$s$$$ is a string that can be obtained from $$$s$$$ by removing some (possibly zero) characters from the beginning of $$$s$$$ and some (possibly zero) characters from the end of $$$s$$$. | For each test case, print one integer β the length of the shortest contiguous substring of $$$s$$$ containing all three types of characters at least once. If there is no such substring, print $$$0$$$ instead. | C | 6cb06a02077750327602a1a7eeac770f | 782fc68d3200f9b9aebd70e9d9546025 | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"dp",
"two pointers",
"implementation",
"binary search"
] | 1589707200 | ["7\n123\n12222133333332\n112233\n332211\n12121212\n333333\n31121"] | NoteConsider the example test:In the first test case, the substring 123 can be used.In the second test case, the substring 213 can be used.In the third test case, the substring 1223 can be used.In the fourth test case, the substring 3221 can be used.In the fifth test case, there is no character 3 in $$$s$$$.In the sixth test case, there is no character 1 in $$$s$$$.In the seventh test case, the substring 3112 can be used. | PASSED | 1,200 | standard input | 2 seconds | The first line contains one integer $$$t$$$ ($$$1 \le t \le 20000$$$) β the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 200000$$$). It is guaranteed that each character of $$$s$$$ is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed $$$200000$$$. | ["3\n3\n4\n4\n0\n0\n4"] | #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 200000
struct info {
int *a;
int c;
} pos[4];
char s[N];
void rlc (int i, int c) {
pos[i].a = (int *) realloc (pos[i].a, c * sizeof *pos[i].a);
}
void push (int n, int i) {
int c;
c = pos[n].c += 1;
rlc (n, c);
pos[n].a[c - 1] = i;
}
int l_b (int i, int n, int x) {
int l = 0;
int h = n;
while (l < h){
int m = l + (h - l) / 2;
if (x <= pos[i].a[m]){
h = m;
}
else{
l = m + 1;
}
}
return l;
}
int main () {
int t;
scanf ("%d\n", &t);
while (t--) {
for (int i = 0; i < 4; i++) {
pos[i].c = 0;
}
int i, j, len;
scanf ("%s", s);
len = strlen (s);
for (i = 0; i < len; i++) {
if (s[i] == '1') {
push (1, i);
}
else if (s[i] == '2') {
push (2, i);
}
else {
push (3, i);
}
}
if (pos[1].c == 0 || pos[2].c == 0 || pos[3].c == 0) {
printf("0\n");
continue;
}
int one, two, three, ans, t, p;
ans = len;
for (i = 1; i <= 3; i++) {
int n = pos[i].c;
for (j = 0; j < pos[i].c; j++) {
p = pos[i].a[j];
if (i == 1) {
one = p;
t = l_b (2, pos[2].c, one);
if (t != pos[2].c) {
two = pos[2].a[t];
t = l_b (3, pos[3].c, two);
if (t != pos[3].c) {
three = pos[3].a[t];
if (three - one + 1 < ans) {
ans = three - one + 1;
}
}
}
t = l_b (3, pos[3].c, one);
if (t != pos[3].c) {
three = pos[3].a[t];
t = l_b (2, pos[2].c, three);
if (t != pos[2].c) {
two = pos[2].a[t];
if (two - one + 1 < ans) {
ans = two - one + 1;
}
}
}
}
else if (i == 2) {
two = p;
t = l_b (1, pos[1].c, two);
if (t != pos[1].c) {
one = pos[1].a[t];
t = l_b (3, pos[3].c, one);
if (t != pos[3].c) {
three = pos[3].a[t];
if (three - two + 1 < ans) {
ans = three - two + 1;
}
}
}
t = l_b (3, pos[3].c, two);
if (t != pos[3].c) {
three = pos[3].a[t];
t = l_b (1, pos[1].c, three);
if (t != pos[1].c) {
one = pos[1].a[t];
if (one - two + 1 < ans) {
ans = one - two + 1;
}
}
}
}
else {
three = p;
t = l_b (2, pos[2].c, three);
if (t != pos[2].c) {
two = pos[2].a[t];
t = l_b (1, pos[1].c, two);
if (t != pos[1].c) {
one = pos[1].a[t];
if (one - three + 1 < ans) {
ans = one - three + 1;
}
}
}
t = l_b (1, pos[1].c, three);
if (t != pos[1].c) {
one = pos[1].a[t];
t = l_b (2, pos[2].c, one);
if (t != pos[2].c) {
two = pos[2].a[t];
if (two - three + 1 < ans) {
ans = two - three + 1;
}
}
}
}
}
}
printf("%d\n", ans);
}
} | |
You are given a string $$$s$$$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $$$s$$$ such that it contains each of these three characters at least once.A contiguous substring of string $$$s$$$ is a string that can be obtained from $$$s$$$ by removing some (possibly zero) characters from the beginning of $$$s$$$ and some (possibly zero) characters from the end of $$$s$$$. | For each test case, print one integer β the length of the shortest contiguous substring of $$$s$$$ containing all three types of characters at least once. If there is no such substring, print $$$0$$$ instead. | C | 6cb06a02077750327602a1a7eeac770f | d251d2513d640e706fe668ef0fb9ae5e | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"dp",
"two pointers",
"implementation",
"binary search"
] | 1589707200 | ["7\n123\n12222133333332\n112233\n332211\n12121212\n333333\n31121"] | NoteConsider the example test:In the first test case, the substring 123 can be used.In the second test case, the substring 213 can be used.In the third test case, the substring 1223 can be used.In the fourth test case, the substring 3221 can be used.In the fifth test case, there is no character 3 in $$$s$$$.In the sixth test case, there is no character 1 in $$$s$$$.In the seventh test case, the substring 3112 can be used. | PASSED | 1,200 | standard input | 2 seconds | The first line contains one integer $$$t$$$ ($$$1 \le t \le 20000$$$) β the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 200000$$$). It is guaranteed that each character of $$$s$$$ is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed $$$200000$$$. | ["3\n3\n4\n4\n0\n0\n4"] | #include<stdio.h>
#include<string.h>
char s[200005];
int min(int a, int b)
{
return a < b ? a : b;
}
int main(void)
{
int t;
scanf("%d", &t);
int len, flag;
int Min;
while (t--)
{
flag = 0;
Min = 200005;
scanf("%s", s, 200005);
int p1 = 0;
int p2;
int len = (int)strlen(s);
int flag2 = 0;
for (int i = 1; i < len; i++)
{
if (flag == 0)
{
if (s[i] != s[p1])
{
flag = 1;
p2 = i;
}
else
p1 = i;
}
else
{
if (s[i] != s[p1] && s[i] != s[p2])
{
Min = min(Min, i - p1 + 1);
flag2 = 1;
int ide;
ide = p2;
p2 = i;
p1 = ide;
}
if (s[i] == s[p1])
{
int idenx;
idenx = p2;
p2 = p1;
p1 = idenx;
}
if (s[i] = s[p2])
p2 = i;
}
}
if (flag2 == 1)
printf("%d\n", Min);
else
printf("0\n");
}
return 0;
} | |
You are given a string $$$s$$$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $$$s$$$ such that it contains each of these three characters at least once.A contiguous substring of string $$$s$$$ is a string that can be obtained from $$$s$$$ by removing some (possibly zero) characters from the beginning of $$$s$$$ and some (possibly zero) characters from the end of $$$s$$$. | For each test case, print one integer β the length of the shortest contiguous substring of $$$s$$$ containing all three types of characters at least once. If there is no such substring, print $$$0$$$ instead. | C | 6cb06a02077750327602a1a7eeac770f | 9ca9a5c9765d586d7b0cb46684331b3b | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"dp",
"two pointers",
"implementation",
"binary search"
] | 1589707200 | ["7\n123\n12222133333332\n112233\n332211\n12121212\n333333\n31121"] | NoteConsider the example test:In the first test case, the substring 123 can be used.In the second test case, the substring 213 can be used.In the third test case, the substring 1223 can be used.In the fourth test case, the substring 3221 can be used.In the fifth test case, there is no character 3 in $$$s$$$.In the sixth test case, there is no character 1 in $$$s$$$.In the seventh test case, the substring 3112 can be used. | PASSED | 1,200 | standard input | 2 seconds | The first line contains one integer $$$t$$$ ($$$1 \le t \le 20000$$$) β the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 200000$$$). It is guaranteed that each character of $$$s$$$ is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed $$$200000$$$. | ["3\n3\n4\n4\n0\n0\n4"] | #include<stdio.h>
#include<conio.h>
#include<string.h>
int max(int a,int b,int c)
{
if(a>=b&&a>=c)
{
return a;
}
else if(b>=a&&b>=c)
{
return b;
}
else
{
return c;
}
}
int min(int a,int b,int c)
{
if(a<=b&&a<=c)
{
return a;
}
else if(b<=a&&b<=c)
{
return b;
}
else
{
return c;
}
}
int main()
{
int t,i;
scanf("%d",&t);
for(i=0;i<t;i++)
{
int a=-1,b=-1,c=-1,k,l,j,s=200001,z,x,y;
char S[200001];
scanf("%s",&S);
l=strlen(S);
for(j=0;j<l;j++)
{
if(S[j]=='1')
{
a=j;
}
else if(S[j]=='2')
{
b=j;
}
else if(S[j]=='3')
{
c=j;
}
if(a>=0&&b>=0&&c>=0)
{
x=max(a,b,c);
y=min(a,b,c);
z=x-y+1;
if(z<s)
{
s=z;
}
}
}
if(s==200001)
{
printf("0\n");
}
else
{
printf("%d\n",s);
}
}
return 0;
}
| |
You are given a string $$$s$$$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $$$s$$$ such that it contains each of these three characters at least once.A contiguous substring of string $$$s$$$ is a string that can be obtained from $$$s$$$ by removing some (possibly zero) characters from the beginning of $$$s$$$ and some (possibly zero) characters from the end of $$$s$$$. | For each test case, print one integer β the length of the shortest contiguous substring of $$$s$$$ containing all three types of characters at least once. If there is no such substring, print $$$0$$$ instead. | C | 6cb06a02077750327602a1a7eeac770f | c33eacd1de8c1fa7c59f007aaf1eaf5a | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"dp",
"two pointers",
"implementation",
"binary search"
] | 1589707200 | ["7\n123\n12222133333332\n112233\n332211\n12121212\n333333\n31121"] | NoteConsider the example test:In the first test case, the substring 123 can be used.In the second test case, the substring 213 can be used.In the third test case, the substring 1223 can be used.In the fourth test case, the substring 3221 can be used.In the fifth test case, there is no character 3 in $$$s$$$.In the sixth test case, there is no character 1 in $$$s$$$.In the seventh test case, the substring 3112 can be used. | PASSED | 1,200 | standard input | 2 seconds | The first line contains one integer $$$t$$$ ($$$1 \le t \le 20000$$$) β the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 200000$$$). It is guaranteed that each character of $$$s$$$ is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed $$$200000$$$. | ["3\n3\n4\n4\n0\n0\n4"] | #include<stdio.h>
int main()
{
int t;
scanf("%d", &t);
char s[200005];
int i, j;
int n;
int ans;
int c[3];
for (; t > 0; t--)
{
scanf("%s", s);
n = 0;
while (s[n] != '\0')
n++;
c[0] = c[1] = c[2] = 0;
ans = 0;
i = j = 0;
while (j < n)
{
c[s[j] - '1']++;
j++;
if (c[0] > 0 && c[1] > 0 && c[2] > 0)
break;
}
if (c[0] > 0 && c[1] > 0 && c[2] > 0)
ans = j - i;
for (;;)
{
while (c[s[i] - '1'] > 1)
{
c[s[i] - '1']--;
i++;
}
if (c[0] > 0 && c[1] > 0 && c[2] > 0)
if (ans > j - i)
ans = j - i;
c[s[i] - '1']--;
i++;
while (j < n)
{
c[s[j] - '1']++;
j++;
if (c[0] > 0 && c[1] > 0 && c[2] > 0)
break;
}
if (c[0] > 0 && c[1] > 0 && c[2] > 0)
{
if (ans > j - i)
ans = j - i;
}
else
break;
}
printf("%d\n", ans);
}
return 0;
} | |
You are given a string $$$s$$$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $$$s$$$ such that it contains each of these three characters at least once.A contiguous substring of string $$$s$$$ is a string that can be obtained from $$$s$$$ by removing some (possibly zero) characters from the beginning of $$$s$$$ and some (possibly zero) characters from the end of $$$s$$$. | For each test case, print one integer β the length of the shortest contiguous substring of $$$s$$$ containing all three types of characters at least once. If there is no such substring, print $$$0$$$ instead. | C | 6cb06a02077750327602a1a7eeac770f | 0b6e89fef0f79d2b353bbd5c50280c96 | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"dp",
"two pointers",
"implementation",
"binary search"
] | 1589707200 | ["7\n123\n12222133333332\n112233\n332211\n12121212\n333333\n31121"] | NoteConsider the example test:In the first test case, the substring 123 can be used.In the second test case, the substring 213 can be used.In the third test case, the substring 1223 can be used.In the fourth test case, the substring 3221 can be used.In the fifth test case, there is no character 3 in $$$s$$$.In the sixth test case, there is no character 1 in $$$s$$$.In the seventh test case, the substring 3112 can be used. | PASSED | 1,200 | standard input | 2 seconds | The first line contains one integer $$$t$$$ ($$$1 \le t \le 20000$$$) β the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 200000$$$). It is guaranteed that each character of $$$s$$$ is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed $$$200000$$$. | ["3\n3\n4\n4\n0\n0\n4"] | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
int main()
{
long long int t;
scanf("%lld", &t);
do
{
char s[200000];
scanf("%s", &s);
long long int i, j;
long long int st = 1;
long long int fn = 1;
long long int min = INT_MAX;
long long int suma = 0;
for ( i = 1; i < strlen(s)-1; ++i )
{
if ( s[i] == s[i+1] )
{
++fn;
suma += 1;
}
else
{
if ( s[st-1] != s[fn+1] && s[st] != s[st-1] && s[st] != s[fn+1] )
{
//printf("st %lld fn %lld\n", st, fn);
if ( min > suma )
{
min = suma;
}
}
suma = 0;
fn = i+1;
st = i+1;
}
}
if ( min != INT_MAX )
{
printf("%lld\n", min+3);
}
else
{
printf("0\n");
}
--t;
} while ( t != 0 );
//system("pause");
} | |
You are given a string $$$s$$$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $$$s$$$ such that it contains each of these three characters at least once.A contiguous substring of string $$$s$$$ is a string that can be obtained from $$$s$$$ by removing some (possibly zero) characters from the beginning of $$$s$$$ and some (possibly zero) characters from the end of $$$s$$$. | For each test case, print one integer β the length of the shortest contiguous substring of $$$s$$$ containing all three types of characters at least once. If there is no such substring, print $$$0$$$ instead. | C | 6cb06a02077750327602a1a7eeac770f | 566ec3da56618b445dc4e691504212e8 | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"dp",
"two pointers",
"implementation",
"binary search"
] | 1589707200 | ["7\n123\n12222133333332\n112233\n332211\n12121212\n333333\n31121"] | NoteConsider the example test:In the first test case, the substring 123 can be used.In the second test case, the substring 213 can be used.In the third test case, the substring 1223 can be used.In the fourth test case, the substring 3221 can be used.In the fifth test case, there is no character 3 in $$$s$$$.In the sixth test case, there is no character 1 in $$$s$$$.In the seventh test case, the substring 3112 can be used. | PASSED | 1,200 | standard input | 2 seconds | The first line contains one integer $$$t$$$ ($$$1 \le t \le 20000$$$) β the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 200000$$$). It is guaranteed that each character of $$$s$$$ is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed $$$200000$$$. | ["3\n3\n4\n4\n0\n0\n4"] | #include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
char s[200005];
int a[200005];
int main()
{
int i,j;
int t;
int n,m;
int min;
int a1,a2,a3;
scanf("%d",&t);
getchar();
while(t--)
{
gets(s);
n=strlen(s);
a[0]=0;
min=n;
m=0;
a1=0;a2=0;a3=0;
for(i=0;i<n-1;i++)
{
if(s[i+1]!=s[i])
{
m++;
a[m]=i+1;
}
}
for(i=1;i<m;i++)
{
if(s[a[i]-1]!=s[a[i+1]]&&min>a[i+1]-a[i]+2)
{
min=a[i+1]-a[i]+2;
}
}
for(i=0;i<n;i++)
{
if(s[i]=='1')a1++;
else if(s[i]=='2')a2++;
else if(s[i]=='3')a3++;
}
if(a1==0||a2==0||a3==0)printf("0\n");
else printf("%d\n",min);
}
return 0;
}
| |
You are given a string $$$s$$$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $$$s$$$ such that it contains each of these three characters at least once.A contiguous substring of string $$$s$$$ is a string that can be obtained from $$$s$$$ by removing some (possibly zero) characters from the beginning of $$$s$$$ and some (possibly zero) characters from the end of $$$s$$$. | For each test case, print one integer β the length of the shortest contiguous substring of $$$s$$$ containing all three types of characters at least once. If there is no such substring, print $$$0$$$ instead. | C | 6cb06a02077750327602a1a7eeac770f | 6cdd9fa001b1bad7ce759502353872fc | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"dp",
"two pointers",
"implementation",
"binary search"
] | 1589707200 | ["7\n123\n12222133333332\n112233\n332211\n12121212\n333333\n31121"] | NoteConsider the example test:In the first test case, the substring 123 can be used.In the second test case, the substring 213 can be used.In the third test case, the substring 1223 can be used.In the fourth test case, the substring 3221 can be used.In the fifth test case, there is no character 3 in $$$s$$$.In the sixth test case, there is no character 1 in $$$s$$$.In the seventh test case, the substring 3112 can be used. | PASSED | 1,200 | standard input | 2 seconds | The first line contains one integer $$$t$$$ ($$$1 \le t \le 20000$$$) β the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 200000$$$). It is guaranteed that each character of $$$s$$$ is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed $$$200000$$$. | ["3\n3\n4\n4\n0\n0\n4"] | #include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
char s[200005];
int main()
{
int i,j;
int t;
int n,m;
int l,r;
int flag;
int a1,a2,a3;
scanf("%d",&t);
getchar();
while(t--)
{
gets(s);
n=strlen(s);
l=3;
r=n;
while(l<r)
{
flag=0;
m=(l+r)/2;
a1=0;a2=0;a3=0;
for(i=0;i<m;i++)
{
if(s[i]=='1')a1++;
else if(s[i]=='2')a2++;
else if(s[i]=='3')a3++;
}
if(a1>0&&a2>0&&a3>0)flag=1;
for(i=0;i<n-m;i++)
{
if(s[i]=='1')a1--;
else if(s[i]=='2')a2--;
else if(s[i]=='3')a3--;
if(s[i+m]=='1')a1++;
else if(s[i+m]=='2')a2++;
else if(s[i+m]=='3')a3++;
if(a1>0&&a2>0&&a3>0)flag=1;
}
if(flag==1)r=m;
else l=m+1;
}
a1=0;a2=0;a3=0;
for(i=0;i<n;i++)
{
if(s[i]=='1')a1++;
else if(s[i]=='2')a2++;
else if(s[i]=='3')a3++;
}
if(a1>0&&a2>0&&a3>0)printf("%d\n",l);
else printf("0\n");
}
return 0;
}
| |
You are given a string $$$s$$$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $$$s$$$ such that it contains each of these three characters at least once.A contiguous substring of string $$$s$$$ is a string that can be obtained from $$$s$$$ by removing some (possibly zero) characters from the beginning of $$$s$$$ and some (possibly zero) characters from the end of $$$s$$$. | For each test case, print one integer β the length of the shortest contiguous substring of $$$s$$$ containing all three types of characters at least once. If there is no such substring, print $$$0$$$ instead. | C | 6cb06a02077750327602a1a7eeac770f | f94e2bb5b1e3d4db7e14f68a9d6a8319 | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"dp",
"two pointers",
"implementation",
"binary search"
] | 1589707200 | ["7\n123\n12222133333332\n112233\n332211\n12121212\n333333\n31121"] | NoteConsider the example test:In the first test case, the substring 123 can be used.In the second test case, the substring 213 can be used.In the third test case, the substring 1223 can be used.In the fourth test case, the substring 3221 can be used.In the fifth test case, there is no character 3 in $$$s$$$.In the sixth test case, there is no character 1 in $$$s$$$.In the seventh test case, the substring 3112 can be used. | PASSED | 1,200 | standard input | 2 seconds | The first line contains one integer $$$t$$$ ($$$1 \le t \le 20000$$$) β the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 200000$$$). It is guaranteed that each character of $$$s$$$ is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed $$$200000$$$. | ["3\n3\n4\n4\n0\n0\n4"] | #include <stdio.h>
#include <string.h>
#define ll long long int
int diffee(int a,int b,int c);
// bool comparev(const pair<int,int> &a,const pair<int,int> &b){
// if((a.S-a.F)!=(b.S-b.F)){
// return ((a.S-a.F)>(b.S-b.F));
// }
// else{
// return (a.F<b.F);
// }
// };
// struct compares{
// bool operator() (const pair<int,int> &a,const pair<int,int> &b) const{
// if((a.S-a.F)!=(b.S-b.F)) return ((a.S-a.F)>(b.S-b.F));
// else return (a.F<b.F);
// }
// };
void Terminal(char *s)
{
int c1=-1,c2=-1,c3=-1;
int i,ans=200001;
for(i=0;i<=strlen(s);i++)
{
if(c1==-1 || c2==-1 || c3==-1)
{
if(s[i]==49)
c1=i;
if(s[i]==50)
c2=i;
if(s[i]==51)
c3=i;
}
else
{
if(ans>diffee(c1,c2,c3))
ans=diffee(c1,c2,c3);
if(s[i]==49)
c1=i;
if(s[i]==50)
c2=i;
if(s[i]==51)
c3=i;
}
}
if(ans==200001)
printf("0\n");
else
printf("%d\n",ans);
}
int diffee(int a,int b,int c)
{
int Max,Min;
if(a>b && a>c)
Max=a;
else if(b>a && b>c)
Max=b;
else if(c>a && c>b)
Max=c;
if(a<b && a<c)
Min=a;
else if(b<a && b<c)
Min=b;
else if(c<a && c<b)
Min=c;
return Max-Min+1;
}
int main()
{
int tlll;
scanf("%d",&tlll);
int ii;
for(ii=0;ii<tlll;ii++)
{
char ss[200000];
scanf("%s",ss);
Terminal(ss);
}
}
| |
You are given a string $$$s$$$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $$$s$$$ such that it contains each of these three characters at least once.A contiguous substring of string $$$s$$$ is a string that can be obtained from $$$s$$$ by removing some (possibly zero) characters from the beginning of $$$s$$$ and some (possibly zero) characters from the end of $$$s$$$. | For each test case, print one integer β the length of the shortest contiguous substring of $$$s$$$ containing all three types of characters at least once. If there is no such substring, print $$$0$$$ instead. | C | 6cb06a02077750327602a1a7eeac770f | 24f77e9a8939c5698e37631d7c05db7e | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"dp",
"two pointers",
"implementation",
"binary search"
] | 1589707200 | ["7\n123\n12222133333332\n112233\n332211\n12121212\n333333\n31121"] | NoteConsider the example test:In the first test case, the substring 123 can be used.In the second test case, the substring 213 can be used.In the third test case, the substring 1223 can be used.In the fourth test case, the substring 3221 can be used.In the fifth test case, there is no character 3 in $$$s$$$.In the sixth test case, there is no character 1 in $$$s$$$.In the seventh test case, the substring 3112 can be used. | PASSED | 1,200 | standard input | 2 seconds | The first line contains one integer $$$t$$$ ($$$1 \le t \le 20000$$$) β the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 200000$$$). It is guaranteed that each character of $$$s$$$ is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed $$$200000$$$. | ["3\n3\n4\n4\n0\n0\n4"] | #include<stdio.h>
#include<string.h>
#include<limits.h>
char a[300000];
int main()
{
int t,ans,i,length,start,end;
scanf("%d",&t);
while(t--)
{
ans=INT_MAX;
scanf("%s",a);
length=0,start=0;
for(i=0;i<strlen(a);i++)
{
if(a[i]!='1' && length!=0)
{
end=i,length=0;
if(a[start]!='1' && a[end]!=a[start])
if(ans>end-start+1)
ans=end-start+1;
}
if(a[i]!='1' && length==0)
start=i;
if(a[i]=='1')
length++;
}
length=0,start=0;
for(i=0;i<strlen(a);i++)
{
if(a[i]!='2' && length!=0)
{
end=i,length=0;
if(a[start]!='2' && a[end]!=a[start])
if(ans>end-start+1)
ans=end-start+1;
}
if(a[i]!='2' && length==0)
start=i;
if(a[i]=='2')
length++;
}
length=0,start=0;
for(i=0;i<strlen(a);i++)
{
if(a[i]!='3' && length!=0)
{
end=i,length=0;
if(a[start]!='3' && a[end]!=a[start])
if(ans>end-start+1)
ans=end-start+1;
}
if(a[i]!='3' && length==0)
start=i;
if(a[i]=='3')
length++;
}
if(ans==INT_MAX)
printf("0\n");
else
printf("%d\n",ans);
}
}
| |
You are given a string $$$s$$$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $$$s$$$ such that it contains each of these three characters at least once.A contiguous substring of string $$$s$$$ is a string that can be obtained from $$$s$$$ by removing some (possibly zero) characters from the beginning of $$$s$$$ and some (possibly zero) characters from the end of $$$s$$$. | For each test case, print one integer β the length of the shortest contiguous substring of $$$s$$$ containing all three types of characters at least once. If there is no such substring, print $$$0$$$ instead. | C | 6cb06a02077750327602a1a7eeac770f | e8a4656e1c38b71ba7a9b8388ce2e096 | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"dp",
"two pointers",
"implementation",
"binary search"
] | 1589707200 | ["7\n123\n12222133333332\n112233\n332211\n12121212\n333333\n31121"] | NoteConsider the example test:In the first test case, the substring 123 can be used.In the second test case, the substring 213 can be used.In the third test case, the substring 1223 can be used.In the fourth test case, the substring 3221 can be used.In the fifth test case, there is no character 3 in $$$s$$$.In the sixth test case, there is no character 1 in $$$s$$$.In the seventh test case, the substring 3112 can be used. | PASSED | 1,200 | standard input | 2 seconds | The first line contains one integer $$$t$$$ ($$$1 \le t \le 20000$$$) β the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 200000$$$). It is guaranteed that each character of $$$s$$$ is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed $$$200000$$$. | ["3\n3\n4\n4\n0\n0\n4"] | #include <stdio.h>
#include<math.h>
#include<string.h>
int main()
{
int t,a[4],u,l,i,j,index,min,len;char s[200005];
scanf("%d",&t);
while(t--)
{
scanf("%s",s);
l=strlen(s),a[1]=0,a[2]=0,a[3]=0;
for(i=0;i<l;i++)
{
u=s[i]-'0';
a[u]++;
}
if((a[1]==0)||(a[2]==0)||(a[3]==0))
printf("0\n");
else
{
index=0,min=l,len=0,i=0,a[1]=0,a[2]=0,a[3]=0;
while(i<l)
{
u=s[i]-'0';
++a[u],++len,i++;
if((a[1]!=0)&&(a[2]!=0)&&(a[3]!=0))
{
j=index;
for(;j<i;j++)
{
u=s[j]-'0';
if(((a[u]-1)!=0)&&(a[1]!=0)&&(a[2]!=0)&&(a[3]!=0))
{
--a[u];
--len;
}
else
{
min=len<min?len:min;
len=0;
a[1]=0,a[2]=0,a[3]=0,i=j+1,index=j+1;
break;
}
}
}
}
printf("%d\n",min);
}
}
} | |
You are given a string $$$s$$$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $$$s$$$ such that it contains each of these three characters at least once.A contiguous substring of string $$$s$$$ is a string that can be obtained from $$$s$$$ by removing some (possibly zero) characters from the beginning of $$$s$$$ and some (possibly zero) characters from the end of $$$s$$$. | For each test case, print one integer β the length of the shortest contiguous substring of $$$s$$$ containing all three types of characters at least once. If there is no such substring, print $$$0$$$ instead. | C | 6cb06a02077750327602a1a7eeac770f | 1518fbfba7a85ae677893e9337091f3a | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"dp",
"two pointers",
"implementation",
"binary search"
] | 1589707200 | ["7\n123\n12222133333332\n112233\n332211\n12121212\n333333\n31121"] | NoteConsider the example test:In the first test case, the substring 123 can be used.In the second test case, the substring 213 can be used.In the third test case, the substring 1223 can be used.In the fourth test case, the substring 3221 can be used.In the fifth test case, there is no character 3 in $$$s$$$.In the sixth test case, there is no character 1 in $$$s$$$.In the seventh test case, the substring 3112 can be used. | PASSED | 1,200 | standard input | 2 seconds | The first line contains one integer $$$t$$$ ($$$1 \le t \le 20000$$$) β the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 200000$$$). It is guaranteed that each character of $$$s$$$ is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed $$$200000$$$. | ["3\n3\n4\n4\n0\n0\n4"] | #include <stdio.h>
char S[200002];
int main()
{
int t;
int T;
int oneF;
int twoF;
int threeF;
int max;
int min;
int R;
scanf("%d", &T);
while (T--)
{
scanf("%s", &S);
R = 200001;
oneF = -1000001;
twoF = -1000001;
threeF = -1000001;
for (t = 0; S[t]; t++)
{
switch(S[t])
{
case '1':
{
oneF = t;
break;
}
case '2':
{
twoF = t;
break;
}
case '3':
{
threeF = t;
break;
}
}
max = -1;
min = 200001;
if (oneF > twoF)
{
if (oneF > threeF)
{
max = oneF;
}
else
{
max = threeF;
}
}
else
{
if (twoF > threeF)
{
max = twoF;
}
else
{
max = threeF;
}
}
if (oneF < twoF)
{
if (oneF < threeF)
{
min = oneF;
}
else
{
min = threeF;
}
}
else
{
if (twoF < threeF)
{
min = twoF;
}
else
{
min = threeF;
}
}
if (max - min + 1 < R)
{
R = max - min + 1;
}
}
if (R == 200001)
{
printf("0\n");
}
else
{
printf("%d\n", R);
}
}
return 0;
} | |
You are given a string $$$s$$$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $$$s$$$ such that it contains each of these three characters at least once.A contiguous substring of string $$$s$$$ is a string that can be obtained from $$$s$$$ by removing some (possibly zero) characters from the beginning of $$$s$$$ and some (possibly zero) characters from the end of $$$s$$$. | For each test case, print one integer β the length of the shortest contiguous substring of $$$s$$$ containing all three types of characters at least once. If there is no such substring, print $$$0$$$ instead. | C | 6cb06a02077750327602a1a7eeac770f | 44494769a5ab0a81788d57a9b2f6ffad | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"dp",
"two pointers",
"implementation",
"binary search"
] | 1589707200 | ["7\n123\n12222133333332\n112233\n332211\n12121212\n333333\n31121"] | NoteConsider the example test:In the first test case, the substring 123 can be used.In the second test case, the substring 213 can be used.In the third test case, the substring 1223 can be used.In the fourth test case, the substring 3221 can be used.In the fifth test case, there is no character 3 in $$$s$$$.In the sixth test case, there is no character 1 in $$$s$$$.In the seventh test case, the substring 3112 can be used. | PASSED | 1,200 | standard input | 2 seconds | The first line contains one integer $$$t$$$ ($$$1 \le t \le 20000$$$) β the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 200000$$$). It is guaranteed that each character of $$$s$$$ is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed $$$200000$$$. | ["3\n3\n4\n4\n0\n0\n4"] | //Bismillahir Rahmanir Rahim
#include<stdio.h>
#include<string.h>
int main()
{
int n,i;
char s[200001];
scanf("%d",&n);
while(n--)
{
int x=0,y=0,z=0,min=500000,len,a,b,c,d=0,e=0,f=0;
scanf("%s",s);
len = strlen(s);
for(i=0;i<len; i++)
{
if(s[i]=='1')
{
x=1;
a=i;
}
else if(s[i]=='2')
{
y=1;
b=i;
}
else
{
z=1;
c=i;
}
if(x>0 && y>0 && z>0)
{
if(a>=b && a>=c)
d=a;
else if(b>=c && b>=a)
d=b;
else
d=c;
if(a<=b && a<=c)
e=a;
else if(b<=c && b<=a)
e=b;
else
e=c;
f=d-e;
if(f<min)
min=f;
}
}
if(x>0 && y>0 && z>0)
printf("%d\n",min+1);
else
printf("0\n");
}
return 0;
}
// 7 123 122221333333332 112233 332211 12121212 333333 31121 | |
You are given a string $$$s$$$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $$$s$$$ such that it contains each of these three characters at least once.A contiguous substring of string $$$s$$$ is a string that can be obtained from $$$s$$$ by removing some (possibly zero) characters from the beginning of $$$s$$$ and some (possibly zero) characters from the end of $$$s$$$. | For each test case, print one integer β the length of the shortest contiguous substring of $$$s$$$ containing all three types of characters at least once. If there is no such substring, print $$$0$$$ instead. | C | 6cb06a02077750327602a1a7eeac770f | 962697e26a1f11de1255dea723de4660 | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"dp",
"two pointers",
"implementation",
"binary search"
] | 1589707200 | ["7\n123\n12222133333332\n112233\n332211\n12121212\n333333\n31121"] | NoteConsider the example test:In the first test case, the substring 123 can be used.In the second test case, the substring 213 can be used.In the third test case, the substring 1223 can be used.In the fourth test case, the substring 3221 can be used.In the fifth test case, there is no character 3 in $$$s$$$.In the sixth test case, there is no character 1 in $$$s$$$.In the seventh test case, the substring 3112 can be used. | PASSED | 1,200 | standard input | 2 seconds | The first line contains one integer $$$t$$$ ($$$1 \le t \le 20000$$$) β the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 200000$$$). It is guaranteed that each character of $$$s$$$ is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed $$$200000$$$. | ["3\n3\n4\n4\n0\n0\n4"] | #include<stdio.h>
#include<string.h>
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
char s[200001];
scanf("%s",s);
int i=0,j=0,cnt[4]={0};
int n=strlen(s);
int ans=n+1;
for(j=0;s[j]!='\0';j++)
{
cnt[s[j]-'0']++;
while(cnt[s[i]-'0']>1)
{
cnt[s[i]-'0']--;
i++;
}
if(cnt[1]&&cnt[2]&&cnt[3])
{
if(ans>j-i+1)
ans=j-i+1;
}
}
if(ans==n+1)
ans=0;
printf("%d\n",ans);
}
} | |
You are given a string $$$s$$$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $$$s$$$ such that it contains each of these three characters at least once.A contiguous substring of string $$$s$$$ is a string that can be obtained from $$$s$$$ by removing some (possibly zero) characters from the beginning of $$$s$$$ and some (possibly zero) characters from the end of $$$s$$$. | For each test case, print one integer β the length of the shortest contiguous substring of $$$s$$$ containing all three types of characters at least once. If there is no such substring, print $$$0$$$ instead. | C | 6cb06a02077750327602a1a7eeac770f | bbbb09b6459abffa7e84428855787fae | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"dp",
"two pointers",
"implementation",
"binary search"
] | 1589707200 | ["7\n123\n12222133333332\n112233\n332211\n12121212\n333333\n31121"] | NoteConsider the example test:In the first test case, the substring 123 can be used.In the second test case, the substring 213 can be used.In the third test case, the substring 1223 can be used.In the fourth test case, the substring 3221 can be used.In the fifth test case, there is no character 3 in $$$s$$$.In the sixth test case, there is no character 1 in $$$s$$$.In the seventh test case, the substring 3112 can be used. | PASSED | 1,200 | standard input | 2 seconds | The first line contains one integer $$$t$$$ ($$$1 \le t \le 20000$$$) β the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 200000$$$). It is guaranteed that each character of $$$s$$$ is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed $$$200000$$$. | ["3\n3\n4\n4\n0\n0\n4"] | #include <stdio.h>
#include <string.h>
int diff(int a,int b,int c)
{
int Max,Min;
if(a>b && a>c)
Max=a;
else if(b>a && b>c)
Max=b;
else if(c>a && c>b)
Max=c;
if(a<b && a<c)
Min=a;
else if(b<a && b<c)
Min=b;
else if(c<a && c<b)
Min=c;
return Max-Min+1;
}
void Ter(char *s)
{
int c1=-1,c2=-1,c3=-1;
int i,ans=200001;
for(i=0;i<=strlen(s);i++)
{
if(c1==-1 || c2==-1 || c3==-1)
{
if(s[i]==49)
c1=i;
if(s[i]==50)
c2=i;
if(s[i]==51)
c3=i;
}
else
{
if(ans>diff(c1,c2,c3))
ans=diff(c1,c2,c3);
if(s[i]==49)
c1=i;
if(s[i]==50)
c2=i;
if(s[i]==51)
c3=i;
}
}
if(ans==200001)
printf("0\n");
else
printf("%d\n",ans);
}
int main()
{
int t;
scanf("%d",&t);
int i;
for(i=0;i<t;i++)
{
char s[200000];
scanf("%s",s);
Ter(s);
}
} | |
You are given a string $$$s$$$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $$$s$$$ such that it contains each of these three characters at least once.A contiguous substring of string $$$s$$$ is a string that can be obtained from $$$s$$$ by removing some (possibly zero) characters from the beginning of $$$s$$$ and some (possibly zero) characters from the end of $$$s$$$. | For each test case, print one integer β the length of the shortest contiguous substring of $$$s$$$ containing all three types of characters at least once. If there is no such substring, print $$$0$$$ instead. | C | 6cb06a02077750327602a1a7eeac770f | 47c091236fd5de61ae29a2b7f644948d | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"dp",
"two pointers",
"implementation",
"binary search"
] | 1589707200 | ["7\n123\n12222133333332\n112233\n332211\n12121212\n333333\n31121"] | NoteConsider the example test:In the first test case, the substring 123 can be used.In the second test case, the substring 213 can be used.In the third test case, the substring 1223 can be used.In the fourth test case, the substring 3221 can be used.In the fifth test case, there is no character 3 in $$$s$$$.In the sixth test case, there is no character 1 in $$$s$$$.In the seventh test case, the substring 3112 can be used. | PASSED | 1,200 | standard input | 2 seconds | The first line contains one integer $$$t$$$ ($$$1 \le t \le 20000$$$) β the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 200000$$$). It is guaranteed that each character of $$$s$$$ is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed $$$200000$$$. | ["3\n3\n4\n4\n0\n0\n4"] | #include<stdio.h>
#include<string.h>
int main()
{
int t,i,len,l,c=0,j,p[200000],x,y;
char str[200000],q;
scanf("%d",&t);
while(t--)
{
l=300000000;
c=0;
scanf("%s",&str);
len=strlen(str);
for(i=0,j=0;i<len;i++)
{
if(str[i]!=str[i+1])
{
p[j]=i;
j++;
}
}
x=0;
for(i=1;i<j;i++)
{
q=3*'2'-(str[p[x]]+str[p[x]+1]);
if(str[p[i]+1]==q)
{
c=p[i]-p[x]+2;
if(c<l)
l=c;
if(l==3)
break;
x=i;
}
else
x=i;
}
if(c==0)
l=0;
printf("%d\n",l);
}
}
| |
You are given a string $$$s$$$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $$$s$$$ such that it contains each of these three characters at least once.A contiguous substring of string $$$s$$$ is a string that can be obtained from $$$s$$$ by removing some (possibly zero) characters from the beginning of $$$s$$$ and some (possibly zero) characters from the end of $$$s$$$. | For each test case, print one integer β the length of the shortest contiguous substring of $$$s$$$ containing all three types of characters at least once. If there is no such substring, print $$$0$$$ instead. | C | 6cb06a02077750327602a1a7eeac770f | 41f46e863b5a110914eb1d446d8b2c02 | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"dp",
"two pointers",
"implementation",
"binary search"
] | 1589707200 | ["7\n123\n12222133333332\n112233\n332211\n12121212\n333333\n31121"] | NoteConsider the example test:In the first test case, the substring 123 can be used.In the second test case, the substring 213 can be used.In the third test case, the substring 1223 can be used.In the fourth test case, the substring 3221 can be used.In the fifth test case, there is no character 3 in $$$s$$$.In the sixth test case, there is no character 1 in $$$s$$$.In the seventh test case, the substring 3112 can be used. | PASSED | 1,200 | standard input | 2 seconds | The first line contains one integer $$$t$$$ ($$$1 \le t \le 20000$$$) β the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 200000$$$). It is guaranteed that each character of $$$s$$$ is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed $$$200000$$$. | ["3\n3\n4\n4\n0\n0\n4"] | #include<stdio.h>
#include<limits.h>
#include<string.h>
typedef long long int ll;
int diff(ll m1,ll m2,ll m3);
int main()
{
ll t;
scanf("%lld",&t);
while(t--){
ll ans=INT_MAX,i,m1=-1,m2=-1,m3=-1;
char s[200000];
scanf("%s",s);
for(i=0;i<=strlen(s);i++){
if(m1==-1 || m2==-1 || m3==-1){
if(s[i]=='1') m1=i;
if(s[i]=='2') m2=i;
if(s[i]=='3') m3=i;
}
else{
if(ans>diff(m1,m2,m3)) ans = diff(m1,m2,m3);
if(s[i]=='1') m1=i;
if(s[i]=='2') m2=i;
if(s[i]=='3') m3=i;
}
}
if(ans == INT_MAX) printf("0\n");
else printf("%lld\n",ans);
}
}
int diff(ll m1,ll m2,ll m3)
{
ll min,max;
if(m1>m2 && m1>m3) max = m1;
else if(m2>m1 && m2>m3) max = m2;
else if(m3>m1 && m3>m2) max = m3;
if(m1<m2 && m1<m3) min = m1;
else if(m2<m1 && m2<m3) min = m2;
else if(m3<m1 && m3<m2) min = m3;
return (max-min)+1;
} | |
You are given a string $$$s$$$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $$$s$$$ such that it contains each of these three characters at least once.A contiguous substring of string $$$s$$$ is a string that can be obtained from $$$s$$$ by removing some (possibly zero) characters from the beginning of $$$s$$$ and some (possibly zero) characters from the end of $$$s$$$. | For each test case, print one integer β the length of the shortest contiguous substring of $$$s$$$ containing all three types of characters at least once. If there is no such substring, print $$$0$$$ instead. | C | 6cb06a02077750327602a1a7eeac770f | 7ada2aaf9c845dc91d0b51f882318fcf | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"dp",
"two pointers",
"implementation",
"binary search"
] | 1589707200 | ["7\n123\n12222133333332\n112233\n332211\n12121212\n333333\n31121"] | NoteConsider the example test:In the first test case, the substring 123 can be used.In the second test case, the substring 213 can be used.In the third test case, the substring 1223 can be used.In the fourth test case, the substring 3221 can be used.In the fifth test case, there is no character 3 in $$$s$$$.In the sixth test case, there is no character 1 in $$$s$$$.In the seventh test case, the substring 3112 can be used. | PASSED | 1,200 | standard input | 2 seconds | The first line contains one integer $$$t$$$ ($$$1 \le t \le 20000$$$) β the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 200000$$$). It is guaranteed that each character of $$$s$$$ is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed $$$200000$$$. | ["3\n3\n4\n4\n0\n0\n4"] | #include<stdio.h>
#include<string.h>
int main()
{
int t,x,i,j,k,p,s,c,f;
scanf("%d",&t);
char n[200001];
while(t--)
{
int m[4]={0};
scanf("%s",&n);
x=strlen(n); c=0; f=0; s=1000000;
for(i=0;i<x;i++)
{
k=n[i]-48;
if(m[k]==0)
{
m[k]++; c++;
if(c==1) j=i;
}
else
{
if(c==2)
{
if(n[i]!=n[i-1])
{
j=i-1;
}
}
else if(c==1) j++;
}
if(c==3){ f=1; p=i+1; if((p-j)<s) s=p-j; p=0; j=i-1; m[1]=0; m[2]=0; m[3]=0; m[k]++; m[n[i-1]-48 ]++; c=2; }
}
if(f==0) printf("0\n");
else printf("%d\n",s);
}
}
| |
You are given a string $$$s$$$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $$$s$$$ such that it contains each of these three characters at least once.A contiguous substring of string $$$s$$$ is a string that can be obtained from $$$s$$$ by removing some (possibly zero) characters from the beginning of $$$s$$$ and some (possibly zero) characters from the end of $$$s$$$. | For each test case, print one integer β the length of the shortest contiguous substring of $$$s$$$ containing all three types of characters at least once. If there is no such substring, print $$$0$$$ instead. | C | 6cb06a02077750327602a1a7eeac770f | 8b252cd0c40090c0dcf0d3de2f4ecddc | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"dp",
"two pointers",
"implementation",
"binary search"
] | 1589707200 | ["7\n123\n12222133333332\n112233\n332211\n12121212\n333333\n31121"] | NoteConsider the example test:In the first test case, the substring 123 can be used.In the second test case, the substring 213 can be used.In the third test case, the substring 1223 can be used.In the fourth test case, the substring 3221 can be used.In the fifth test case, there is no character 3 in $$$s$$$.In the sixth test case, there is no character 1 in $$$s$$$.In the seventh test case, the substring 3112 can be used. | PASSED | 1,200 | standard input | 2 seconds | The first line contains one integer $$$t$$$ ($$$1 \le t \le 20000$$$) β the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 200000$$$). It is guaranteed that each character of $$$s$$$ is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed $$$200000$$$. | ["3\n3\n4\n4\n0\n0\n4"] | char s[7<<15];m=3e5;main(i,j,r,l){
for(scanf("%*d");~scanf("%s",s);){int a[3]={0};for(i=r=m;s[i-m];){
a[s[i-m]%3]=i++;l=m*2;for(j=3;~--j;)l=a[j]<l?a[j]:l;r=i-l<r?i-l:r;}
printf("%d ",r%m);}} | |
You are given a string $$$s$$$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $$$s$$$ such that it contains each of these three characters at least once.A contiguous substring of string $$$s$$$ is a string that can be obtained from $$$s$$$ by removing some (possibly zero) characters from the beginning of $$$s$$$ and some (possibly zero) characters from the end of $$$s$$$. | For each test case, print one integer β the length of the shortest contiguous substring of $$$s$$$ containing all three types of characters at least once. If there is no such substring, print $$$0$$$ instead. | C | 6cb06a02077750327602a1a7eeac770f | 836be67c24c08fd53ff9f3bb08972f2d | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"dp",
"two pointers",
"implementation",
"binary search"
] | 1589707200 | ["7\n123\n12222133333332\n112233\n332211\n12121212\n333333\n31121"] | NoteConsider the example test:In the first test case, the substring 123 can be used.In the second test case, the substring 213 can be used.In the third test case, the substring 1223 can be used.In the fourth test case, the substring 3221 can be used.In the fifth test case, there is no character 3 in $$$s$$$.In the sixth test case, there is no character 1 in $$$s$$$.In the seventh test case, the substring 3112 can be used. | PASSED | 1,200 | standard input | 2 seconds | The first line contains one integer $$$t$$$ ($$$1 \le t \le 20000$$$) β the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 200000$$$). It is guaranteed that each character of $$$s$$$ is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed $$$200000$$$. | ["3\n3\n4\n4\n0\n0\n4"] | char s[7<<15];m=3e5;main(i,j,r,l){
for(scanf("%*d");~scanf("%s",s);){int a[3]={0};for(i=r=m;s[i-m];){
a[s[i-m]-49]=i++;l=m*2;for(j=3;~--j;)l=a[j]<l?a[j]:l;r=i-l<r?i-l:r;}
printf("%d ",r%m);}} | |
You are given a string $$$s$$$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $$$s$$$ such that it contains each of these three characters at least once.A contiguous substring of string $$$s$$$ is a string that can be obtained from $$$s$$$ by removing some (possibly zero) characters from the beginning of $$$s$$$ and some (possibly zero) characters from the end of $$$s$$$. | For each test case, print one integer β the length of the shortest contiguous substring of $$$s$$$ containing all three types of characters at least once. If there is no such substring, print $$$0$$$ instead. | C | 6cb06a02077750327602a1a7eeac770f | f3430335db006544c92a33bc1549215e | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"dp",
"two pointers",
"implementation",
"binary search"
] | 1589707200 | ["7\n123\n12222133333332\n112233\n332211\n12121212\n333333\n31121"] | NoteConsider the example test:In the first test case, the substring 123 can be used.In the second test case, the substring 213 can be used.In the third test case, the substring 1223 can be used.In the fourth test case, the substring 3221 can be used.In the fifth test case, there is no character 3 in $$$s$$$.In the sixth test case, there is no character 1 in $$$s$$$.In the seventh test case, the substring 3112 can be used. | PASSED | 1,200 | standard input | 2 seconds | The first line contains one integer $$$t$$$ ($$$1 \le t \le 20000$$$) β the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 200000$$$). It is guaranteed that each character of $$$s$$$ is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed $$$200000$$$. | ["3\n3\n4\n4\n0\n0\n4"] | char s[7<<15];m=3e5;main(i,x,y,r){
for(scanf("%*d");~scanf("%s",s);){int a[3]={0};for(i=r=m;x=s[i-m];){
a[x%3]=i++;y=a[(x+1)%3];x=a[(x+2)%3];x=i-(x<y?x:y);r=x<r?x:r;}
printf("%d ",r%m);}} | |
You are given a string $$$s$$$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $$$s$$$ such that it contains each of these three characters at least once.A contiguous substring of string $$$s$$$ is a string that can be obtained from $$$s$$$ by removing some (possibly zero) characters from the beginning of $$$s$$$ and some (possibly zero) characters from the end of $$$s$$$. | For each test case, print one integer β the length of the shortest contiguous substring of $$$s$$$ containing all three types of characters at least once. If there is no such substring, print $$$0$$$ instead. | C | 6cb06a02077750327602a1a7eeac770f | ff47f8024cffba2c79768516c7322d58 | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"dp",
"two pointers",
"implementation",
"binary search"
] | 1589707200 | ["7\n123\n12222133333332\n112233\n332211\n12121212\n333333\n31121"] | NoteConsider the example test:In the first test case, the substring 123 can be used.In the second test case, the substring 213 can be used.In the third test case, the substring 1223 can be used.In the fourth test case, the substring 3221 can be used.In the fifth test case, there is no character 3 in $$$s$$$.In the sixth test case, there is no character 1 in $$$s$$$.In the seventh test case, the substring 3112 can be used. | PASSED | 1,200 | standard input | 2 seconds | The first line contains one integer $$$t$$$ ($$$1 \le t \le 20000$$$) β the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 200000$$$). It is guaranteed that each character of $$$s$$$ is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed $$$200000$$$. | ["3\n3\n4\n4\n0\n0\n4"] | char s[7<<15];m=3e5;main(i,j,r,l){
for(scanf("%*d");~scanf("%s",s);){
int a[3]={0};for(i=r=m;s[i-m];)
{a[s[i-m]-49]=i++;for(l=m*2,j=3;~--j;)l=a[j]<l?a[j]:l;r=(i-l<r?i-l:r);}
printf("%d ",r%m);}} | |
You are given a string $$$s$$$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $$$s$$$ such that it contains each of these three characters at least once.A contiguous substring of string $$$s$$$ is a string that can be obtained from $$$s$$$ by removing some (possibly zero) characters from the beginning of $$$s$$$ and some (possibly zero) characters from the end of $$$s$$$. | For each test case, print one integer β the length of the shortest contiguous substring of $$$s$$$ containing all three types of characters at least once. If there is no such substring, print $$$0$$$ instead. | C | 6cb06a02077750327602a1a7eeac770f | 85a1b43951655f946c459870a93ee2c2 | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"dp",
"two pointers",
"implementation",
"binary search"
] | 1589707200 | ["7\n123\n12222133333332\n112233\n332211\n12121212\n333333\n31121"] | NoteConsider the example test:In the first test case, the substring 123 can be used.In the second test case, the substring 213 can be used.In the third test case, the substring 1223 can be used.In the fourth test case, the substring 3221 can be used.In the fifth test case, there is no character 3 in $$$s$$$.In the sixth test case, there is no character 1 in $$$s$$$.In the seventh test case, the substring 3112 can be used. | PASSED | 1,200 | standard input | 2 seconds | The first line contains one integer $$$t$$$ ($$$1 \le t \le 20000$$$) β the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 200000$$$). It is guaranteed that each character of $$$s$$$ is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed $$$200000$$$. | ["3\n3\n4\n4\n0\n0\n4"] | char s[7<<15];a[3];main(i,j,r,l,m){
for(scanf("%*d");~scanf("%s",s);memset(a,0,12)){
for(i=r=m=7<<15;s[i-m];)
{a[s[i-m]-49]=i++;for(l=m*2,j=3;~--j;)l=a[j]<l?a[j]:l;r=(i-l<r?i-l:r);}
printf("%d ",r%m);}} | |
You are given a string $$$s$$$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $$$s$$$ such that it contains each of these three characters at least once.A contiguous substring of string $$$s$$$ is a string that can be obtained from $$$s$$$ by removing some (possibly zero) characters from the beginning of $$$s$$$ and some (possibly zero) characters from the end of $$$s$$$. | For each test case, print one integer β the length of the shortest contiguous substring of $$$s$$$ containing all three types of characters at least once. If there is no such substring, print $$$0$$$ instead. | C | 6cb06a02077750327602a1a7eeac770f | 94940fd608d606456ea2e90cad1c1a7e | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"dp",
"two pointers",
"implementation",
"binary search"
] | 1589707200 | ["7\n123\n12222133333332\n112233\n332211\n12121212\n333333\n31121"] | NoteConsider the example test:In the first test case, the substring 123 can be used.In the second test case, the substring 213 can be used.In the third test case, the substring 1223 can be used.In the fourth test case, the substring 3221 can be used.In the fifth test case, there is no character 3 in $$$s$$$.In the sixth test case, there is no character 1 in $$$s$$$.In the seventh test case, the substring 3112 can be used. | PASSED | 1,200 | standard input | 2 seconds | The first line contains one integer $$$t$$$ ($$$1 \le t \le 20000$$$) β the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 200000$$$). It is guaranteed that each character of $$$s$$$ is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed $$$200000$$$. | ["3\n3\n4\n4\n0\n0\n4"] | char s[7<<15];m=7<<15;main(i,j,r,l){
for(scanf("%*d");~scanf("%s",s);){
int a[3]={0};for(i=r=m;s[i-m];)
{a[s[i-m]-49]=i++;for(l=m*2,j=3;~--j;)l=a[j]<l?a[j]:l;r=(i-l<r?i-l:r);}
printf("%d ",r%m);}} | |
You are given a string $$$s$$$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $$$s$$$ such that it contains each of these three characters at least once.A contiguous substring of string $$$s$$$ is a string that can be obtained from $$$s$$$ by removing some (possibly zero) characters from the beginning of $$$s$$$ and some (possibly zero) characters from the end of $$$s$$$. | For each test case, print one integer β the length of the shortest contiguous substring of $$$s$$$ containing all three types of characters at least once. If there is no such substring, print $$$0$$$ instead. | C | 6cb06a02077750327602a1a7eeac770f | 1ea819970ac5701997fcc84f768cc74d | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"dp",
"two pointers",
"implementation",
"binary search"
] | 1589707200 | ["7\n123\n12222133333332\n112233\n332211\n12121212\n333333\n31121"] | NoteConsider the example test:In the first test case, the substring 123 can be used.In the second test case, the substring 213 can be used.In the third test case, the substring 1223 can be used.In the fourth test case, the substring 3221 can be used.In the fifth test case, there is no character 3 in $$$s$$$.In the sixth test case, there is no character 1 in $$$s$$$.In the seventh test case, the substring 3112 can be used. | PASSED | 1,200 | standard input | 2 seconds | The first line contains one integer $$$t$$$ ($$$1 \le t \le 20000$$$) β the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 200000$$$). It is guaranteed that each character of $$$s$$$ is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed $$$200000$$$. | ["3\n3\n4\n4\n0\n0\n4"] | char s[7<<15];m=3e5;main(i,j,r,l){
for(scanf("%*d");~scanf("%s",s);){int a[3]={0};for(i=r=m;s[i-m];){
a[s[i-m]%3]=i++;l=m*2;for(j=3;~--j;)l=a[j]<l?a[j]:l;r=i-l<r?i-l:r;}
printf("%d ",r%m);}} | |
You are given a string $$$s$$$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $$$s$$$ such that it contains each of these three characters at least once.A contiguous substring of string $$$s$$$ is a string that can be obtained from $$$s$$$ by removing some (possibly zero) characters from the beginning of $$$s$$$ and some (possibly zero) characters from the end of $$$s$$$. | For each test case, print one integer β the length of the shortest contiguous substring of $$$s$$$ containing all three types of characters at least once. If there is no such substring, print $$$0$$$ instead. | C | 6cb06a02077750327602a1a7eeac770f | feab42d306155ce6d22e01a66e1583d1 | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"dp",
"two pointers",
"implementation",
"binary search"
] | 1589707200 | ["7\n123\n12222133333332\n112233\n332211\n12121212\n333333\n31121"] | NoteConsider the example test:In the first test case, the substring 123 can be used.In the second test case, the substring 213 can be used.In the third test case, the substring 1223 can be used.In the fourth test case, the substring 3221 can be used.In the fifth test case, there is no character 3 in $$$s$$$.In the sixth test case, there is no character 1 in $$$s$$$.In the seventh test case, the substring 3112 can be used. | PASSED | 1,200 | standard input | 2 seconds | The first line contains one integer $$$t$$$ ($$$1 \le t \le 20000$$$) β the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 200000$$$). It is guaranteed that each character of $$$s$$$ is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed $$$200000$$$. | ["3\n3\n4\n4\n0\n0\n4"] | #include<stdio.h>
int main()
{
int t, i, x, a, b;
char s[200001];
scanf("%d", &t);
while(t--)
{
scanf("%s", s);
int f=0, c=0, y=0;
for(i=0; s[i]!='\0'; i++)
{
if(i==0)
{
a=s[i];
}
else if(i==1)
{
b=s[i];
}
else
{
x=s[i];
if(a==b)
{
if(x!=a){
b=x;
}
}
else if(x==a)
{
a=b;
b=x;
c=0;
}
else if(x==b)
{
c++;
}
else
{
c++;
if(y==0){
f=c;
y++;
}
else if(c<=f){
f=c;
}
a=b;
b=x;
c=0;
}
}
}
if(f==0 || i<3)
{
printf("0\n");
}
else
{
printf("%d\n", f+2);
}
}
return 0;
} | |
You are given a string $$$s$$$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $$$s$$$ such that it contains each of these three characters at least once.A contiguous substring of string $$$s$$$ is a string that can be obtained from $$$s$$$ by removing some (possibly zero) characters from the beginning of $$$s$$$ and some (possibly zero) characters from the end of $$$s$$$. | For each test case, print one integer β the length of the shortest contiguous substring of $$$s$$$ containing all three types of characters at least once. If there is no such substring, print $$$0$$$ instead. | C | 6cb06a02077750327602a1a7eeac770f | 4c5b27b1e0c749f3fbf1b2cded843a9b | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"dp",
"two pointers",
"implementation",
"binary search"
] | 1589707200 | ["7\n123\n12222133333332\n112233\n332211\n12121212\n333333\n31121"] | NoteConsider the example test:In the first test case, the substring 123 can be used.In the second test case, the substring 213 can be used.In the third test case, the substring 1223 can be used.In the fourth test case, the substring 3221 can be used.In the fifth test case, there is no character 3 in $$$s$$$.In the sixth test case, there is no character 1 in $$$s$$$.In the seventh test case, the substring 3112 can be used. | PASSED | 1,200 | standard input | 2 seconds | The first line contains one integer $$$t$$$ ($$$1 \le t \le 20000$$$) β the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 200000$$$). It is guaranteed that each character of $$$s$$$ is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed $$$200000$$$. | ["3\n3\n4\n4\n0\n0\n4"] | #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define sum(x) (x[0]>0) +(x[1]>0) +(x[2]>0)
void solve()
{
char s[200000];
int hash[3]={0,0,0},n,left=0,right=0,min=200004;
scanf("%s",s);
n=strlen(s);
while(1){
if(sum(hash)<3){
hash[s[right]-'1']++;
right++;
}
else{
hash[s[left]-'1']--;
left++;
}
if(sum(hash)==3 && (right-left)<min){
min=right-left;
}
if(right==n && sum(hash)<3){
break;
}
}
if(min==200004){
printf("0\n");
}
else
printf("%d\n",min);
}
int main(){
int t;
scanf("%d",&t);
while(t--){
solve();
}
return 0;
} | |
You are given a string $$$s$$$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $$$s$$$ such that it contains each of these three characters at least once.A contiguous substring of string $$$s$$$ is a string that can be obtained from $$$s$$$ by removing some (possibly zero) characters from the beginning of $$$s$$$ and some (possibly zero) characters from the end of $$$s$$$. | For each test case, print one integer β the length of the shortest contiguous substring of $$$s$$$ containing all three types of characters at least once. If there is no such substring, print $$$0$$$ instead. | C | 6cb06a02077750327602a1a7eeac770f | 02a84dce1c5144e57d7eefffeb8ccd8a | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"dp",
"two pointers",
"implementation",
"binary search"
] | 1589707200 | ["7\n123\n12222133333332\n112233\n332211\n12121212\n333333\n31121"] | NoteConsider the example test:In the first test case, the substring 123 can be used.In the second test case, the substring 213 can be used.In the third test case, the substring 1223 can be used.In the fourth test case, the substring 3221 can be used.In the fifth test case, there is no character 3 in $$$s$$$.In the sixth test case, there is no character 1 in $$$s$$$.In the seventh test case, the substring 3112 can be used. | PASSED | 1,200 | standard input | 2 seconds | The first line contains one integer $$$t$$$ ($$$1 \le t \le 20000$$$) β the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 200000$$$). It is guaranteed that each character of $$$s$$$ is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed $$$200000$$$. | ["3\n3\n4\n4\n0\n0\n4"] | #include<stdio.h>
#include<stdlib.h>
#include<string.h>
int min(int a,int b)
{
return a>b?b:a;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
char s[200002];
scanf("%s",s);
int l=strlen(s);
int ans=2147483647,cnt[3]={0,0,0};
for(int i=0,j=0;i<l;i++)
{
while((!cnt[0]||!cnt[1]||!cnt[2])&&j<l)
cnt[s[j++]-'1']++;
if(cnt[0]&&cnt[1]&&cnt[2])
ans=min(ans,j-i);
cnt[s[i]-'1']--;
}
if(ans==2147483647)
printf("0\n");
else
printf("%d\n",ans);
}
}
| |
There are $$$n$$$ kids, numbered from $$$1$$$ to $$$n$$$, dancing in a circle around the Christmas tree. Let's enumerate them in a clockwise direction as $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ (all these numbers are from $$$1$$$ to $$$n$$$ and are distinct, so $$$p$$$ is a permutation). Let the next kid for a kid $$$p_i$$$ be kid $$$p_{i + 1}$$$ if $$$i < n$$$ and $$$p_1$$$ otherwise. After the dance, each kid remembered two kids: the next kid (let's call him $$$x$$$) and the next kid for $$$x$$$. Each kid told you which kids he/she remembered: the kid $$$i$$$ remembered kids $$$a_{i, 1}$$$ and $$$a_{i, 2}$$$. However, the order of $$$a_{i, 1}$$$ and $$$a_{i, 2}$$$ can differ from their order in the circle. Example: 5 kids in a circle, $$$p=[3, 2, 4, 1, 5]$$$ (or any cyclic shift). The information kids remembered is: $$$a_{1,1}=3$$$, $$$a_{1,2}=5$$$; $$$a_{2,1}=1$$$, $$$a_{2,2}=4$$$; $$$a_{3,1}=2$$$, $$$a_{3,2}=4$$$; $$$a_{4,1}=1$$$, $$$a_{4,2}=5$$$; $$$a_{5,1}=2$$$, $$$a_{5,2}=3$$$. You have to restore the order of the kids in the circle using this information. If there are several answers, you may print any. It is guaranteed that at least one solution exists.If you are Python programmer, consider using PyPy instead of Python when you submit your code. | Print $$$n$$$ integers $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ β permutation of integers from $$$1$$$ to $$$n$$$, which corresponds to the order of kids in the circle. If there are several answers, you may print any (for example, it doesn't matter which kid is the first in the circle). It is guaranteed that at least one solution exists. | C | 819d3694fccf2b5af0ec3b4ee429dbb3 | 5e23fcf6621ff9b82bdd7c104b0521a7 | GNU C11 | standard output | 256 megabytes | train_000.jsonl | [
"implementation"
] | 1545921300 | ["5\n3 5\n1 4\n2 4\n1 5\n2 3", "3\n2 3\n3 1\n1 2"] | null | PASSED | 1,600 | standard input | 3 seconds | The first line of the input contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of the kids. The next $$$n$$$ lines contain $$$2$$$ integers each. The $$$i$$$-th line contains two integers $$$a_{i, 1}$$$ and $$$a_{i, 2}$$$ ($$$1 \le a_{i, 1}, a_{i, 2} \le n, a_{i, 1} \ne a_{i, 2}$$$) β the kids the $$$i$$$-th kid remembered, given in arbitrary order. | ["3 2 4 1 5", "3 1 2"] | #include <stdio.h>
#include <stdlib.h>
int n;
int m[200001][2];
int nextSol(int i){
int a, b;
a = m[i][0];
b = m[i][1];
if(m[a][0] == b || m[a][1] == b){
return a;
}
return b;
}
int main(int argc, char *argv[]){
int i, j, sol;
scanf("%d", &n);
for(i = 1; i<=n; i++){
scanf("%d %d", &m[i][0], &m[i][1]);
}
if( n == 3 ){
printf("1 2 3\n");
return 0;
}
sol = 1;
for(i = 0; i<n; i++){
printf("%d ", sol);
sol = nextSol(sol);
}
printf("\n");
return 0;
}
| |
Pashmak decided to give Parmida a pair of flowers from the garden. There are n flowers in the garden and the i-th of them has a beauty number bi. Parmida is a very strange girl so she doesn't want to have the two most beautiful flowers necessarily. She wants to have those pairs of flowers that their beauty difference is maximal possible!Your task is to write a program which calculates two things: The maximum beauty difference of flowers that Pashmak can give to Parmida. The number of ways that Pashmak can pick the flowers. Two ways are considered different if and only if there is at least one flower that is chosen in the first way and not chosen in the second way. | The only line of output should contain two integers. The maximum beauty difference and the number of ways this may happen, respectively. | C | eb2d1072c5308d9ef686315a122d9d3c | 659808f55cb98e9f3506186528ea6d5e | GNU C | standard output | 256 megabytes | train_000.jsonl | [
"combinatorics",
"implementation",
"sortings"
] | 1408116600 | ["2\n1 2", "3\n1 4 5", "5\n3 1 2 3 1"] | NoteIn the third sample the maximum beauty difference is 2 and there are 4 ways to do this: choosing the first and the second flowers; choosing the first and the fifth flowers; choosing the fourth and the second flowers; choosing the fourth and the fifth flowers. | PASSED | 1,300 | standard input | 1 second | The first line of the input contains n (2ββ€βnββ€β2Β·105). In the next line there are n space-separated integers b1, b2, ..., bn (1ββ€βbiββ€β109). | ["1 1", "4 1", "2 4"] | #include <stdio.h>
#include <stdlib.h>
int main(void)
{
long long int n,*a,max=0,min,mxpos=0,mnpos=0,t,i;
scanf("%lld",&n);
a=(long long int *)malloc(sizeof(long long int) * n);
for(i=0; i<n; i++)
{
scanf("%lld",&a[i]);
if(i==0)
{
min=a[i];
}
if(max<=a[i])
{
max=a[i];
}
if(min>=a[i])
{
min=a[i];
}
}
for(i=0; i<n; i++)
{
if(max==a[i])
{
mxpos++;
}
if(min==a[i])
{
mnpos++;
}
}
if(max==min)
{
printf("%lld %lld\n",max-min,(n*(n-1))/2);
}
else
{
printf("%lld %lld\n",max-min,mxpos*mnpos);
}
return 0;
} | |
Pashmak decided to give Parmida a pair of flowers from the garden. There are n flowers in the garden and the i-th of them has a beauty number bi. Parmida is a very strange girl so she doesn't want to have the two most beautiful flowers necessarily. She wants to have those pairs of flowers that their beauty difference is maximal possible!Your task is to write a program which calculates two things: The maximum beauty difference of flowers that Pashmak can give to Parmida. The number of ways that Pashmak can pick the flowers. Two ways are considered different if and only if there is at least one flower that is chosen in the first way and not chosen in the second way. | The only line of output should contain two integers. The maximum beauty difference and the number of ways this may happen, respectively. | C | eb2d1072c5308d9ef686315a122d9d3c | 98c081fc76eb0e0768e14eb31f78df46 | GNU C | standard output | 256 megabytes | train_000.jsonl | [
"combinatorics",
"implementation",
"sortings"
] | 1408116600 | ["2\n1 2", "3\n1 4 5", "5\n3 1 2 3 1"] | NoteIn the third sample the maximum beauty difference is 2 and there are 4 ways to do this: choosing the first and the second flowers; choosing the first and the fifth flowers; choosing the fourth and the second flowers; choosing the fourth and the fifth flowers. | PASSED | 1,300 | standard input | 1 second | The first line of the input contains n (2ββ€βnββ€β2Β·105). In the next line there are n space-separated integers b1, b2, ..., bn (1ββ€βbiββ€β109). | ["1 1", "4 1", "2 4"] | #include <stdio.h>
int main()
{
long long a[200001]={0},min=1000000000,max=1,n,i,x,mincount=0,maxcount=0;
scanf("%I64d",&n);
for (i=0;i<n;i++){
scanf("%I64d",&x);
if (x<min){
mincount=0;
min=x;
}
if (x==min)
mincount++;
if (x>max){
maxcount=0;
max=x;
}
if (x==max)
maxcount++;
}
if (min!=max)
printf("%I64d %I64d\n",max-min,maxcount*mincount);
else
printf("%I64d %I64d\n",max-min,(maxcount*(maxcount-1))/2);
return 0;
}
| |
Pashmak decided to give Parmida a pair of flowers from the garden. There are n flowers in the garden and the i-th of them has a beauty number bi. Parmida is a very strange girl so she doesn't want to have the two most beautiful flowers necessarily. She wants to have those pairs of flowers that their beauty difference is maximal possible!Your task is to write a program which calculates two things: The maximum beauty difference of flowers that Pashmak can give to Parmida. The number of ways that Pashmak can pick the flowers. Two ways are considered different if and only if there is at least one flower that is chosen in the first way and not chosen in the second way. | The only line of output should contain two integers. The maximum beauty difference and the number of ways this may happen, respectively. | C | eb2d1072c5308d9ef686315a122d9d3c | ff72ef507cf02509ef135d6160f90504 | GNU C | standard output | 256 megabytes | train_000.jsonl | [
"combinatorics",
"implementation",
"sortings"
] | 1408116600 | ["2\n1 2", "3\n1 4 5", "5\n3 1 2 3 1"] | NoteIn the third sample the maximum beauty difference is 2 and there are 4 ways to do this: choosing the first and the second flowers; choosing the first and the fifth flowers; choosing the fourth and the second flowers; choosing the fourth and the fifth flowers. | PASSED | 1,300 | standard input | 1 second | The first line of the input contains n (2ββ€βnββ€β2Β·105). In the next line there are n space-separated integers b1, b2, ..., bn (1ββ€βbiββ€β109). | ["1 1", "4 1", "2 4"] | #include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main()
{
long long int n,i,minCount=0,maxCount=0,min=INT_MAX,max=INT_MIN,num;
scanf("%lld",&n);
int flag = 1;
for(i=0;i<n;i++)
{
scanf("%lld",&num);
if(flag)
{
min=num;
max = num;
minCount = 1;
maxCount=1;
flag = 0;
}
else
{
if(min > num)
{
min = num;
minCount = 1;
}
else if(min == num)
{
minCount++;
}
if(max < num)
{
max = num;
maxCount = 1;
}
else if(max == num)
{
maxCount++;
}
}
}
if(max != min)
{
printf("%lld %lld\n",(max-min),(minCount*maxCount));
}
else
{
printf("0 %lld\n",(n*(n-1))/2);;
}
return 0;
}
| |
Pashmak decided to give Parmida a pair of flowers from the garden. There are n flowers in the garden and the i-th of them has a beauty number bi. Parmida is a very strange girl so she doesn't want to have the two most beautiful flowers necessarily. She wants to have those pairs of flowers that their beauty difference is maximal possible!Your task is to write a program which calculates two things: The maximum beauty difference of flowers that Pashmak can give to Parmida. The number of ways that Pashmak can pick the flowers. Two ways are considered different if and only if there is at least one flower that is chosen in the first way and not chosen in the second way. | The only line of output should contain two integers. The maximum beauty difference and the number of ways this may happen, respectively. | C | eb2d1072c5308d9ef686315a122d9d3c | 8084dc2550d45c9957d41bf984dfd7de | GNU C | standard output | 256 megabytes | train_000.jsonl | [
"combinatorics",
"implementation",
"sortings"
] | 1408116600 | ["2\n1 2", "3\n1 4 5", "5\n3 1 2 3 1"] | NoteIn the third sample the maximum beauty difference is 2 and there are 4 ways to do this: choosing the first and the second flowers; choosing the first and the fifth flowers; choosing the fourth and the second flowers; choosing the fourth and the fifth flowers. | PASSED | 1,300 | standard input | 1 second | The first line of the input contains n (2ββ€βnββ€β2Β·105). In the next line there are n space-separated integers b1, b2, ..., bn (1ββ€βbiββ€β109). | ["1 1", "4 1", "2 4"] | #include<stdio.h>
#include<string.h>
#include<math.h>
#include<limits.h>
int arr[200005];
int compare(int *a,int *b){
return *a-*b;
}
int main(){
int a,b,c;
scanf("%d",&a);
int i;
int min=INT_MAX;
int max=0;
for(i=0;i<a;i++){
scanf("%d",arr+i);
if(arr[i]>max)
max=arr[i];
if(arr[i]<min)
min=arr[i];
}
qsort(arr,a,sizeof(int),compare);
if(abs(max-min)==0){
printf("%d %lld",abs(max-min),(long long int)((1+(a-1))*((a-1)/2.0)));
return 0;
}
i=0;
while(arr[i]==min)
i++;
int cnt1=i;
i=a-1;
int cnt2=0;
while(arr[i]==max){
cnt2++;
i--;}
printf("%d %lld",abs(max-min),(long long int)cnt1*cnt2);
}
| |
Pashmak decided to give Parmida a pair of flowers from the garden. There are n flowers in the garden and the i-th of them has a beauty number bi. Parmida is a very strange girl so she doesn't want to have the two most beautiful flowers necessarily. She wants to have those pairs of flowers that their beauty difference is maximal possible!Your task is to write a program which calculates two things: The maximum beauty difference of flowers that Pashmak can give to Parmida. The number of ways that Pashmak can pick the flowers. Two ways are considered different if and only if there is at least one flower that is chosen in the first way and not chosen in the second way. | The only line of output should contain two integers. The maximum beauty difference and the number of ways this may happen, respectively. | C | eb2d1072c5308d9ef686315a122d9d3c | a31e081d607fa80f1002f0373b5e31b8 | GNU C | standard output | 256 megabytes | train_000.jsonl | [
"combinatorics",
"implementation",
"sortings"
] | 1408116600 | ["2\n1 2", "3\n1 4 5", "5\n3 1 2 3 1"] | NoteIn the third sample the maximum beauty difference is 2 and there are 4 ways to do this: choosing the first and the second flowers; choosing the first and the fifth flowers; choosing the fourth and the second flowers; choosing the fourth and the fifth flowers. | PASSED | 1,300 | standard input | 1 second | The first line of the input contains n (2ββ€βnββ€β2Β·105). In the next line there are n space-separated integers b1, b2, ..., bn (1ββ€βbiββ€β109). | ["1 1", "4 1", "2 4"] | #include<stdio.h>
#include<string.h>
#include<math.h>
#include<limits.h>
int arr[200005];
int compare(int *a,int *b){
return *a-*b;
}
long long int bound(int arr[],int size,int target,int check){
int f,m,l;
f=0;
l=size;
if(!check){
while(f<l){
m=(f+l)/2;
if(arr[m]<=target){
f=m+1;
}
else l=m;
}
return l;}
while(f<l){
m=(f+l)/2;
if(arr[m]>=target){
l=m;
}
else f=m+1;
}
return size-l;
}
int main(){
int a,b,c;
scanf("%d",&a);
int i;
int min=INT_MAX;
int max=0;
for(i=0;i<a;i++){
scanf("%d",arr+i);
if(arr[i]>max)
max=arr[i];
if(arr[i]<min)
min=arr[i];
}
qsort(arr,a,sizeof(int),compare);
if(abs(max-min)==0){
printf("%d %lld",abs(max-min),(long long int)(((long long int)1+((long long int)a-1))*(((long long int)a-1)/2.0)));
return 0;
}
printf("%d %lld",abs(max-min),bound(arr,a,max,1)*(long long int)bound(arr,a,min,0));
}
| |
Pashmak decided to give Parmida a pair of flowers from the garden. There are n flowers in the garden and the i-th of them has a beauty number bi. Parmida is a very strange girl so she doesn't want to have the two most beautiful flowers necessarily. She wants to have those pairs of flowers that their beauty difference is maximal possible!Your task is to write a program which calculates two things: The maximum beauty difference of flowers that Pashmak can give to Parmida. The number of ways that Pashmak can pick the flowers. Two ways are considered different if and only if there is at least one flower that is chosen in the first way and not chosen in the second way. | The only line of output should contain two integers. The maximum beauty difference and the number of ways this may happen, respectively. | C | eb2d1072c5308d9ef686315a122d9d3c | a3b554be22bacc65088fadd042d512d0 | GNU C | standard output | 256 megabytes | train_000.jsonl | [
"combinatorics",
"implementation",
"sortings"
] | 1408116600 | ["2\n1 2", "3\n1 4 5", "5\n3 1 2 3 1"] | NoteIn the third sample the maximum beauty difference is 2 and there are 4 ways to do this: choosing the first and the second flowers; choosing the first and the fifth flowers; choosing the fourth and the second flowers; choosing the fourth and the fifth flowers. | PASSED | 1,300 | standard input | 1 second | The first line of the input contains n (2ββ€βnββ€β2Β·105). In the next line there are n space-separated integers b1, b2, ..., bn (1ββ€βbiββ€β109). | ["1 1", "4 1", "2 4"] | #include<stdio.h>
int main()
{
long long int n,i,j,max=0,count=0,l,m,min=20000000000,count2=0;
scanf("%I64d",&n);
long long int a[n];
for(i=0;i<n;i++)
{
scanf("%I64d",&a[i]);
if(a[i]>max)
max=a[i];
if(a[i]<min)
min=a[i];
}
/* for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if((a[i]-a[j])>max)
max=a[i]-a[j];
}
}*/
for(l=0;l<n;l++)
{
if(a[l]==max)
count++;
if(a[l]==min)
count2++;
}
if(max==min)
printf("%I64d %I64d",max-min,(n*(n-1))/2);
else
printf("%I64d %I64d",max-min,count*count2);
} | |
Pashmak decided to give Parmida a pair of flowers from the garden. There are n flowers in the garden and the i-th of them has a beauty number bi. Parmida is a very strange girl so she doesn't want to have the two most beautiful flowers necessarily. She wants to have those pairs of flowers that their beauty difference is maximal possible!Your task is to write a program which calculates two things: The maximum beauty difference of flowers that Pashmak can give to Parmida. The number of ways that Pashmak can pick the flowers. Two ways are considered different if and only if there is at least one flower that is chosen in the first way and not chosen in the second way. | The only line of output should contain two integers. The maximum beauty difference and the number of ways this may happen, respectively. | C | eb2d1072c5308d9ef686315a122d9d3c | 54f146497bfc15c9ddba6930ff1a46f7 | GNU C | standard output | 256 megabytes | train_000.jsonl | [
"combinatorics",
"implementation",
"sortings"
] | 1408116600 | ["2\n1 2", "3\n1 4 5", "5\n3 1 2 3 1"] | NoteIn the third sample the maximum beauty difference is 2 and there are 4 ways to do this: choosing the first and the second flowers; choosing the first and the fifth flowers; choosing the fourth and the second flowers; choosing the fourth and the fifth flowers. | PASSED | 1,300 | standard input | 1 second | The first line of the input contains n (2ββ€βnββ€β2Β·105). In the next line there are n space-separated integers b1, b2, ..., bn (1ββ€βbiββ€β109). | ["1 1", "4 1", "2 4"] | #include<stdio.h>
//#include<string.h>
#include<math.h>
main()
{
//freopen("out.txt", "r", stdin);
int m, c = 1, i, max = 0, min = 99999999999;
long long cmax = 0, cmin = 0, n;
scanf("%I64d", &n);
int a[n];
for(i = 0; i<n; i++)
{
scanf("%d", &a[i]);
if(a[i]>=max)
max = a[i];
if(a[i]<=min)
min = a[i];
}
if(max == min)
{
printf("0 %I64d", n*(n-1)/2 );
return 0;
}
for(i = 0; i<n; i++)
{
if(a[i]==max)
cmax++;
if(a[i]==min)
cmin++;
}
printf("%d %I64d", max-min, cmax*cmin);
//printf("0 %I64d", n*(n-1)/2 );
}
| |
Pashmak decided to give Parmida a pair of flowers from the garden. There are n flowers in the garden and the i-th of them has a beauty number bi. Parmida is a very strange girl so she doesn't want to have the two most beautiful flowers necessarily. She wants to have those pairs of flowers that their beauty difference is maximal possible!Your task is to write a program which calculates two things: The maximum beauty difference of flowers that Pashmak can give to Parmida. The number of ways that Pashmak can pick the flowers. Two ways are considered different if and only if there is at least one flower that is chosen in the first way and not chosen in the second way. | The only line of output should contain two integers. The maximum beauty difference and the number of ways this may happen, respectively. | C | eb2d1072c5308d9ef686315a122d9d3c | 01a76473a49939c2ab5f28cc8f4957ca | GNU C | standard output | 256 megabytes | train_000.jsonl | [
"combinatorics",
"implementation",
"sortings"
] | 1408116600 | ["2\n1 2", "3\n1 4 5", "5\n3 1 2 3 1"] | NoteIn the third sample the maximum beauty difference is 2 and there are 4 ways to do this: choosing the first and the second flowers; choosing the first and the fifth flowers; choosing the fourth and the second flowers; choosing the fourth and the fifth flowers. | PASSED | 1,300 | standard input | 1 second | The first line of the input contains n (2ββ€βnββ€β2Β·105). In the next line there are n space-separated integers b1, b2, ..., bn (1ββ€βbiββ€β109). | ["1 1", "4 1", "2 4"] | #include<stdio.h>
int main()
{
long long int n,b,i,j,mn=10000000000,mx=0,cnt_min,cnt_max,dif,way;
scanf("%I64d",&n);
for(i=0;i<n;i++)
{
scanf("%I64d",&b);
if(b>mx)
{
mx=b;
cnt_max=1;
}
else if(b==mx) cnt_max++;
if(b<mn)
{
mn=b;
cnt_min=1;
}
else if(b==mn) cnt_min++;
}
dif=mx-mn;
way=cnt_max*cnt_min;
if(dif==0) way=(cnt_max-1)*cnt_max/2;
printf("%I64d %I64d",dif,way);
}
| |
Pashmak decided to give Parmida a pair of flowers from the garden. There are n flowers in the garden and the i-th of them has a beauty number bi. Parmida is a very strange girl so she doesn't want to have the two most beautiful flowers necessarily. She wants to have those pairs of flowers that their beauty difference is maximal possible!Your task is to write a program which calculates two things: The maximum beauty difference of flowers that Pashmak can give to Parmida. The number of ways that Pashmak can pick the flowers. Two ways are considered different if and only if there is at least one flower that is chosen in the first way and not chosen in the second way. | The only line of output should contain two integers. The maximum beauty difference and the number of ways this may happen, respectively. | C | eb2d1072c5308d9ef686315a122d9d3c | 485074671c70b5f9b21bed17fb098202 | GNU C | standard output | 256 megabytes | train_000.jsonl | [
"combinatorics",
"implementation",
"sortings"
] | 1408116600 | ["2\n1 2", "3\n1 4 5", "5\n3 1 2 3 1"] | NoteIn the third sample the maximum beauty difference is 2 and there are 4 ways to do this: choosing the first and the second flowers; choosing the first and the fifth flowers; choosing the fourth and the second flowers; choosing the fourth and the fifth flowers. | PASSED | 1,300 | standard input | 1 second | The first line of the input contains n (2ββ€βnββ€β2Β·105). In the next line there are n space-separated integers b1, b2, ..., bn (1ββ€βbiββ€β109). | ["1 1", "4 1", "2 4"] | #include <stdio.h>
#include <stdlib.h>
int main(){
long long int n;
long long int bn = 1LL;
long long int sn = 1LL;
scanf("%I64d", &n);
long long *b;
long long bbuff;
long long sbuff;
b = (long long int*)malloc(sizeof(long long int)*n*1.5);
int i;
for (i = 0; i < n; i++)
{
scanf("%I64d", b+i);
}
bbuff = *b;
sbuff = *b;
for (i = 1; i < n; i++)
{
if (*(b+i)>bbuff)
{
bbuff = *(b + i);
bn = 1;
}
else if (*(b+i) == bbuff)
bn++;
if (*(b+i)<sbuff)
{
sbuff = *(b+i);
sn = 1;
}
else if (*(b+i) == sbuff)
sn++;
}
long long k;
k = bn * sn;
if (bbuff - sbuff == 0){
k = 0;
for (i = 1; i < bn; i++)
k += i;
}
printf("%I64d %I64d", bbuff - sbuff, k);
return 0;
}
| |
Pashmak decided to give Parmida a pair of flowers from the garden. There are n flowers in the garden and the i-th of them has a beauty number bi. Parmida is a very strange girl so she doesn't want to have the two most beautiful flowers necessarily. She wants to have those pairs of flowers that their beauty difference is maximal possible!Your task is to write a program which calculates two things: The maximum beauty difference of flowers that Pashmak can give to Parmida. The number of ways that Pashmak can pick the flowers. Two ways are considered different if and only if there is at least one flower that is chosen in the first way and not chosen in the second way. | The only line of output should contain two integers. The maximum beauty difference and the number of ways this may happen, respectively. | C | eb2d1072c5308d9ef686315a122d9d3c | 83fd6a1ac134060f4eaab35e9c6f3b9e | GNU C | standard output | 256 megabytes | train_000.jsonl | [
"combinatorics",
"implementation",
"sortings"
] | 1408116600 | ["2\n1 2", "3\n1 4 5", "5\n3 1 2 3 1"] | NoteIn the third sample the maximum beauty difference is 2 and there are 4 ways to do this: choosing the first and the second flowers; choosing the first and the fifth flowers; choosing the fourth and the second flowers; choosing the fourth and the fifth flowers. | PASSED | 1,300 | standard input | 1 second | The first line of the input contains n (2ββ€βnββ€β2Β·105). In the next line there are n space-separated integers b1, b2, ..., bn (1ββ€βbiββ€β109). | ["1 1", "4 1", "2 4"] | #include <stdio.h>
#include <stdlib.h>
int all_same(int *list, int N){
int i, first;
first = list[0];
for(i=1; i<N; i++){
if(list[i] != first)
return 0;
}
return 1;
}
long long NCR(int N, int r){
int i;
long long temp = 1;
for(i=N; i>N-r; i--){
temp = temp * i;
}
for(i=1; i<=r; i++){
temp = temp / i;
}
return temp;
}
int main(void){
int i, N, *list, max=-1, max_count=0, min=1000000001, min_count=0;
scanf("%d", &N);
list = (int *)malloc(sizeof(int)*N);
for(i=0; i<N; i++){
scanf("%d", &list[i]);
}
for(i=0; i<N; i++){
if(max == list[i]){
max_count++;
}
if(min == list[i]){
min_count++;
}
if(max < list[i]){
max_count = 1;
max = list[i];
}
if(min > list[i]){
min_count = 1;
min = list[i];
}
}
if(all_same(list, N) == 1){
printf("0 %lld", NCR(N, 2));
}else{
printf("%d %lld", max-min, (long long)max_count*min_count);
}
return 0;
} | |
Pashmak decided to give Parmida a pair of flowers from the garden. There are n flowers in the garden and the i-th of them has a beauty number bi. Parmida is a very strange girl so she doesn't want to have the two most beautiful flowers necessarily. She wants to have those pairs of flowers that their beauty difference is maximal possible!Your task is to write a program which calculates two things: The maximum beauty difference of flowers that Pashmak can give to Parmida. The number of ways that Pashmak can pick the flowers. Two ways are considered different if and only if there is at least one flower that is chosen in the first way and not chosen in the second way. | The only line of output should contain two integers. The maximum beauty difference and the number of ways this may happen, respectively. | C | eb2d1072c5308d9ef686315a122d9d3c | 8ac6a4554482a52ab2113f0a9dd926f3 | GNU C | standard output | 256 megabytes | train_000.jsonl | [
"combinatorics",
"implementation",
"sortings"
] | 1408116600 | ["2\n1 2", "3\n1 4 5", "5\n3 1 2 3 1"] | NoteIn the third sample the maximum beauty difference is 2 and there are 4 ways to do this: choosing the first and the second flowers; choosing the first and the fifth flowers; choosing the fourth and the second flowers; choosing the fourth and the fifth flowers. | PASSED | 1,300 | standard input | 1 second | The first line of the input contains n (2ββ€βnββ€β2Β·105). In the next line there are n space-separated integers b1, b2, ..., bn (1ββ€βbiββ€β109). | ["1 1", "4 1", "2 4"] | #include<stdio.h>
int main()
{
int max,min,x,y;
int n,i;
int a,b;
while(scanf("%d",&n)==1)
{
scanf("%d",&a);
min=max=a;
x=y=1;
for(i=1;i<n;i++)
{
scanf("%d",&a);
if(a>max)
{
max=a;
x=0;
}
if(a<min)
{
min=a;
y=0;
}
if(max==a)
x++;
if(min==a)
y++;
}
if(max==min)
printf("%d %I64d\n",max-min,(long long)(x-1)*(x)/2);
else
printf("%d %I64d\n",max-min,(long long)x*y);
}
return 0;
}
| |
Pashmak decided to give Parmida a pair of flowers from the garden. There are n flowers in the garden and the i-th of them has a beauty number bi. Parmida is a very strange girl so she doesn't want to have the two most beautiful flowers necessarily. She wants to have those pairs of flowers that their beauty difference is maximal possible!Your task is to write a program which calculates two things: The maximum beauty difference of flowers that Pashmak can give to Parmida. The number of ways that Pashmak can pick the flowers. Two ways are considered different if and only if there is at least one flower that is chosen in the first way and not chosen in the second way. | The only line of output should contain two integers. The maximum beauty difference and the number of ways this may happen, respectively. | C | eb2d1072c5308d9ef686315a122d9d3c | 75083e94604daecb0b03eb44bd5ecfbc | GNU C | standard output | 256 megabytes | train_000.jsonl | [
"combinatorics",
"implementation",
"sortings"
] | 1408116600 | ["2\n1 2", "3\n1 4 5", "5\n3 1 2 3 1"] | NoteIn the third sample the maximum beauty difference is 2 and there are 4 ways to do this: choosing the first and the second flowers; choosing the first and the fifth flowers; choosing the fourth and the second flowers; choosing the fourth and the fifth flowers. | PASSED | 1,300 | standard input | 1 second | The first line of the input contains n (2ββ€βnββ€β2Β·105). In the next line there are n space-separated integers b1, b2, ..., bn (1ββ€βbiββ€β109). | ["1 1", "4 1", "2 4"] | #include<stdio.h>
int main()
{
long long a[200001],min, max,b,c,n,i;
scanf("%I64d",&n);
scanf("%I64d",&a[0]);
min=a[0];
max=a[0];
for(i=1;i<n;++i)
{
scanf("%I64d",&a[i]);
if(a[i]>max)
max=a[i];
if(a[i]<min)
min=a[i];
}
printf("%I64d ",max-min);
b=0;
c=0;
for(i=0;i<n;++i)
{
if(a[i]==min)
{
b++;
}
if(a[i]==max)
c++;
}
if(min==max)
printf("%I64d",n*(n-1)/2);
else
printf("%I64d",b*c);
return 0;
} | |
Pashmak decided to give Parmida a pair of flowers from the garden. There are n flowers in the garden and the i-th of them has a beauty number bi. Parmida is a very strange girl so she doesn't want to have the two most beautiful flowers necessarily. She wants to have those pairs of flowers that their beauty difference is maximal possible!Your task is to write a program which calculates two things: The maximum beauty difference of flowers that Pashmak can give to Parmida. The number of ways that Pashmak can pick the flowers. Two ways are considered different if and only if there is at least one flower that is chosen in the first way and not chosen in the second way. | The only line of output should contain two integers. The maximum beauty difference and the number of ways this may happen, respectively. | C | eb2d1072c5308d9ef686315a122d9d3c | 6ca3e834e0d6cf3cc223438283188551 | GNU C | standard output | 256 megabytes | train_000.jsonl | [
"combinatorics",
"implementation",
"sortings"
] | 1408116600 | ["2\n1 2", "3\n1 4 5", "5\n3 1 2 3 1"] | NoteIn the third sample the maximum beauty difference is 2 and there are 4 ways to do this: choosing the first and the second flowers; choosing the first and the fifth flowers; choosing the fourth and the second flowers; choosing the fourth and the fifth flowers. | PASSED | 1,300 | standard input | 1 second | The first line of the input contains n (2ββ€βnββ€β2Β·105). In the next line there are n space-separated integers b1, b2, ..., bn (1ββ€βbiββ€β109). | ["1 1", "4 1", "2 4"] | #include<stdio.h>
#include<stdlib.h>
int comparefunc(const void *a,const void *b)
{
return (*(int*)a-*(int*)b);
}
int main()
{
long long n,i,j,max,min,count1=0,count2=0;
scanf("%lld",&n);
int num[n];
for(i=0;i<n;i++)scanf("%lld",&num[i]);
qsort(num,n,sizeof(int),comparefunc);
min=num[0],max=num[n-1];
for(i=0,j=n-1;i<n || j>=0;i++,j--){
if(num[i]!=min && num[j]!=max)break;
if(num[i]==min)count1++;
if(num[j]==max)count2++;
}
if(max==min){
count2=0;
for(i=count1-1;i>0;i--)count2+=i;
printf("%lld %lld",max-min,count2);
}
else printf("%lld %lld",max-min,count1*count2);
return 0;
}
| |
Pashmak decided to give Parmida a pair of flowers from the garden. There are n flowers in the garden and the i-th of them has a beauty number bi. Parmida is a very strange girl so she doesn't want to have the two most beautiful flowers necessarily. She wants to have those pairs of flowers that their beauty difference is maximal possible!Your task is to write a program which calculates two things: The maximum beauty difference of flowers that Pashmak can give to Parmida. The number of ways that Pashmak can pick the flowers. Two ways are considered different if and only if there is at least one flower that is chosen in the first way and not chosen in the second way. | The only line of output should contain two integers. The maximum beauty difference and the number of ways this may happen, respectively. | C | eb2d1072c5308d9ef686315a122d9d3c | b88747963fe25bdbdb4b5c0fd65ae9c3 | GNU C | standard output | 256 megabytes | train_000.jsonl | [
"combinatorics",
"implementation",
"sortings"
] | 1408116600 | ["2\n1 2", "3\n1 4 5", "5\n3 1 2 3 1"] | NoteIn the third sample the maximum beauty difference is 2 and there are 4 ways to do this: choosing the first and the second flowers; choosing the first and the fifth flowers; choosing the fourth and the second flowers; choosing the fourth and the fifth flowers. | PASSED | 1,300 | standard input | 1 second | The first line of the input contains n (2ββ€βnββ€β2Β·105). In the next line there are n space-separated integers b1, b2, ..., bn (1ββ€βbiββ€β109). | ["1 1", "4 1", "2 4"] | #include<stdio.h>
int main(void)
{
long long int n,i,max=-1,min=2000000000,a[200005];
long long int countmax=0,countmin=0,dif,mul;
scanf("%I64d",&n);
for(i=0;i<n;i++) {
scanf("%I64d",&a[i]);
if(max<a[i])
max=a[i];
if(min>a[i])
min=a[i];
}
if(n==2 && a[0]==1 && a[1]==1) {
printf("0 1");
return 0;
}
if(min==max) {
printf("0 %I64d",n*(n-1)/2);
return 0;
}
dif=max-min;
for(i=0;i<n;i++) {
if(a[i]==max)
countmax++;
if(a[i]==min)
countmin++;
}
mul=countmax*countmin;
printf("%I64d %I64d",dif,mul);
return 0;
} | |
Pashmak decided to give Parmida a pair of flowers from the garden. There are n flowers in the garden and the i-th of them has a beauty number bi. Parmida is a very strange girl so she doesn't want to have the two most beautiful flowers necessarily. She wants to have those pairs of flowers that their beauty difference is maximal possible!Your task is to write a program which calculates two things: The maximum beauty difference of flowers that Pashmak can give to Parmida. The number of ways that Pashmak can pick the flowers. Two ways are considered different if and only if there is at least one flower that is chosen in the first way and not chosen in the second way. | The only line of output should contain two integers. The maximum beauty difference and the number of ways this may happen, respectively. | C | eb2d1072c5308d9ef686315a122d9d3c | 22057e35859e79d368cdfc43a2c11c0d | GNU C | standard output | 256 megabytes | train_000.jsonl | [
"combinatorics",
"implementation",
"sortings"
] | 1408116600 | ["2\n1 2", "3\n1 4 5", "5\n3 1 2 3 1"] | NoteIn the third sample the maximum beauty difference is 2 and there are 4 ways to do this: choosing the first and the second flowers; choosing the first and the fifth flowers; choosing the fourth and the second flowers; choosing the fourth and the fifth flowers. | PASSED | 1,300 | standard input | 1 second | The first line of the input contains n (2ββ€βnββ€β2Β·105). In the next line there are n space-separated integers b1, b2, ..., bn (1ββ€βbiββ€β109). | ["1 1", "4 1", "2 4"] | #include<stdio.h>
#include<stdlib.h>
#include<string.h>
int cmpfunc(const void *a_a, const void *b_b)
{
return (*(int*)a_a - *(int*)b_b); //For ascending order
}
//void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*))
unsigned long long factorial(int n)
{
int c;
unsigned long long result = 1;
for( c = 1 ; c <= n ; c++ )
result = result*c;
return ( result );
}
int main()
{
int n;
scanf("%d", &n);
int *b = malloc(sizeof(int)*n);
int i;
for(i=0; i<n; i++)
scanf("%d", b+i);
qsort(b, n, sizeof(int), cmpfunc);
unsigned long long int minCnt=0, maxCnt=0, min = b[0], max = b[n-1];
for(i=0; i<n; i++)
if(min == b[i])
minCnt++;
else
break;
for(i=n-1; i>-1; i--)
if(max == b[i])
maxCnt++;
else
break;
if(min != max)
printf("%llu %llu", max-min, minCnt*maxCnt);
else {
unsigned long long int n1 = n;
unsigned long long int nc2 = (n1*(n1-1))/2;
printf("%llu %llu", max-min, nc2);
//printf("%llu",nc2);
}
return 0;
} | |
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? | Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. | C | ddc9201725e30297a5fc83f4eed75fc9 | b08659a29b4dcdf6a28a73cc21ba75be | GNU C | standard output | 265 megabytes | train_000.jsonl | [
"implementation"
] | 1312390800 | ["0", "10", "991"] | NoteIn the first sample the number already is one-digit β Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991βββ19βββ10βββ1. After three transformations the number becomes one-digit. | PASSED | 1,000 | standard input | 2 seconds | The first line contains the only integer n (0ββ€βnββ€β10100000). It is guaranteed that n doesn't contain any leading zeroes. | ["0", "1", "3"] | #include"stdio.h"
int cnt=0,i;
char arr[100005];
void sum(){
int t=0;
for(i=0;arr[i];i++)
t+=arr[i]-48;
sprintf(arr,"%d",t);
}
int main(){
scanf("%s",arr);
while(1){
if(arr[1]==0)break;
sum(); cnt++;
}
printf("%d",cnt);
return 0;
}
| |
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? | Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. | C | ddc9201725e30297a5fc83f4eed75fc9 | 3c0128592bb2db24db022259b1c29a4a | GNU C | standard output | 265 megabytes | train_000.jsonl | [
"implementation"
] | 1312390800 | ["0", "10", "991"] | NoteIn the first sample the number already is one-digit β Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991βββ19βββ10βββ1. After three transformations the number becomes one-digit. | PASSED | 1,000 | standard input | 2 seconds | The first line contains the only integer n (0ββ€βnββ€β10100000). It is guaranteed that n doesn't contain any leading zeroes. | ["0", "1", "3"] | #include<stdio.h>
#include<string.h>
int sm;
int f(int n)
{
sm=0;int s;
while(n>0)
{
s=n%10;
// printf("%d---",s);
sm=sm+s;
n=n/10;
//printf("%d\n",n);
}
return sm;
// printf("%d",sm);
}
int main()
{
int n=0,cnt=0,z,i;
char c[100005];
scanf("%s",c);
for(i=0;i<strlen(c);i++)
{
n=n+(c[i]-'0');
}
if(strlen(c)==1)
{
printf("0");
return 0;
}
// while(t--)
// printf("..%d\n",n);
// w=n;
while(n>=10)
{
z=f(n);
cnt++;
// printf("%d\n",z);
n=z;
}
printf("%d",cnt+1);
return 0;
}
| |
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? | Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. | C | ddc9201725e30297a5fc83f4eed75fc9 | acf03f34ef3322a6217253ff9a4ffa14 | GNU C | standard output | 265 megabytes | train_000.jsonl | [
"implementation"
] | 1312390800 | ["0", "10", "991"] | NoteIn the first sample the number already is one-digit β Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991βββ19βββ10βββ1. After three transformations the number becomes one-digit. | PASSED | 1,000 | standard input | 2 seconds | The first line contains the only integer n (0ββ€βnββ€β10100000). It is guaranteed that n doesn't contain any leading zeroes. | ["0", "1", "3"] | //Bismillahir rahmanir rahim
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main(void) {
// your code goes here
char num[100001];
gets(num);
int i,c=0, y,sum=0;
y = strlen(num);
if(y<2){
goto asd;
}
qer:
for(i=0;num[i];++i)
sum += num[i]-'0';
//printf("%d\n",sum);
i=0;
if(sum<10){
++c;
goto asd;
}
while(sum){
num[i]=sum%10+'0';
sum/=10;
i++;
}
num[i]='\0';
++c;
goto qer;
asd:
printf("%d",c);
return 0;
}
| |
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? | Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. | C | ddc9201725e30297a5fc83f4eed75fc9 | d49b5ed278250131289687e75285c894 | GNU C | standard output | 265 megabytes | train_000.jsonl | [
"implementation"
] | 1312390800 | ["0", "10", "991"] | NoteIn the first sample the number already is one-digit β Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991βββ19βββ10βββ1. After three transformations the number becomes one-digit. | PASSED | 1,000 | standard input | 2 seconds | The first line contains the only integer n (0ββ€βnββ€β10100000). It is guaranteed that n doesn't contain any leading zeroes. | ["0", "1", "3"] | #include<stdio.h>
#include<string.h>
int main()
{
char arr[100002];
int sum=0,c=0,i,j,k,l;
scanf("%s",arr);
l=strlen(arr);
if(l!=1)
{
for(i=0;i<l;i++)
sum=sum+arr[i]-48;
c++;
}
while(sum>=10)
{
k=0;
while(sum!=0)
{
j=sum%10;
k+=j;
sum/=10;
}
sum=k;
c++;
}
printf("%d",c);
return(0);
}
| |
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? | Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. | C | ddc9201725e30297a5fc83f4eed75fc9 | 65b1b5daaa85265c67f83154236ac5dd | GNU C | standard output | 265 megabytes | train_000.jsonl | [
"implementation"
] | 1312390800 | ["0", "10", "991"] | NoteIn the first sample the number already is one-digit β Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991βββ19βββ10βββ1. After three transformations the number becomes one-digit. | PASSED | 1,000 | standard input | 2 seconds | The first line contains the only integer n (0ββ€βnββ€β10100000). It is guaranteed that n doesn't contain any leading zeroes. | ["0", "1", "3"] | # include <stdio.h>
main()
{
long long int t,ans=1,a[100001],j,n=0,i=0;
char c;
while((c=getchar())!='\n')
a[i++]=c-'0';
i--;
if(i==0){printf("0");return 0;}
for(j=0;j<=i;j++)n+=a[j];
while((n/10)!=0)
{
t=0;ans++;
while(n!=0)
{
t+=(n%10);
n/=10;
}
n=t;
}
printf("%I64d",ans);
return 0;
}
| |
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? | Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. | C | ddc9201725e30297a5fc83f4eed75fc9 | 9dce1ae52e5097b84ee4d46e4efe6094 | GNU C | standard output | 265 megabytes | train_000.jsonl | [
"implementation"
] | 1312390800 | ["0", "10", "991"] | NoteIn the first sample the number already is one-digit β Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991βββ19βββ10βββ1. After three transformations the number becomes one-digit. | PASSED | 1,000 | standard input | 2 seconds | The first line contains the only integer n (0ββ€βnββ€β10100000). It is guaranteed that n doesn't contain any leading zeroes. | ["0", "1", "3"] | #include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
int n,i,j,sum=0,count=0;char no[100002];
scanf("%s",&no);
while(no[1])
{
count++;
int sum=0;
for(j=0;no[j];j++)sum+=no[j]-'0';
sprintf(no,"%d",sum);
}
printf("%d",count);
return 0;
} | |
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? | Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. | C | ddc9201725e30297a5fc83f4eed75fc9 | 5ad8dacc821cb4af5ad672938dfd09f4 | GNU C | standard output | 265 megabytes | train_000.jsonl | [
"implementation"
] | 1312390800 | ["0", "10", "991"] | NoteIn the first sample the number already is one-digit β Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991βββ19βββ10βββ1. After three transformations the number becomes one-digit. | PASSED | 1,000 | standard input | 2 seconds | The first line contains the only integer n (0ββ€βnββ€β10100000). It is guaranteed that n doesn't contain any leading zeroes. | ["0", "1", "3"] | #include<stdio.h>
#include<string.h>
#define LENGTH 100001
int main(void){
int count[10];
char num[LENGTH];
int i,j,k,l,times=0,sum;
scanf("%s",num);
for(i=0;i<10;i++) count[i]=0;
l=strlen(num);
if(l==1){
puts("0");
return 0;
}
for(i=0;i<l;i++) count[num[i]-'0']++;
times=1;
while(1){
for(i=0,k=0;i<10;i++) k+=count[i];
if(k<=1) break;
for(i=0,sum=0;i<10;i++) sum+=i*count[i];
sprintf(num,"%d\0",sum);
l=strlen(num);
for(i=0;i<10;i++) count[i]=0;
for(i=0;i<l;i++) count[num[i]-'0']++;
times++;
}
printf("%d\n",times-1);
return 0;
}
| |
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? | Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. | C | ddc9201725e30297a5fc83f4eed75fc9 | 6e0c934e2dac38dcf7fa620327363a49 | GNU C | standard output | 265 megabytes | train_000.jsonl | [
"implementation"
] | 1312390800 | ["0", "10", "991"] | NoteIn the first sample the number already is one-digit β Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991βββ19βββ10βββ1. After three transformations the number becomes one-digit. | PASSED | 1,000 | standard input | 2 seconds | The first line contains the only integer n (0ββ€βnββ€β10100000). It is guaranteed that n doesn't contain any leading zeroes. | ["0", "1", "3"] | #include<stdio.h>
#include<string.h>
char a[100009];
int main()
{
int n,m=0,i,j,len,count=1,m1;
gets(a);
len=strlen(a);
if(len==1)
{
printf("0");
return 0;
}
for(i=0;i<len;i++)
m+=(a[i]-48);
m1=m;
//printf("%d ",m);
while(1)
{
if(m1/10==0)
break;
else
{
m=0;
count++;
while(m1)
{
m+=m1%10;
m1/=10;
}
m1=m;
}
}
printf("%d",count);
}
| |
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? | Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. | C | ddc9201725e30297a5fc83f4eed75fc9 | cff103aaabc1d76c263ea275bbbc7d44 | GNU C | standard output | 265 megabytes | train_000.jsonl | [
"implementation"
] | 1312390800 | ["0", "10", "991"] | NoteIn the first sample the number already is one-digit β Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991βββ19βββ10βββ1. After three transformations the number becomes one-digit. | PASSED | 1,000 | standard input | 2 seconds | The first line contains the only integer n (0ββ€βnββ€β10100000). It is guaranteed that n doesn't contain any leading zeroes. | ["0", "1", "3"] |
int main()
{
char digits[100001];
scanf("%s", digits);
int nbr=0;
if (digits[1] != 0)
{
int digitsum=0;
char* p = digits;
while (*p != 0)
{
digitsum += (*p - '0');
++p;
}
++nbr;
while (digitsum > 9)
{
int m=digitsum;
digitsum = 0;
while (m != 0)
{
digitsum += m % 10;
m /= 10;
}
++nbr;
}
}
printf("%d\n", nbr);
return 0;
} | |
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? | Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. | C | ddc9201725e30297a5fc83f4eed75fc9 | 2d721c400c1de405591ff9e7faa5b368 | GNU C | standard output | 265 megabytes | train_000.jsonl | [
"implementation"
] | 1312390800 | ["0", "10", "991"] | NoteIn the first sample the number already is one-digit β Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991βββ19βββ10βββ1. After three transformations the number becomes one-digit. | PASSED | 1,000 | standard input | 2 seconds | The first line contains the only integer n (0ββ€βnββ€β10100000). It is guaranteed that n doesn't contain any leading zeroes. | ["0", "1", "3"] | #include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main()
{
char ch[100002];
scanf("%s",ch);
int n=0,i,j=0;
for(i=0;ch[i];i++)
{
n+=ch[i]-'0';
}
int m=0;
if(!n||i==1)
{
printf("%d",0);
return 0;
}
j++;
l1:
if(n>9)
{
j++;
while(n)
{
m+=n%10;
n=n/10;
}
n^=m;
m^=n;
n^=m;
goto l1;
}
printf("%d",j);
return 0;
} | |
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? | Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. | C | ddc9201725e30297a5fc83f4eed75fc9 | fbe021122b97e414ab26bd1c3a38ba7a | GNU C | standard output | 265 megabytes | train_000.jsonl | [
"implementation"
] | 1312390800 | ["0", "10", "991"] | NoteIn the first sample the number already is one-digit β Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991βββ19βββ10βββ1. After three transformations the number becomes one-digit. | PASSED | 1,000 | standard input | 2 seconds | The first line contains the only integer n (0ββ€βnββ€β10100000). It is guaranteed that n doesn't contain any leading zeroes. | ["0", "1", "3"] | #include<stdio.h>
#include<string.h>
#define MAX 100000
int strsum(char *s)
{
int ans=0;
while(*s){
ans+=*s-'0';
s++;
}
return ans;
}
int main()
{
int ans,in,out;
char inchar[MAX+1];
scanf("%s",inchar);
if(inchar[1]=='\0')
printf("0\n");
else{
ans=1;
in=strsum(inchar);
while(in>9){
out=0;
while(in){
out+=in%10;
in/=10;
}
ans++;
in=out;
}
printf("%d\n",ans);
}
return 0;
}
| |
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? | Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. | C | ddc9201725e30297a5fc83f4eed75fc9 | 2c773c74ca5c9cc8ced8924f65957407 | GNU C | standard output | 265 megabytes | train_000.jsonl | [
"implementation"
] | 1312390800 | ["0", "10", "991"] | NoteIn the first sample the number already is one-digit β Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991βββ19βββ10βββ1. After three transformations the number becomes one-digit. | PASSED | 1,000 | standard input | 2 seconds | The first line contains the only integer n (0ββ€βnββ€β10100000). It is guaranteed that n doesn't contain any leading zeroes. | ["0", "1", "3"] | #include <stdio.h>
#include <string.h>
int main()
{
char s[100005];
int n = 1, sum = 0, i;
scanf("%s", s);
if (strlen(s) == 1) {
puts("0");
return 0;
}
for (i = 0; i < strlen(s); i++) sum += s[i] - '0';
while (sum / 10 > 0) {
int p = 0;
while (sum) {
p += sum % 10;
sum /= 10;
}
sum = p;
n++;
}
printf("%d\n", n);
return 0;
}
| |
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? | Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. | C | ddc9201725e30297a5fc83f4eed75fc9 | 4a068fceb9b6d005cffb05106e98105b | GNU C | standard output | 265 megabytes | train_000.jsonl | [
"implementation"
] | 1312390800 | ["0", "10", "991"] | NoteIn the first sample the number already is one-digit β Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991βββ19βββ10βββ1. After three transformations the number becomes one-digit. | PASSED | 1,000 | standard input | 2 seconds | The first line contains the only integer n (0ββ€βnββ€β10100000). It is guaranteed that n doesn't contain any leading zeroes. | ["0", "1", "3"] | #include <stdio.h>
int conv(char *inp) {
int n = 0;
while (*inp) {
n += *inp - '0';
inp++;
}
return n;
}
int nsum(int n) {
int sum = 0;
while (n) {
sum += (n % 10);
n /= 10;
}
return sum;
}
int main(void) {
int n = 0, cnt = 0;
char inp[100002] = {0, };
scanf("%s", inp);
if (inp[1] != 0) {
n = conv(inp);
cnt++;
while (n >= 10) {
n = nsum(n);
cnt++;
}
}
printf("%d\n", cnt);
return 0;
} | |
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? | Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. | C | ddc9201725e30297a5fc83f4eed75fc9 | 0fd197343d8bc2a65e582b29435b8aa0 | GNU C | standard output | 265 megabytes | train_000.jsonl | [
"implementation"
] | 1312390800 | ["0", "10", "991"] | NoteIn the first sample the number already is one-digit β Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991βββ19βββ10βββ1. After three transformations the number becomes one-digit. | PASSED | 1,000 | standard input | 2 seconds | The first line contains the only integer n (0ββ€βnββ€β10100000). It is guaranteed that n doesn't contain any leading zeroes. | ["0", "1", "3"] | #include <stdio.h>
#include <string.h>
char s[100009];
int main()
{
scanf("%s", s);
int n = 0, res = 0, i;
if (strlen(s) < 2) {
printf("0\n");
return 0;
}
while (1) {
res++;
int n = 0;
for (i = 0; i < strlen(s); i++) {
n += s[i] - '0';
}
if (n <= 9) break;
sprintf(s, "%d", n);
}
printf("%d\n", res);
return 0;
}
| |
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? | Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. | C | ddc9201725e30297a5fc83f4eed75fc9 | c555ca34cf5faa475690b2e6bbabc1e7 | GNU C | standard output | 265 megabytes | train_000.jsonl | [
"implementation"
] | 1312390800 | ["0", "10", "991"] | NoteIn the first sample the number already is one-digit β Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991βββ19βββ10βββ1. After three transformations the number becomes one-digit. | PASSED | 1,000 | standard input | 2 seconds | The first line contains the only integer n (0ββ€βnββ€β10100000). It is guaranteed that n doesn't contain any leading zeroes. | ["0", "1", "3"] | #include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(){
int n,i,j=0,a,sum=0;
char str[100000];
scanf("%s",str);
//while(sum>=10){
if(strlen(str)==1)
printf("0");
else{
for(j=1;j<=1000000000;j++){
sum=0;
for(i=0;i<=strlen(str)-1;i++){
a=str[i]-'0';
sum=sum+a;
}
if(sum<10)
break;
sprintf(str,"%d",sum);
//printf("%s ",str);
}
printf("%d",j);
}
return 0;
}
| |
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? | Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. | C | ddc9201725e30297a5fc83f4eed75fc9 | 707aa62af031a0e46b5971619344d344 | GNU C | standard output | 265 megabytes | train_000.jsonl | [
"implementation"
] | 1312390800 | ["0", "10", "991"] | NoteIn the first sample the number already is one-digit β Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991βββ19βββ10βββ1. After three transformations the number becomes one-digit. | PASSED | 1,000 | standard input | 2 seconds | The first line contains the only integer n (0ββ€βnββ€β10100000). It is guaranteed that n doesn't contain any leading zeroes. | ["0", "1", "3"] | #include<stdio.h>
#include<string.h>
main()
{
int i=0,j,k,total=0,c;
char a[100005];
scanf("%s",a);
for(i=0;i<strlen(a);i++)
{
total+=(a[i]-'0');
}
if(strlen(a)==1){j=-1;goto x;}
for(j=0;total/10!=0;j++)
{
c=0;
for(k=0;total!=0;k++)
{
c+=total%10;
total/=10;
}
total=c;
}
x:
printf("%d",j+1);
return 0;
}
| |
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? | Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. | C | ddc9201725e30297a5fc83f4eed75fc9 | afdf8193d269961c055e57efcdb14796 | GNU C | standard output | 265 megabytes | train_000.jsonl | [
"implementation"
] | 1312390800 | ["0", "10", "991"] | NoteIn the first sample the number already is one-digit β Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991βββ19βββ10βββ1. After three transformations the number becomes one-digit. | PASSED | 1,000 | standard input | 2 seconds | The first line contains the only integer n (0ββ€βnββ€β10100000). It is guaranteed that n doesn't contain any leading zeroes. | ["0", "1", "3"] | #include<stdio.h>
int a[1000005];
int main(void)
{
int n,i,j,sum=0;
char ch[1000005];
for(i=0; ;i++) {
scanf("%c",&ch[i]);
a[i]=ch[i]-'0';
if(ch[i]=='\n') {
break;
}
}
n=i;
if(n==1) {
printf("0");
return 0;
}
for(i=1; ;i++) {
for(j=0;j<n;j++) {
sum+=a[j];
}
if(sum>9) {
for(j=0;j<n;j++) {
a[j]=sum%10;
sum=sum/10;
if(sum==0) {
n=j+1;
break;
}
}
}
else {
break;
}
}
printf("%d",i);
return 0;
}
| |
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? | Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. | C | ddc9201725e30297a5fc83f4eed75fc9 | c7b7e8a93a1dc9b7300d1f45b437ce50 | GNU C | standard output | 265 megabytes | train_000.jsonl | [
"implementation"
] | 1312390800 | ["0", "10", "991"] | NoteIn the first sample the number already is one-digit β Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991βββ19βββ10βββ1. After three transformations the number becomes one-digit. | PASSED | 1,000 | standard input | 2 seconds | The first line contains the only integer n (0ββ€βnββ€β10100000). It is guaranteed that n doesn't contain any leading zeroes. | ["0", "1", "3"] | #include<stdio.h>
int main () {
char n[100000];
int i=0,sum=0,count=0,j,k,l;
scanf("%s",n);
while(n[i]!=0) {
sum+=n[i]-48;
i++;
}
if(sum>9)
count++;
k=sum;
while(k>9) {
l=0;
while(k!=0) {
j=k%10;
l+=j;
k/=10;
}
k=l;
count++;
}
if((n[1]!=0)&&(sum<10))
count++;
printf("%d",count);
return 0;
} | |
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? | Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. | C | ddc9201725e30297a5fc83f4eed75fc9 | 7f86ca7d3fb22b0b61f43f8fb2bec435 | GNU C | standard output | 265 megabytes | train_000.jsonl | [
"implementation"
] | 1312390800 | ["0", "10", "991"] | NoteIn the first sample the number already is one-digit β Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991βββ19βββ10βββ1. After three transformations the number becomes one-digit. | PASSED | 1,000 | standard input | 2 seconds | The first line contains the only integer n (0ββ€βnββ€β10100000). It is guaranteed that n doesn't contain any leading zeroes. | ["0", "1", "3"] | #include<stdio.h>
#include<string.h>
int main()
{
char s[100100];
int cnt,n,i,t,l;
scanf("%s",s);
l=strlen(s);
if(l==1) {printf("0");return 0;}
n=0;
for(i=0;i<l;i++)
n+=s[i]-'0';
cnt=1;
while(n/10!=0)
{
t=0;
while(n!=0)
{
t+=n%10;
n=n/10;
}
n=t;
cnt++;
}
printf("%d\n",cnt);
return 0;
} | |
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? | Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. | C | ddc9201725e30297a5fc83f4eed75fc9 | ff666d159106d05012d88e5e760fd952 | GNU C | standard output | 265 megabytes | train_000.jsonl | [
"implementation"
] | 1312390800 | ["0", "10", "991"] | NoteIn the first sample the number already is one-digit β Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991βββ19βββ10βββ1. After three transformations the number becomes one-digit. | PASSED | 1,000 | standard input | 2 seconds | The first line contains the only integer n (0ββ€βnββ€β10100000). It is guaranteed that n doesn't contain any leading zeroes. | ["0", "1", "3"] | #include <stdio.h>
int main (){
char a;
int count = 0;
int count2 = 0;
int res = 0;
int res2 = 0;
int res3 = 0;
while (a != '\n'){
scanf ("%c", &a);
res = res + (int)(a-'0');
count2++;
}
res = res + 38;
if (count2 >= 3)
count++;
while (res/10 != 0){
res2 = res;
while (res2 != 0){
res3 = res3 + res2%10;
res2 = res2 / 10;
}
res = res3;
res3 = 0;
count++;
}
printf ("%i\n", count);
return 0;
}
| |
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? | Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. | C | ddc9201725e30297a5fc83f4eed75fc9 | a35f55f70e304ee5074fa090be32d700 | GNU C | standard output | 265 megabytes | train_000.jsonl | [
"implementation"
] | 1312390800 | ["0", "10", "991"] | NoteIn the first sample the number already is one-digit β Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991βββ19βββ10βββ1. After three transformations the number becomes one-digit. | PASSED | 1,000 | standard input | 2 seconds | The first line contains the only integer n (0ββ€βnββ€β10100000). It is guaranteed that n doesn't contain any leading zeroes. | ["0", "1", "3"] | #include <stdio.h>
#include <string.h>
char s[100005 ] , a, b ;
int n , i , j , k , sum , q , t[100001] ;
int digit_count( int p )
{
if ( p == 0 ) return 0 ;
return 1 + digit_count( p / 10 ) ;
}
int digit_sum( int m )
{
if ( m == 0) return 0 ;
return m%10 + digit_sum(m / 10 ) ;
}
int main()
{
scanf("%s" , s) ;
n = strlen(s) ;
if ( n == 1 ) printf("0 ") ;
else
{
for ( i = 0 ; i < n ; i++) sum += s[i] - '0' ;
k = 1 ;
while ( 1 != digit_count( sum ) )
{
sum = digit_sum(sum ) ;
k++ ;
// printf("sum = %d\n", sum ) ;
}
printf("%d", k ) ;
}
return 0 ;
}
| |
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? | Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. | C | ddc9201725e30297a5fc83f4eed75fc9 | 0d165764c7af38eec09a0fa17124692f | GNU C | standard output | 265 megabytes | train_000.jsonl | [
"implementation"
] | 1312390800 | ["0", "10", "991"] | NoteIn the first sample the number already is one-digit β Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991βββ19βββ10βββ1. After three transformations the number becomes one-digit. | PASSED | 1,000 | standard input | 2 seconds | The first line contains the only integer n (0ββ€βnββ€β10100000). It is guaranteed that n doesn't contain any leading zeroes. | ["0", "1", "3"] | #include <stdio.h>
#include <string.h>
char s[1000001];
void func()
{
int n = 0, m, ans, len = strlen(s), i;
if (len == 1)
{
printf("0\n");
return;
}
for (i = 0; i < len; i++)
{
n += (s[i] - '0');
}
ans = 1;
while (n / 10 != 0)
{
m = 0;
while (n)
{
m += n % 10;
n /= 10;
}
ans++;
n = m;
}
printf("%d\n", ans);
}
int main(int argc, char **argv)
{
while (scanf("%s", s) == 1)
{
func();
}
return 0;
} | |
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? | Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. | C | ddc9201725e30297a5fc83f4eed75fc9 | 338beaf4fd6d7084bfeecff2d6b1c3bc | GNU C | standard output | 265 megabytes | train_000.jsonl | [
"implementation"
] | 1312390800 | ["0", "10", "991"] | NoteIn the first sample the number already is one-digit β Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991βββ19βββ10βββ1. After three transformations the number becomes one-digit. | PASSED | 1,000 | standard input | 2 seconds | The first line contains the only integer n (0ββ€βnββ€β10100000). It is guaranteed that n doesn't contain any leading zeroes. | ["0", "1", "3"] | #include <stdio.h>
#include <stdlib.h>
unsigned int sum_digits(char *array, int length)
{
int i, sum = 0;
for(i = 0; i < length; i++)
{
sum += array[i];
}
return sum;
}
char *mkarray(unsigned int n, int *length)
{
unsigned int temp = n;
int len = 0;
while(temp > 0)
{
temp /= 10;
len ++;
}
char *array = (char *)malloc(sizeof(unsigned int) * len);
*length = len;
unsigned int i = 0;
while(n > 0)
{
array[i] = (char)(n % 10);
n /= 10;
i++;
}
return array;
}
int main()
{
char *s = (char *)malloc(sizeof(char) * 100002);
scanf("%s", s);
char *p = s;
int sum = 0;
int len = 0;
while(*p != '\0')
{
*p = *p - '0';
p ++;
len ++;
}
int time = 0;
if(len > 1)
{
do
{
time ++;
sum = sum_digits(s, len);
free(s);
s = mkarray(sum, &len);
} while(sum >= 10);
}
printf("%d\n", time);
return 0;
}
| |
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? | Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. | C | ddc9201725e30297a5fc83f4eed75fc9 | dbc6114c5a4baa6cfce82e23c81e2049 | GNU C | standard output | 265 megabytes | train_000.jsonl | [
"implementation"
] | 1312390800 | ["0", "10", "991"] | NoteIn the first sample the number already is one-digit β Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991βββ19βββ10βββ1. After three transformations the number becomes one-digit. | PASSED | 1,000 | standard input | 2 seconds | The first line contains the only integer n (0ββ€βnββ€β10100000). It is guaranteed that n doesn't contain any leading zeroes. | ["0", "1", "3"] |
int main()
{
char s[100001];
int n,t,i,r;
scanf("%s",s);
t=0;
while (s[1])
{
t++;
r=0;
for(i=0;s[i];i++)
{
r+=s[i]-'0';
}
sprintf(s,"%d",r);
}
printf("%d\n",t);
return 0;
}
| |
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? | Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. | C | ddc9201725e30297a5fc83f4eed75fc9 | 58a39842704bc9b39991acba752f891f | GNU C | standard output | 265 megabytes | train_000.jsonl | [
"implementation"
] | 1312390800 | ["0", "10", "991"] | NoteIn the first sample the number already is one-digit β Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991βββ19βββ10βββ1. After three transformations the number becomes one-digit. | PASSED | 1,000 | standard input | 2 seconds | The first line contains the only integer n (0ββ€βnββ€β10100000). It is guaranteed that n doesn't contain any leading zeroes. | ["0", "1", "3"] | #include <stdio.h>
char s[100100];
int main()
{
int n,cnt,i,res;
scanf("%s",s);
cnt=0;
while (s[1])
{
cnt++;
res=0;
for(i=0;s[i];i++)
res+=s[i]-'0';
sprintf(s,"%d",res);
}
printf("%d\n",cnt);
return 0;
}
| |
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? | Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. | C | ddc9201725e30297a5fc83f4eed75fc9 | d4d2dbd0b4a479ac355e0694fd41121e | GNU C | standard output | 265 megabytes | train_000.jsonl | [
"implementation"
] | 1312390800 | ["0", "10", "991"] | NoteIn the first sample the number already is one-digit β Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991βββ19βββ10βββ1. After three transformations the number becomes one-digit. | PASSED | 1,000 | standard input | 2 seconds | The first line contains the only integer n (0ββ€βnββ€β10100000). It is guaranteed that n doesn't contain any leading zeroes. | ["0", "1", "3"] | #include <stdio.h>
int main()
{
char s[111111];
int m,i,res;
scanf("%s",s);
m=0;
while (s[1])
{
m++;
res=0;
for(i=0;s[i];i++)
res+=s[i]-'0';
sprintf(s,"%d",res);
}
printf("%d",m);
return 0;
}
| |
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? | Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. | C | ddc9201725e30297a5fc83f4eed75fc9 | 730d5832af1e4b9f4e4c4aa14a8817d3 | GNU C | standard output | 265 megabytes | train_000.jsonl | [
"implementation"
] | 1312390800 | ["0", "10", "991"] | NoteIn the first sample the number already is one-digit β Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991βββ19βββ10βββ1. After three transformations the number becomes one-digit. | PASSED | 1,000 | standard input | 2 seconds | The first line contains the only integer n (0ββ€βnββ€β10100000). It is guaranteed that n doesn't contain any leading zeroes. | ["0", "1", "3"] | #include<stdio.h>
#include<string.h>
char s[1000000];
int g(int a)
{
int z=0;
while(1)
{
z+=a%10;
a=a/10;
if(a==0)
return z;
}
}
int main()
{
scanf("%s",&s);
int len=strlen(s);
int x=0;
if(len==1)
{
printf("0\n");
return 0;
}
for(int i=0;i<len;i++)
{
x=x+s[i]-'0';
}
int ans=1;
while(x>9)
{
ans++;
x=g(x);
}
printf("%d\n",ans);
}
| |
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? | Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. | C | ddc9201725e30297a5fc83f4eed75fc9 | e6d78a1e0deb60b0c40c9469a2220266 | GNU C | standard output | 265 megabytes | train_000.jsonl | [
"implementation"
] | 1312390800 | ["0", "10", "991"] | NoteIn the first sample the number already is one-digit β Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991βββ19βββ10βββ1. After three transformations the number becomes one-digit. | PASSED | 1,000 | standard input | 2 seconds | The first line contains the only integer n (0ββ€βnββ€β10100000). It is guaranteed that n doesn't contain any leading zeroes. | ["0", "1", "3"] | #include<stdio.h>
#include<string.h>
int main()
{
char a[100002];
int sum=0,i,l,n=1,m,b=0;
scanf("%s",a);
l=strlen(a);
for(i=0;i<l;i++)
{
if(a[i]=='0')
sum+=0;
else if(a[i]=='1')
sum+=1;
else if(a[i]=='2')
sum+=2;
else if(a[i]=='3')
sum+=3;
else if(a[i]=='4')
sum+=4;
else if(a[i]=='5')
sum+=5;
else if(a[i]=='6')
sum+=6;
else if(a[i]=='7')
sum+=7;
else if(a[i]=='8')
sum+=8;
else if(a[i]=='9')
sum+=9;
}
while(sum>=10)
{
m=sum;
while(m>0)
{
b=b+m%10;
m=m/10;
}
sum=b;
b=0;
n++;
}
if(l==1)
printf("0\n");
else
printf("%d\n",n);
}
| |
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? | Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. | C | ddc9201725e30297a5fc83f4eed75fc9 | 2822f6d13ed1b5cd19f270993ae01f59 | GNU C | standard output | 265 megabytes | train_000.jsonl | [
"implementation"
] | 1312390800 | ["0", "10", "991"] | NoteIn the first sample the number already is one-digit β Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991βββ19βββ10βββ1. After three transformations the number becomes one-digit. | PASSED | 1,000 | standard input | 2 seconds | The first line contains the only integer n (0ββ€βnββ€β10100000). It is guaranteed that n doesn't contain any leading zeroes. | ["0", "1", "3"] | #include<stdio.h>
int main(void)
{
char c;
int n=0,b=0,x;
while(1)
{
scanf("%c",&c);
if(c=='\n')
break;
else
{
n+=c-48;
b++;
}
}
if(b==1)
x=0;
else
x=1;
int s=0;
if(n>9)
{
while(n)
{
s+=n%10;
n=n/10;
if(n==0&&s<10)
x++;
else if(n==0&&s>=10)
{
n=s;
s=0;
x++;
}
}
}
printf("%d\n",x);
return 0;
}
| |
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? | Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. | C | ddc9201725e30297a5fc83f4eed75fc9 | d6759048ea9670cf2e522b3a7ea91708 | GNU C | standard output | 265 megabytes | train_000.jsonl | [
"implementation"
] | 1312390800 | ["0", "10", "991"] | NoteIn the first sample the number already is one-digit β Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991βββ19βββ10βββ1. After three transformations the number becomes one-digit. | PASSED | 1,000 | standard input | 2 seconds | The first line contains the only integer n (0ββ€βnββ€β10100000). It is guaranteed that n doesn't contain any leading zeroes. | ["0", "1", "3"] | #include<stdio.h>
#include<string.h>
char s[100100];
int main()
{
int n,cnt,i,res;
scanf("%s",s);
cnt=0;
while (s[1])
{
cnt++;
res=0;
for(i=0;s[i];i++)
{
res+=s[i]-'0';
}
sprintf(s,"%d",res);
}
printf("%d\n",cnt);
return 0;
}
| |
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? | Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. | C | ddc9201725e30297a5fc83f4eed75fc9 | 93b0bfa6bc442328a9d9b6b8319ac3b0 | GNU C | standard output | 265 megabytes | train_000.jsonl | [
"implementation"
] | 1312390800 | ["0", "10", "991"] | NoteIn the first sample the number already is one-digit β Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991βββ19βββ10βββ1. After three transformations the number becomes one-digit. | PASSED | 1,000 | standard input | 2 seconds | The first line contains the only integer n (0ββ€βnββ€β10100000). It is guaranteed that n doesn't contain any leading zeroes. | ["0", "1", "3"] | #include<stdio.h>
#include<string.h>
int main()
{
char str[100002];
int n,l=0,count=0,sum=0,i;
scanf("%s",str);
n=strlen(str);
while(n!=1)
{
for(i=0;str[i]!='\0';i++)
{
sum=sum+str[i]-'0';
}
l=0;
while(sum)
{
if(sum%10==0)
{
str[l++]=48;sum=sum/10;
}
else
{
str[l++]=(sum%10)+48;
sum=sum/10;
}
str[l]='\0';
}
n=strlen(str);
count++;
sum=0;
}
printf("%d",count);
}
| |
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit? | Print the number of times a number can be replaced by the sum of its digits until it only contains one digit. | C | ddc9201725e30297a5fc83f4eed75fc9 | 54f4bd61b2da9569616e74db6a69b567 | GNU C | standard output | 265 megabytes | train_000.jsonl | [
"implementation"
] | 1312390800 | ["0", "10", "991"] | NoteIn the first sample the number already is one-digit β Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991βββ19βββ10βββ1. After three transformations the number becomes one-digit. | PASSED | 1,000 | standard input | 2 seconds | The first line contains the only integer n (0ββ€βnββ€β10100000). It is guaranteed that n doesn't contain any leading zeroes. | ["0", "1", "3"] | #include<stdio.h>
#include<string.h>
int main()
{
int i, j, k, sum=100;
char ar[100001];
scanf("%s", &ar);
int ans=0;
int len = strlen(ar);
if(len<2)
printf("0");
else
{
ans=0;k=0;
for(i=0;i<len;i++)
k = k+ar[i]-'0';
ans++;
//printf("k=%d\n", k);
if(k<10)
printf("1");
else
{
sum=k;
//printf("%d\n", sum);
while(sum>=10)
{
sum=0;
j=k;
while(j>0)
{
sum+=j%10;
j/=10;
}
ans++;
k = sum;
}
printf("%d", ans);
}
}
return 0;
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.