2021-1-11

D. a-Good String

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a string s[1…n]s[1…n] consisting of lowercase Latin letters. It is guaranteed that n=2kn=2k for some integer k≥0k≥0.

The string s[1…n]s[1…n] is called cc-good if at least one of the following three conditions is satisfied:

  • The length of ss is 11, and it consists of the character cc (i.e. s1=cs1=c);
  • The length of ss is greater than 11, the first half of the string consists of only the character cc (i.e. s1=s2=⋯=sn2=cs1=s2=⋯=sn2=c) and the second half of the string (i.e. the string sn2+1sn2+2…snsn2+1sn2+2…sn) is a (c+1)(c+1)-good string;
  • The length of ss is greater than 11, the second half of the string consists of only the character cc (i.e. sn2+1=sn2+2=⋯=sn=csn2+1=sn2+2=⋯=sn=c) and the first half of the string (i.e. the string s1s2…sn2s1s2…sn2) is a (c+1)(c+1)-good string.

For example: "aabc" is 'a'-good, "ffgheeee" is 'e'-good.

In one move, you can choose one index ii from 11 to nn and replace sisi with any lowercase Latin letter (any character from 'a' to 'z').

Your task is to find the minimum number of moves required to obtain an 'a'-good string from ss (i.e. cc-good string for c=c= 'a'). It is guaranteed that the answer always exists.

You have to answer tt independent test cases.

Another example of an 'a'-good string is as follows. Consider the string s=s="cdbbaaaa". It is an 'a'-good string, because:

  • the second half of the string ("aaaa") consists of only the character 'a';
  • the first half of the string ("cdbb") is 'b'-good string, because:
    • the second half of the string ("bb") consists of only the character 'b';
    • the first half of the string ("cd") is 'c'-good string, because:
      • the first half of the string ("c") consists of only the character 'c';
      • the second half of the string ("d") is 'd'-good string.

Input

The first line of the input contains one integer tt (1≤t≤2⋅1041≤t≤2⋅104) — the number of test cases. Then tt test cases follow.

The first line of the test case contains one integer nn (1≤n≤131 0721≤n≤131 072) — the length of ss. It is guaranteed that n=2kn=2k for some integer k≥0k≥0. The second line of the test case contains the string ss consisting of nn lowercase Latin letters.

It is guaranteed that the sum of nn does not exceed 2⋅1052⋅105 (∑n≤2⋅105∑n≤2⋅105).

Output

For each test case, print the answer — the minimum number of moves required to obtain an 'a'-good string from ss (i.e. cc-good string with c=c= 'a'). It is guaranteed that the answer exists.

Example

input

Copy

6
8
bbdcaaaa
8
asdfghjk
8
ceaaaabb
8
bbaaddcc
1
z
2
ac

output

Copy

0
7
4
5
1
1

return 的是改变的个数,理解题意很重要

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
char str[200005];
int check(int s,int e,int len,int k)
{
    if(len==1)
    {
        if(str[s]==k+'a')
            return 0;
        else
            return 1;
    }
    else
    {
        int mid=(s+e)/2;
        int numl=0,numr=0;
        for(int i=s; i<=mid; i++)
        {
            if(str[i]==k+'a')
            {
               numl++;
            }
        }
        for(int i=mid+1;i<=e;i++)
        {
            if(str[i]==k+'a')
            {
                numr++;
            }
        }
        return min(len/2-numl+check(mid+1,e,len/2,k+1),len/2-numr+check(s,mid,len/2,k+1));
 
    }
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int len;
        scanf("%d",&len);
        scanf("%s",str);
        cout<<check(0,len-1,len,0)<<endl;
    }
 
 
    return 0;
 
}

H. Ahahahahahahahaha

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Alexandra has an even-length array aa, consisting of 00s and 11s. The elements of the array are enumerated from 11 to nn. She wants to remove at most n2n2 elements (where nn — length of array) in the way that alternating sum of the array will be equal 00 (i.e. a1−a2+a3−a4+…=0a1−a2+a3−a4+…=0). In other words, Alexandra wants sum of all elements at the odd positions and sum of all elements at the even positions to become equal. The elements that you remove don't have to be consecutive.

For example, if she has a=[1,0,1,0,0,0]a=[1,0,1,0,0,0] and she removes 22nd and 44th elements, aa will become equal [1,1,0,0][1,1,0,0] and its alternating sum is 1−1+0−0=01−1+0−0=0.

Help her!

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1031≤t≤103). Description of the test cases follows.

The first line of each test case contains a single integer nn (2≤n≤1032≤n≤103, nn is even)  — length of the array.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤10≤ai≤1)  — elements of the array.

It is guaranteed that the sum of nn over all test cases does not exceed 103103.

Output

For each test case, firstly, print kk (n2≤k≤nn2≤k≤n) — number of elements that will remain after removing in the order they appear in aa. Then, print this kk numbers. Note that you should print the numbers themselves, not their indices.

We can show that an answer always exists. If there are several answers, you can output any of them.

Example

input

Copy

4
2
1 0
2
0 0
4
0 1 1 1
4
1 1 0 0

output

Copy

1
0
1
0
2
1 1
4
1 1 0 0

Note

In the first and second cases, alternating sum of the array, obviously, equals 00.

In the third case, alternating sum of the array equals 1−1=01−1=0.

In the fourth case, alternating sum already equals 1−1+0−0=01−1+0−0=0, so we don't have to remove anything.

只有0 或者1,所以就那样做

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[2000005];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,ans=0;
        scanf("%d",&n);
        int num0=0,num1=0;
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
            if(a[i]==0)
                num0++;
            else
                num1++;
        }
        if(num0<num1)
        {
            if(num1%2==1)
                num1--;
            printf("%d\n",num1);
            for(int j=1; j<=num1; j++)
                cout<<"1 ";
            cout<<endl;
        }
        else if(num1<=num0)
        {
            printf("%d\n",num0);
            for(int j=1; j<=num0; j++)
                cout<<"0 ";
            cout<<endl;
        }
 
    }
 
 
    return 0;
 
}

交互推导!!!(J) Chocolate Bunny

https://blog.csdn.net/jziwjxjd/article/details/108488100

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[100005];
int main()
{
    int j=1;
    int n;
    scanf("%d",&n);
    for(int i=2; i<=n; i++)
    {
        int x,y;

        printf("? %d %d\n",i,j);
        fflush(stdout);
        scanf("%d",&x);

        printf("? %d %d\n",j,i);
        fflush(stdout);
        scanf("%d",&y);
        if(x<y)///
        {
            a[j]=y;
            j=i;///无漏
        }
        else
        {
            a[i]=x;
        }
    }
    a[j]=n;
    printf("!");
    for(int i=1;i<=n;i++)
        printf(" %d",a[i]);



    return 0;

}

I. Big Vova

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Alexander is a well-known programmer. Today he decided to finally go out and play football, but with the first hit he left a dent on the new Rolls-Royce of the wealthy businessman Big Vova. Vladimir has recently opened a store on the popular online marketplace "Zmey-Gorynych", and offers Alex a job: if he shows his programming skills by solving a task, he'll work as a cybersecurity specialist. Otherwise, he'll be delivering some doubtful products for the next two years.

You're given nn positive integers a1,a2,…,ana1,a2,…,an. Using each of them exactly at once, you're to make such sequence b1,b2,…,bnb1,b2,…,bn that sequence c1,c2,…,cnc1,c2,…,cn is lexicographically maximal, where ci=GCD(b1,…,bi)ci=GCD(b1,…,bi) - the greatest common divisor of the first ii elements of bb.

Alexander is really afraid of the conditions of this simple task, so he asks you to solve it.

A sequence aa is lexicographically smaller than a sequence bb if and only if one of the following holds:

 

 

 

  • aa is a prefix of bb, but a≠ba≠b;

 

 

  • in the first position where aa and bb differ, the sequence aa has a smaller element than the corresponding element in bb.

 

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1031≤t≤103). Description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤1031≤n≤103)  — the length of the sequence aa.

The second line of each test case contains nn integers a1,…,ana1,…,an (1≤ai≤1031≤ai≤103)  — the sequence aa.

It is guaranteed that the sum of nn over all test cases does not exceed 103103.

Output

For each test case output the answer in a single line  — the desired sequence bb. If there are multiple answers, print any.

Example

input

Copy

7
2
2 5
4
1 8 2 3
3
3 8 9
5
64 25 75 100 50
1
42
6
96 128 88 80 52 7
5
2 4 8 16 17

output

Copy

5 2 
8 2 1 3 
9 3 8 
100 50 25 75 64 
42 
128 96 80 88 52 7 
17 2 4 8 16 

Note

In the first test case of the example, there are only two possible permutations bb  — [2,5][2,5] and [5,2][5,2]: for the first one c=[2,1]c=[2,1], for the second one c=[5,1]c=[5,1].

In the third test case of the example, number 99 should be the first in bb, and GCD(9,3)=3GCD(9,3)=3, GCD(9,8)=1GCD(9,8)=1, so the second number of bb should be 33.

In the seventh test case of the example, first four numbers pairwise have a common divisor (a power of two), but none of them can be the first in the optimal permutation bb.

第一个数肯定是最大值
然后我把这个数输出并扔出去
让剩余的数和这个最大值取gcd
然后根据gcd的值排序
再取最大值
重复进行
有个性质
gcd(a,b,c)=gcd(gcd(a,b),c)

 

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[100005];
int ans[10005];
int vis[100005];
int gcd(int a,int b)
{
    return b?gcd(b,a%b):a;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        ///gcd(a,b,c)=gcd(gcd(a,b),c)

        memset(vis,0,sizeof(vis));
        int n;
        scanf("%d",&n);
        int maxn=0;
        int pos=0;
        for(int i=1; i<=n; i++)
        {
            scanf("%d",a+i);
            if(maxn<a[i])
            {
                maxn=a[i];
                pos=i;
            }
        }
        vis[pos]=1;
        int k=1;
        ans[k++]=maxn;
        for(int i=1;i<n;i++)
        {
            int temp=0;
            pos=0;
            for(int j=1;j<=n;j++)
            {
                if(gcd(maxn,a[j])>temp&&vis[j]==0)
                {
                    temp=gcd(maxn,a[j]);
                    pos=j;
                }
            }

                vis[pos]=1;
                ans[k++]=a[pos];
                maxn=temp;
        }
        for(int i=1;i<k;i++)
            printf("%d ",ans[i]);
            cout<<endl;
    }

    return 0;

}

 

 

E. Directing Edges

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a graph consisting of nn vertices and mm edges. It is not guaranteed that the given graph is connected. Some edges are already directed and you can't change their direction. Other edges are undirected and you have to choose some direction for all these edges.

You have to direct undirected edges in such a way that the resulting graph is directed and acyclic (i.e. the graph with all edges directed and having no directed cycles). Note that you have to direct all undirected edges.

You have to answer tt independent test cases.

Input

The first line of the input contains one integer tt (1≤t≤2⋅1041≤t≤2⋅104) — the number of test cases. Then tt test cases follow.

The first line of the test case contains two integers nn and mm (2≤n≤2⋅1052≤n≤2⋅105, 1≤m≤min(2⋅105,n(n−1)2)1≤m≤min(2⋅105,n(n−1)2)) — the number of vertices and the number of edges in the graph, respectively.

The next mm lines describe edges of the graph. The ii-th edge is described with three integers titi, xixi and yiyi (ti∈[0;1]ti∈[0;1], 1≤xi,yi≤n1≤xi,yi≤n) — the type of the edge (ti=0ti=0 if the edge is undirected and ti=1ti=1 if the edge is directed) and vertices this edge connects (the undirected edge connects vertices xixi and yiyi and directed edge is going from the vertex xixi to the vertex yiyi). It is guaranteed that the graph do not contain self-loops (i.e. edges from the vertex to itself) and multiple edges (i.e. for each pair (xi,yixi,yi) there are no other pairs (xi,yixi,yi) or (yi,xiyi,xi)).

It is guaranteed that both sum nn and sum mm do not exceed 2⋅1052⋅105 (∑n≤2⋅105∑n≤2⋅105; ∑m≤2⋅105∑m≤2⋅105).

Output

For each test case print the answer — "NO" if it is impossible to direct undirected edges in such a way that the resulting graph is directed and acyclic, otherwise print "YES" on the first line and mm lines describing edges of the resulted directed acyclic graph (in any order). Note that you cannot change the direction of the already directed edges. If there are several answers, you can print any.

Example

input

Copy

4
3 1
0 1 3
5 5
0 2 1
1 1 5
1 5 4
0 5 2
1 3 5
4 5
1 1 2
0 4 3
1 3 1
0 2 3
1 2 4
4 5
1 4 1
1 1 3
0 1 2
1 2 4
1 3 2

output

Copy

YES
3 1
YES
2 1
1 5
5 4
2 5
3 5
YES
1 2
3 4
3 1
3 2
2 4
NO

Note

Explanation of the second test case of the example:

Explanation of the third test case of the example:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct node
{
    int u,v,state;
};
node edge[200005];
int in[200005];
vector<int>g[200005];
int xh[200005],kk;
queue<int>q;
int n,m,t;
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        ///初始化
        memset(xh,0,sizeof(xh));
        memset(in,0,sizeof(in));
        kk=0;
        while(!q.empty())
            q.pop();
        int cnt=0;


        scanf("%d %d",&n,&m);
        for(int i=1; i<=n; i++)
            g[i].clear();
        ///存图
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d%d",&edge[i].state,&edge[i].u,&edge[i].v);
            if(edge[i].state)
            {
                int u=edge[i].u;
                int v=edge[i].v;
                g[u].push_back(v);///u->v
                in[v]++;
            }
        }

        for(int i=1; i<=n; i++)
        {
            if(in[i]==0)
            {
                q.push(i);
                xh[i]=++kk;
                for(int j=0; j<g[i].size(); j++)
                {
                    in[g[i][j]]--;
                }
            }
        }
        while(!q.empty())
        {
            int temp=q.front();
            q.pop();
            for(int i=0; i<g[temp].size(); i++)
            {
                if(xh[g[temp][i]]==0&&in[g[temp][i]]==0)
                {
                    xh[g[temp][i]]=++kk;
                    q.push(g[temp][i]);
                    int u=g[temp][i];
                    for(int j=0; j<g[u].size(); j++)
                    {
                        in[g[u][j]]--;
                    }
                }
            }
        }
        if(kk<n)
        {
            printf("NO\n");
            continue;
        }
        printf("YES\n");
        for(int i=1; i<=m; i++)
        {
            if(edge[i].state)
            {
                printf("%d %d\n",edge[i].u,edge[i].v);
            }
            else
            {
                int u=edge[i].u;
                int v=edge[i].v;
                if(xh[u]>xh[v])
                {
                    printf("%d %d\n",v,u);
                }
                else
                {
                    printf("%d %d\n",u,v);
                }
            }
        }


    }

    return 0;

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值