test20190731 training camp NOIP 16

90 + 0 + 0 = 90. I just picked T2 do.

Grid connection

There is a dot matrix M rows and N columns, may be connected to adjacent points. A longitudinal connection takes one unit, two units takes a lateral connection. There are connections between some points already, and also at least ask how many units it takes to make all the points all connected.

Input format:
The first line of the input two positive integers m and n.
Some of the following four lines each positive integers x1, y1, x2, y2, y2 represents the point of the first column and the second point line x1 x2 y1 column lines have connection. Input ensure | x1-x2 | + | y1 -y2 | = 1.

Output Format:
Output all the communication points that minimum cost is also needed.

Sample input:
2 2
. 1. 1. 1 2

Sample output:
3

Data range:
m, n-<= 1000

Time limit:
1000
Space limitations:
32768

Minimum spanning tree, and do not sorted.

Disorderly letters to

Given n different letters of the disorder (case sensitive, i.e. random in the two letters of the alphabet can be reversed position). Please have a structure of n + 1 such that each letter string of letters to appear in the string.

Input format:
The first line enter a positive integer n.
The following two lines each n letters represent the two adjacent letters needed.

Output Format:
Output a string to meet the requirements.
If the string does not meet the requirements, please output "No Solution".
If there are multiple scheme, the previous output of the ASCII code letter as small as possible (lexicographically smallest) solution

Sample input:
4
aZ
tZ
Xt
aX

Sample output:
XaZtX

Data range:
different letters limited random number, n for the scale can be obtained by calculation.

Time limit:
1000
Space limitations:
32768

answer

Meaning of the title is very clear, apparently seeking the smallest lexicographic Euler scheme, but I forgot Euler thing.

Manual simulation

Found legitimate situation must be odd number of point degree is 0 or 2. If it is 0 then find the smallest, if it is 2 degrees it is odd to find the smallest.

Then intermediate merge two unordered pairs was found, the degree of change is an even number 2, the least one solution. Maintenance can then find the smallest lexicographical use set.

But handed Luo Gu 1341 found the WA. Then look at the data, we found this situation:

5
ax
xx
xt
tp
ps

That is self-loop. Ax when x in preference to go t, but left after less than a x.

So before choosing the lexicographically smaller but also to determine whether a next x must come to their own. This put the other point of even number of sides back to think about it like x, x the number into the back side of at least 3.

There will still be a point of nameless RE, while Xuejun secondary data seems to be the same, so this question is 90 points up.

#include<bits/stdc++.h>
#define co const
#define il inline
template<class T> T read(){
    T x=0,w=1;char c=getchar();
    for(;!isdigit(c);c=getchar())if(c=='-') w=-w;
    for(;isdigit(c);c=getchar()) x=x*10+c-'0';
    return x*w;
}
template<class T>il T read(T&x){
    return x=read<T>();
}
using namespace std;

co int N=52;
il int idx(char c){
    return islower(c)?26+c-'a':c-'A';
}
il char inv(int i){
    return i<26?i+'A':i-26+'a';
}

int n,c[N];
multiset<int> s[N];
multiset<int>::iterator it;
int main(){
    read(n);
    for(int i=1;i<=n;++i){
        char p[3];scanf("%s",p);
        ++c[p[0]=idx(p[0])],++c[p[1]=idx(p[1])];
        s[p[0]].insert(p[1]),s[p[1]].insert(p[0]);
    }
    int cnt=0;
    for(int i=0;i<N;++i)
        if(c[i]&1) ++cnt;
    if(cnt!=0&&cnt!=2) return puts("No Solution"),0;
    char ans[3000];
    if(cnt==0){
        for(int i=0;i<N;++i)
            if(c[i]>=2){ans[0]=i,c[i]-=1;break;}
    }
    else{
        for(int i=0;i<N;++i)
            if(c[i]&1){ans[0]=i,c[i]-=1;break;}
    }
    for(int i=1;i<=n;++i){
        if(s[ans[i-1]].count(ans[i-1])){ // edit 1
            int cnt=0;
            for(int j=0;j<N;++j)if(j!=ans[i-1])
                cnt+=s[j].count(ans[i-1]);
            if(cnt<3){
                ans[i]=ans[i-1],c[ans[i]]-=2;
                s[ans[i]].erase(s[ans[i]].find(ans[i])),s[ans[i]].erase(s[ans[i]].find(ans[i]));;
                continue;
            }
        }
        int cnt=0;
        it=s[ans[i-1]].begin();
        if(i<n&&c[*it]==1) ++it;
        ans[i]=*it,c[ans[i]]-=2; // i==n doesn't matter
        s[ans[i-1]].erase(s[ans[i-1]].find(ans[i])),s[ans[i]].erase(s[ans[i]].find(ans[i-1]));
    }
    for(int i=0;i<=n;++i) ans[i]=inv(ans[i]);
    ans[n+1]=0;
    puts(ans);
    return 0;
}
/*
5
ax
xx
xt
tp
ps
*/

Euler

Euler algorithm can solve this problem directly.

Euler was back in time to answer the path added to the list. So if we can come back after the x, go first is a smaller dictionary order; if not, go first is x.

Feel deeply understand a lot. Time complexity \ (O (n-\ log n-) \) .

Digital conversion

If the divisor and a number x (not including itself, hereinafter) is smaller than its own, then x can become its divisors; if for a certain y> x and divisor y is x, then x can become y. For example, 4 may be changed to become 3,1 7. All digital conversion defined in no more than n a positive integer range, constantly seeking digital conversion without converting the number of steps is repeated up to the number that appears.

Input format:
enter a positive integer n.

Output formats:
Output requires a minimum of time spent.

Sample input:
7

Sample output:
3

Data range:
n-<= 50 000.

Time limit:
1000
Space limitations:
32768

answer

Only to smaller even undirected edges, so in the end must be drawing forest.

The diameter of the tree and then run it.

Linear sieve can be used to practice seeking divisors (error).

The second linear sieve heavy cycle to be out of mesh number \ (x = i \ times p_j \) minimum prime factors must now be enumerated \ (p_j \) , this case is divided into two cases:

  • \ (I \) no prime factors \ (p_j \) , then can be calculated directly.
  • \ (i \) a qualitative factor \ (p_j \) , you should modify \ (p_j \) contribution. It is necessary to record the number of contributions and about the minimum number of each of its prime factors.

Then time complexity \ (O (n-) \) .

Guess you like

Origin www.cnblogs.com/autoint/p/11275585.html