Ejemplo n.º 1
0
int main() {
	freopen("b.in", "r", stdin);
	while (~scanf("%d %lld", &n, &p)) {
		s.clear();
		long long x;
		for (int i = 0; i < n; i++) {
			scanf("%lld", &x);
			arr[i] = x;
			s.insert(x);
		}
		long long ans = 0;
		for (int i = 0; i < n; i++) {
			long long now = arr[i];
			set<long long>::iterator it = s.lower_bound(now);

			s.erase(it);
			it = s.lower_bound(p - now);
			if (it != s.begin()) it--;
			ans = max(ans, (now + *it) % p);
			it = s.lower_bound(p + p - now);
			if (it != s.begin()) it--;
			ans = max(ans, (now + *it) % p);

			s.insert(now);
		}


		cout << ans << endl;
	}
	


	return 0;
}
Ejemplo n.º 2
0
main()
{
    int n,m,Q ; scanf("%d%d%d",&m,&n,&Q) ;
    s1.insert(0) ; s1.insert(n) ;
    s2.insert(0) ; s2.insert(m) ;
    d1.insert(n) ; d2.insert(m) ;
    while(Q--)
    {
        char s[6] ; int x ;
        scanf("%s%d",s,&x) ;
        if(s[0]=='H')
        {
            auto it=s1.lower_bound(x) ;
            int x2=*it ; it-- ; int x1=*it ;
            d1.erase(d1.find(x2-x1)) ;
            d1.insert(x2-x) ;
            d1.insert(x-x1) ;
            s1.insert(x) ;
        }
        else
        {
            auto it=s2.lower_bound(x) ;
            int x2=*it ; it-- ; int x1=*it ;
            d2.erase(d2.find(x2-x1)) ;
            d2.insert(x2-x) ;
            d2.insert(x-x1) ;
            s2.insert(x) ;
        }
        printf("%I64d\n",(LL)(*d1.begin())*(*d2.begin())) ;
    }
}
Ejemplo n.º 3
0
int main() {
	int idx, x;
	while(~scanf("%d%d", &n, &m)) {
		se.clear();
		se2.clear();
		mn = INF;
		for(int i=1; i<=n; i++) {
			scanf("%d", a+i);
			se.insert(a[i]);
			b[i] = a[i];
		}
		for(int i=2; i<=n; i++)
			se2.insert(myabs(a[i]-a[i-1]));
		for(multiset<int>::iterator it = se.begin(); ; ) {
			multiset<int>::iterator it2 = it;
			it++;
			if(it == se.end()) 
				break;
			mn = min(mn, myabs((*it) - (*it2)));
		}
		//show_se();
		//cout << "Min: " << mn << endl;
		while(m--) {
			scanf("%s", com);
			if(com[0] == 'I') {
				scanf("%d%d", &idx, &x);

				multiset<int>::iterator it = se.lower_bound(x);
				mn = min(mn, myabs((*it) - x));
				if(it!=se.begin()) {
					it--;
					mn = min(mn, myabs(x - (*it)));
				}
				se.insert(x);

				if(idx!=n) {
					it = se2.lower_bound(myabs(b[idx] - a[idx+1]));
					//se2.erase(myabs(b[i] - a[i+1]));
					se2.erase(it);
					se2.insert(myabs(x - a[idx+1]));
				}
				se2.insert(myabs(b[idx] - x));
				b[idx] = x;

				//show_se();
			} else if(com[1] == 'i') {
				printf("%d\n", mn);
			} else {
				multiset<int>::iterator it = se2.end();
				it--;
				printf("%d\b", *it);
			}
		}
	}
	return 0;
}
Ejemplo n.º 4
0
int main(){
#ifndef ONLINE_JUDGE
  freopen("in","r",stdin);
#endif
  scanf("%d",&n);
  int maxScore=0,minScore=2*n;
  For(i,n)scanf("%d",&a[i]);
  For(i,n)scanf("%d",&b[i]);

  sort(b+1,b+n+1);
  For(i,n)st.insert(a[i]);
  for(int i=n; i; i--){
    it=st.upper_bound(b[i]);
    if(it!=st.end()){
      maxScore+=2;
      st.erase(it);
      mark[i]=1;
    }
  }
  For(i,n)if(!mark[i]){
    it=st.lower_bound(b[i]);
    if(it!=st.end()){
      maxScore++;
      st.erase(it);
    }
  }

  st.clear();
  memset(mark,0,sizeof(mark));
  sort(a+1,a+n+1);
  For(i,n)st.insert(b[i]);
  for(int i=n; i; i--){
    it=st.upper_bound(a[i]);
    if(it!=st.end()){
      minScore-=2;
      st.erase(it);
      mark[i]=1;
    }
  }
  For(i,n)if(!mark[i]){
    it=st.lower_bound(a[i]);
    if(it!=st.end()){
      minScore--;
      st.erase(it);
    }
  }
  printf("%d %d\n",maxScore,minScore);
  return 0;
}
Ejemplo n.º 5
0
void solve() {
  int rez = 0;
  
  multiset <int> :: iterator it;


  for (int i = 1; i <= n; ++i) {
    it = s.lower_bound(v[i].first);
  
    if (it != s.end() && *it == v[i].first) {
      s.erase(it);
      s.insert(v[i].second);
      ++rez;
    } else {
      if (it == s.begin())
        continue;

      --it;

      s.erase(it);
      s.insert(v[i].second);
      ++rez;
      
    }
  }

  printf("%d\n", rez);
}
Ejemplo n.º 6
0
int main(){
    int t,n;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            int h,w;
            scanf("%d%d",&h,&w);
            v[i].get(h,w,0);
        }
        for(int i=0;i<n;i++){
            int h,w;
            scanf("%d%d",&h,&w);
            v[i+n].get(h,w,1);
        }
        sort(v,v+2*n,cmp);
        m.clear();
        int ans=0;
        for(int i=0;i<2*n;i++){
        //    cout<<v[i].id<<" "<<v[i].h<<" "<<v[i].w<<endl;
            if(v[i].id==0) m.insert(v[i].w);
            else{            
                it=m.lower_bound(v[i].w);
                if(it!=m.end()){
                    m.erase(it);
                    ans++;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
Ejemplo n.º 7
0
int main() {
#ifdef HOME
    freopen("input.txt", "r", stdin);
#endif

    scanf("%d%d", &n, &k);
    i64 s = 0;
    forn(i, n) scanf("%d", &b[i]), s += b[i];
    forn(i, n) scanf("%d", &a[i]);
    sort(a, a+n);
    f.insert(b, b+n);

    ford(i, n) {
        auto it = f.lower_bound(a[i]);
        if (it == f.end()) {
            if (k == 0) {
                printf("NIE\n");
                return 0;
            }
            add += a[i];
            --k;
            a[i] = 0;
        } else {
            a[i] = *it - a[i];
            f.erase(it);
        }
    }
Ejemplo n.º 8
0
int main(int argc, char const *argv[])
{
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%I64d", A + i);
        s += A[i];
        ps[i + 1] = ps[i] + A[i];
    }
    sum.insert(s);
    qu.insert(Pii(n, 0));
    for (int i = 0, p; i < n; ++i) {
        scanf("%d", &p); --p;
        auto it = qu.upper_bound(Pii(p, 0));
        // cout << it->second << " " << it->first << endl;
        ll tmp = ps[it->first] - ps[it->second];
        // cout << "tmp " << tmp << endl;
        sum.erase(sum.lower_bound(tmp));

        tmp = ps[p] - ps[it->second];
        // cout << "tmp " << tmp << endl;
        sum.insert(tmp); qu.insert(Pii(p, it->second));

        tmp = ps[it->first] - ps[p + 1];
        // cout << "tmp " << tmp << endl;
        sum.insert(tmp); qu.insert(Pii(it->first, p + 1));

        qu.erase(it);
        printf("%I64d\n", *sum.rbegin());
    }
    return 0;
}
Ejemplo n.º 9
0
int main() {

    scanf( "%d %d", &C, &G );
    for ( i = 0; i < C + G; i++ ) {
        scanf( "%d %d", &j, &k );
        ls.push_back( make_pair( k, ( i < C ) ? -j : j ) );
    }

    sort( ls.rbegin(), ls.rend() );

    for ( i = 0; i < C + G; i++ )
        if ( ls[i].second > 0 )
            S.insert( ls[i].second );
        else { /* It's a cow */
            set< int >::iterator it = S.lower_bound( -ls[i].second );
            if ( it != S.end() ) {
                sol += *it;
                S.erase( it );
            } else {
                sol = -1;
                break;
            }
        }

    printf( "%lld\n", sol );

    return 0;
}
Ejemplo n.º 10
0
int main()
{
	int T;
	scanf("%d",&T);
	for(int kase=1; kase<=T; kase++)
	{
		st.clear();
		int n;
		scanf("%d",&n);
		printf("Case #%d:\n",kase);
		while(n--)
		{
			P t;
			scanf("%d%d",&t.x,&t.y);
			it=st.lower_bound(t);
			if(it==st.begin()||(--it)->y>t.y)
			{
			//printf("OK");
				st.insert(t);
				it=st.upper_bound(t);
				while(it!=st.end()&&it->y>=t.y)
				{
					//printf("%d %d\n",it->x,it->y);
					st.erase(it++);
				}
			}
			printf("%d\n",st.size());
		}
		if(kase!=T)
			printf("\n");
	}
	return 0;
}
Ejemplo n.º 11
0
int main()
{
    int n,d,r;
    while(scanf("%d%d%d",&n,&d,&r)!=EOF&&n||d||r)
    {
        int k;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&k);
            s1.insert(k);
        }
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&k);
            s2.insert(k);
        }
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            int num=*s1.begin();
            int index=d-num;
            __typeof(s2.begin()) it=s2.lower_bound(index);
            if(it==s2.end())it--;
            ans+=r*max(0,num+*it-d);
            s1.erase(s1.begin());
            s2.erase(it);
        }
        printf("%d\n",ans);
    }
    return 0;
}
int main() {
  int n, m, p, b; scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) {
    scanf("%d%lld", &A[i].x, &A[i].t);
    ++A[i].t;
    A[i].idx = i;
  }
  sort(A, A + n, [&] (Frog A, Frog B) {return A.x > B.x;});
  for (int i = 0; i < n; ++i) root -> set(A[i].x, min(A[i].x + A[i].t, (LL)INF), i);
  while (m--) {
    scanf("%d%d", &p, &b);
    int frog = root -> at(p);
    if (frog == -1) mos.insert({p, b});
    else {
      A[frog].t += b;
      ++A[frog].mos_count;
      auto it = mos.lower_bound({A[frog].x, 0});
      while (it != mos.end() and it -> first < A[frog].x + A[frog].t) {
        A[frog].t += it-> second;
        ++A[frog].mos_count;
        mos.erase(it);
        it = mos.upper_bound({A[frog].x, 0});
      }
      root -> set(A[frog].x, min(A[frog].x + A[frog].t,(LL) INF), frog);
    }
  }
  sort(A, A + n, [&] (Frog A, Frog B) {return A.idx < B.idx;});
  for (int i = 0; i < n; ++i) printf("%d %lld\n", A[i].mos_count, A[i].t - 1);
}
Ejemplo n.º 13
0
void solve(int idx)
{
	if(flag)
		puts("");
	flag=1;
	scanf("%d",&N);
	printf("Case #%d:\n", idx);
	Ms.clear();
	while(N--)
	{
		int L,R;
		scanf("%d %d",&L,&R);
		Point tmp;
		tmp.L=L;tmp.R=R;
		it=Ms.lower_bound(tmp);
		if(it==Ms.begin()||(--it)->R>R)
		{
			Ms.insert(tmp);
			it=Ms.upper_bound(tmp);
			while(it!=Ms.end()&&it->R>=tmp.R)
			{
				Ms.erase(it++);
			}
		}
		printf("%d\n",Ms.size());
	}
}
Ejemplo n.º 14
0
int main() {
	#ifndef ONLINE_JUDGE
	freopen("528a.in", "r", stdin);
	freopen("528a.out", "w", stdout);
	#endif

	cin >> w >> h >> n;
	rx.insert(0);
	rx.insert(w);
	ry.insert(0);
	ry.insert(h);
	distx.insert(-w);
	disty.insert(-h);
	for(int i = 1 ; i <= n ; ++ i) {
		char op; int x;
		cin >> op >> x;
		if(op == 'V') {
			auto p1 = rx.lower_bound(x);
			auto p2 = rx.lower_bound(x);
			if(p1 != rx.begin())
				-- p1;
	//		cout << "x \n";
	//		cout << *p1 << ' ' << *p2 << '\n';
			rx.insert(x);
			distx.erase(distx.find(-(*p2 - *p1)));
			distx.insert(-(x - *p1));
			distx.insert(-(*p2 - x));
			cout << 1LL * (*distx.begin()) * (*disty.begin()) << '\n';
		}
		else {
			auto p1 = ry.lower_bound(x);
			auto p2 = ry.lower_bound(x);
			if(p1 != ry.begin())
				-- p1;
	//		cout << "y \n";
	//		cout << *p1 << ' ' << *p2 << '\n';
			ry.insert(x);
			disty.erase(disty.find(-(*p2 - *p1)));
			disty.insert(-(x - *p1));
			disty.insert(-(*p2 - x));
			cout << 1LL * (*distx.begin()) * (*disty.begin()) << '\n';
		}
	}
	
}
Ejemplo n.º 15
0
int main()
{
    //freopen("I.in","r",stdin);
    //freopen("I.out","w",stdout);
    int T;
    scanf("%d", &T);
    for(int cas=1;cas<=T;cas++)
    {
        mst.clear();
        int n, m, k, cnt = 0;
        long long  num;
        scanf("%d%d%d", &n, &m, &k);
        for(int i = 0; i < n; ++i)
        {
            scanf("%lld", &num);
            if(num <= k)
            {
                mst.insert(num);
            }
        }
        while(true)
        {
            int sz = mst.size();
            if(sz < 2)
                break;
            it1 = mst.end();
            it1--;
            long long  fst = *it1;
            mst.erase(it1);
            it2 = mst.lower_bound(min(fst, k - fst));
            if(it2 == mst.end())
                it2--;
            if(it2 == mst.begin() && *it2 > (k - fst))
                continue;
            if(*it2 <= k - fst)
            {
                ans[cnt++] = fst * ( *it2);
                mst.erase(it2);
            }
            else
            {
                it2 --;
                ans[cnt++] = fst * (*it2);
                mst.erase(it2);
            }
        }
        sort(ans, ans + cnt, cmp);
        long long  res = 0;
        for(int i = 0; i < min(m, cnt); ++i)
        {
            res += ans[i];
        }
        printf("CASE #%d: %lld\n",cas,res);
    }
    return 0;
}
Ejemplo n.º 16
0
inline void modify(int u) {
    col[u] ^= true;
    sum += col[u] ? -1 : 1;
    while(u) {
        d[u] = getD(u);
        if(!col[u]) d[u] = make_pair(max(0, d[u].first), max(0, d[u].second));
        int val = tree[root[pathR[u]]].opt;ans.erase(ans.lower_bound(-val));
        modify(root[pathR[u]], pos[pathR[u]], pos[pathR[u]]+csz[pathR[u]]-1, u);
        adjust(pathR[u]), ans.insert(-tree[root[pathR[u]]].opt), u = fa[pathR[u]];
    }
}
Ejemplo n.º 17
0
void erase_interval(int x, int y){
    x = *(apples.lower_bound(x));
    multiset<int>::iterator it = apples.upper_bound(x);
    vector<int> res;
    while(y && it != apples.end()){
	if(*it > x + b)break;
	add_apple(max(*it - b, 0), *it, root, -1);
	res.push_back(*it);
	apples.erase(it++);
	y--;
    }
    it = apples.lower_bound(x);
    while(y--){
	add_apple(max(*it - b, 0), *it, root, -1);
	cout << *it << ' ';
	apples.erase(it++);
    }
    vector<int>::iterator vit = res.begin();
    while(vit != res.end())
	cout << *vit++ << ' ';
    cout << endl;
}
Ejemplo n.º 18
0
int two_array(multiset<int> &a, multiset<int> &b, int k)
{
	for(int i : a)
	{
		multiset<int>::iterator it = b.lower_bound(k-i);
		if(it == b.end())
			return 0;	
		
		b.erase(it);
	}
	
	return 1;
}
Ejemplo n.º 19
0
int main(){
//    freopen("input.txt","r",stdin);

    int T,n,v;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        mys.clear();
        for(int i=0; i<n; ++i){
            scanf("%d",&v);
            multiset<int>::iterator it = mys.lower_bound(v+1);
            if(it!=mys.end()) mys.erase(it);
            mys.insert(v);
        }
        printf("%d",sz(mys));
        for(multiset<int>::iterator it=mys.begin(); it!=mys.end(); ++it) printf(" %d",*it);
        puts("");
    }
}
Ejemplo n.º 20
0
int main()
{
	#ifdef fn
		freopen(fn ".in", "r", stdin);
		freopen(fn ".out", "w", stdout);
	#endif
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &x);
		se.insert(x);
	}
	for (int i = 1; i <= n + 1; i++) {
		auto it = se.lower_bound(i);
		if (it == se.end()) {
			printf("%d ", i);
			return 0;
		}
		se.erase(it);
	}
}
Ejemplo n.º 21
0
int main()
{
    scanf("%d",&T);
    multiset<int>::iterator iter;
    int tmph,tmpw;
    int sum;

    while(T--)
    {
        scanf("%d",&N);
        myMs.clear();
        sum=0;
        for(int i=0;i<N;++i)
        {
            scanf("%d%d",&tmph,&tmpw);
            cs[i].setValue(tmph,tmpw,P_A);
        }
        for(int i=0;i<N;++i)
        {
            scanf("%d%d",&tmph,&tmpw);
            cs[i+N].setValue(tmph,tmpw,P_B);
        }
        sort(cs,cs+N+N,comp);
        for(int i=0;i<N+N;++i)
        {
            if(cs[i].b==P_A)
                myMs.insert(cs[i].w);
            else
            {
                iter=myMs.lower_bound(cs[i].w);
                if(iter!=myMs.end())
                {
                    ++sum;
                    myMs.erase(iter);
                }
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}
Ejemplo n.º 22
0
 void change(int x){
   int t;
typeof(S.begin()) it;
   if(col[x])er(S,mp(x,0));else S.insert(mp(x,0));
   for(col[x]^=1;x;x=jump[x]){
     n=bel[x];
     if(jump[x]&&!S.empty()){
       it=S.find(mp(jump[x],Gm(D1,sl[n],sl[n+1]-1)-sl[n]+1));
       if(it!=S.end())S.erase(it);
     }
     if(!S.empty()){
    it=S.lower_bound(mp(x,-INF));
        t=(it!=S.end()&&it->first==x)?it->second:INF;
  }else t=INF;
     D0[pos[x]+tn-1]=t-pos[x],D1[pos[x]+tn-1]=t+pos[x];
     for(t=(pos[x]+tn-1)>>1;t;t>>=1)
       D0[t]=max(D0[L(t)],D0[R(t)]),D1[t]=max(D1[L(t)],D1[R(t)]);
     if(jump[x])
       S.insert(mp(jump[x],Gm(D1,sl[n],sl[n+1]-1)-sl[n]+1));
   }
 }
int main()
{
    ifstream in("planificare.in");
    ofstream out("planificare.out");

    in >> N >> K;

    for ( int i = 1; i <= N; ++i )
        in >> v[i].x >> v[i].y;

    sort(v + 1, v + N + 1);

    for ( int i = 1; i <= K; ++i )
        MS.insert(0);

    for ( int i = 1; i <= N; ++i )
    {
        auto it = MS.lower_bound(v[i].x);

        if ( it != MS.end() && *it == v[i].x )
        {
            sol++;
            MS.erase(it);
            MS.insert(v[i].y);
            continue;
        }

        if ( it == MS.begin() )
            continue;

        it--;
        sol++;
        MS.erase(it);
        MS.insert(v[i].y);
    }

    out << sol << "\n";

    return 0;
}
Ejemplo n.º 24
0
void putsegment(const int id, const vector<seg> &a)
{
    pdd tmpp;
    set<seg>::iterator
    nxt = s.lower_bound (a[id]),
    prv = prev (nxt);
    if (nxt != s.end() && abs(nxt->theta-a[id].theta) > EPS)
    {
        tmpp = inspoint(*nxt, a[id]);
        //debug("pushing3", nxt->id, id, tmpp.first, nxt->p.x, nxt->q.x);
        if(inrange(*nxt, a[id], tmpp.first) && tmpp.first > cur_x+EPS)
            e.push(event(tmpp.first, tmpp.second, 0, nxt->id, id));
    }
    if (prv != s.end() && abs(prv->theta-a[id].theta) > EPS)
    {
        tmpp = inspoint(*prv, a[id]);
        //debug("pushing2", id, prv->id, tmpp.first, prv->p.x, prv->q.x);
        if(inrange(*prv, a[id], tmpp.first) && tmpp.first > cur_x+EPS)
            e.push(event(tmpp.first, tmpp.second, 0, prv->id, id));
    }
    //debug("pushin", id);
    where[id] = s.insert (nxt, a[id]);
}
Ejemplo n.º 25
0
int main()
{
 lg i,s1[99],s2[99],j,x,sm,cn,y;
 scanf("%lld%lld%lld",&n,&a,&b);

 n1=n/2;//pahle array ko hi likh  lelete hai n1 pahle array me members ki  count hai 
 n2=n-n1;
 for(i=0;i<n1;i++)
 scanf("%lld",&s1[i]);

 for(i=0;i<n2;i++)
 scanf("%lld",&s2[i]);//chalo dusra aarya inputees

 //For first array
//chalo cool hai
 x1=pow(2,n1);
 
 
 for(i=0;i<x1;i++)
 {
   sm=0;

   for(j=0;j<n1;j++)
     if((i&(1<<j))>0)
       sm+=s1[j];
 
  os.insert(sm);
          
 }

/*cout<<"Printtign first array\n";
for(i=0;i<x1;i++)
cout<<a1[i]<<"  ";
cout<<endl;
*///pahla arra complete

//for second array
 x2=pow(2,n2);
 sm=0;

 for(i=0;i<x2;i++)
 {
   sm=0;

   for(j=0;j<n2;j++)//bhai second array n1 se suru hokar n tak jaega
    if((i&(1<<j))>0)
     sm+=s2[j];
 
 ts.insert(sm);         
 }

//chalo dusra bhi insert hoke stored ho gae


cn=0;
p=os.begin();
for(i=0;i<x1;i++)
{
  
  y=*p;
  p++;

 lb=ts.lower_bound(a-y);

 up=ts.upper_bound(b-y);
 up--;

 if( lb!=os.end() && up!=os.end() )
  { 
     pi=up;
     pi++;
     while(lb!=pi)
     {lb++;cn++;}
    
  }
 else if(lb==os.end() && up!=os.end() )
   {
      while(up!=os.begin() )
      {up--;cn++;}
       cn++;//bcz ek valye chhut rahi hai
   }
 else if(lb!=os.end() && up==os.end() )
   {
       while(lb!=os.end() )
      {lb++;cn++;}
   }

}


printf("%lld\n",cn);
return 0;
}
Ejemplo n.º 26
0
 /** Return the number of hits in the past 5 minutes.
     @param timestamp - The current timestamp (in seconds granularity). */
 int getHits(int timestamp) {
     auto begin = ss.lower_bound(timestamp-300+1>0? timestamp-300+1:0);
     auto end = ss.upper_bound(timestamp);
     return distance(begin, end);
 }
int main () {
    scanf(" %d %d %d", &n, &s, &t);
    for (int i = 0; i < n; i++) {
        scanf(" %d", &v[i]);
        st.insert(v[i]);
    }

    ll _min = inf, _max = 0, aver = 0;
    for (auto i = st.begin(); i != st.end(); i++) {
        int from = *i;
        for (int j = 0; j < n; j++) 
            ms.insert(v[j]);

        int cur = from;
        ll cont = 0;
        while (!ms.empty()) {
            if (ms.find(cur) != ms.end()) {
                ms.erase(ms.find(cur));
                cur = (cur + t) % s;
                cont += ll(t);
            } else {
                auto it = ms.lower_bound(cur);
                if (it == ms.end())
                    it = ms.begin();
                int nx = *it;
                if (nx > cur)
                    cont += ll(nx - cur);
                else 
                    cont += ll(s - cur + nx);
                cur = nx;
            }
        }
        cost[from] = cont;

        _min = min(_min, cont);
        _max = max(_max, cont);
        aver += cont;
    }

    for (int i = 0; i < n; i++)
        ms.insert(v[i]);
    for (int i = 0; i < s; i++) {
        if (ms.find(i) != ms.end())
            continue;
        auto it = ms.lower_bound(i);
        if (it == ms.end())
            it = ms.begin();
        int nx = *it;
        ll cont = 0;
        if (nx > i)
            cont = ll(nx - i + cost[nx]);
        else 
            cont = ll(s - i + nx + cost[nx]);

        _min = min(_min, cont);
        _max = max(_max, cont);
        aver += cont;
    }

    ll d = gcd(aver, s);
    printf("%lld %lld %lld/%lld\n", _min, _max, aver / d, s / d);
}
int main(){
    powB[0] = powB2[0] = 1;
    
    for(int i = 1;i < MAXL;++i){
        powB[i] = B * powB[i - 1];
        powB2[i] = B * powB2[i - 1] % MOD;
    }
    
    int T,L;
    
    scanf("%d",&T);
    
    while(T--){
        scanf("%s",s);
        
        L = strlen(s);
        
        H[L] = H3[L] = 0;
        
        for(int i = L - 1;i >= 0;--i){
            H[i] = ((s[i] >= 'a' && s[i] <= 'z')? s[i] - 'a' : s[i] - 'A' + 26) + B * H[i + 1];
            H3[i] = (((s[i] >= 'a' && s[i] <= 'z')? s[i] - 'a' : s[i] - 'A' + 26) + B * H3[i + 1]) % MOD;
        }
        
        H2[0] = H4[0] = 0;
        
        for(int i = 0;i < L;++i){
            H2[i + 1] = ((s[i] >= 'a' && s[i] <= 'z')? s[i] - 'a' : s[i] - 'A' + 26) + B * H2[i];
            H4[i + 1] = (((s[i] >= 'a' && s[i] <= 'z')? s[i] - 'a' : s[i] - 'A' + 26) + B * H4[i]) % MOD;
        }
        
        for(int i = 0;i < L;++i){
            int lo = 0,hi = min(i + 1,L - i - 1),mi;
            
            while(lo < hi){
                mi = (lo + hi + 1) >> 1;
                
                if(get_hash3(i - mi + 1,i) != get_hash4(i + 1,i + mi)) hi = mi - 1;
                else lo = mi;
            }
            
            r[i] = lo;
        }
        
        int Q = 0;
        vq.clear();
        in.clear();
        out.clear();
        
        for(int i = 0;i < L;++i){
            if(r[i]){
                int r2 = r[i] / 2;
                
                vq.push_back(make_pair(i,i + 1 - r2));
                in.push_back(make_pair(i + 1,i));
                out.push_back(make_pair(i + r[i] + 1,i));
                ++Q;
            }
        }
        
        sort(vq.begin(),vq.end());
        sort(in.begin(),in.end());
        sort(out.begin(),out.end());
        
        S.clear();
        int ans = 0;
        
        for(int i = 0,p1 = 0,p2 = 0,p3 = 0;i < L;++i){
            while(p3 < Q && out[p3].first == i){
                it = S.find(out[p3].first - r[ out[p3].second ]);
                S.erase(it);
                ++p3;
            }
            
            while(p2 < Q && in[p2].first == i){
                S.insert(in[p2].first);
                ++p2;
            }
            
            while(p1 < Q && vq[p1].first == i){
                it = S.lower_bound( vq[p1].second );
                if(it != S.end()) ans = max(ans,i - *it + 1);
                ++p1;
            }
        }
        
        printf("%d\n",ans * 4);
    }
    
    return 0;
}