寒假训练20200114(补题)

A CodeForces 1255C League of Leesins

Problem Description

Bob is an avid fan of the video game “League of Leesins”, and today he celebrates as the League of Leesins World Championship comes to an end!
The tournament consisted of n (n≥5) teams around the world. Before the tournament starts, Bob has made a prediction of the rankings of each team, from 1-st to n-th. After the final, he compared the prediction with the actual result and found out that the i-th team according to his prediction ended up at the pi-th position (1≤pi≤n, all pi are unique). In other words, p is a permutation of 1,2,…,n.
As Bob’s favorite League player is the famous “3ga”, he decided to write down every 3 consecutive elements of the permutation p. Formally, Bob created an array q of n−2 triples, where qi=(pi,pi+1,pi+2) for each 1≤i≤n−2. Bob was very proud of his array, so he showed it to his friend Alice.
After learning of Bob’s array, Alice declared that she could retrieve the permutation p even if Bob rearranges the elements of q and the elements within each triple. Of course, Bob did not believe in such magic, so he did just the same as above to see Alice’s respond.
For example, if n=5 and p=[1,4,2,3,5], then the original array q will be [(1,4,2),(4,2,3),(2,3,5)]. Bob can then rearrange the numbers within each triple and the positions of the triples to get [(4,3,2),(2,3,5),(4,1,2)]. Note that [(1,4,2),(4,2,2),(3,3,5)] is not a valid rearrangement of q, as Bob is not allowed to swap numbers belong to different triples.
As Alice’s friend, you know for sure that Alice was just trying to show off, so you decided to save her some face by giving her any permutation p that is consistent with the array q she was given.

Input

The first line contains a single integer n (5≤n≤105) — the size of permutation p.
The i-th of the next n−2 lines contains 3 integers qi,1, qi,2, qi,3 (1≤qi,j≤n) — the elements of the i-th triple of the rearranged (shuffled) array qi, in random order. Remember, that the numbers within each triple can be rearranged and also the positions of the triples can be rearranged.
It is guaranteed that there is at least one permutation p that is consistent with the input.

Output

Print n distinct integers p1,p2,…,pn (1≤pi≤n) such that p is consistent with array q.
If there are multiple answers, print any.

Example

input
5
4 3 2
2 3 5
4 1 2
output
1 4 2 3 5


题意

有一个n个数的序列,可以拆成n-2个三元组。
例如:p=[1,4,2,3,5],那么三元组为[1,4,2],[4,2,3],[2,3,5]。
现给定打乱三元组之间顺序和三元组之内顺序的三元组,求原序列。

思路

思想:构造 暴力
观察三元组我们可以发现,序列首和序列尾的两个元素在所有三元组内只出现了一次,序列首第二个和序列尾第二个元素在所有三元组内只出现了两次,其他所有的元素在所有的三元组内都出现了三次。
所以我们找到任意一个只出现了一次的元素,那么这个元素则为序列的首元素,然后在这个元素所在的三元组内的只出现了两次的元素即为第二个元素,同时,我们也可以确定这个序列的第三个元素。
这样我们就可以确定整个序列的所有元素了。

坑点

题中给了n>=5,没注意到,所以就没想到首第二个和尾第二个元素在所有三元组内只出现了两次。


Code

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VII;
typedef vector<VII> VIII;
typedef pair<int, int> PII;

#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)

const int N = 1e5+5;

vector<PII> G[N];
VI Ans;
int vis[N];

int main()
{
    int n;
    scanf("%d", &n);
    rep(i,1,n-2)
    {
        int x, y, z;
        scanf("%d%d%d", &x, &y, &z);
        G[x].push_back(make_pair(y, z));
        G[y].push_back(make_pair(x, z));
        G[z].push_back(make_pair(x, y));
    }
    int fi, se;
    rep(i,1,n)
    {
        if(G[i].size() == 1)
        {
            Ans.push_back(i);
            vis[i] = 1;
            fi = G[i][0].first;
            vis[fi] = 1;
            se = G[i][0].second;
            vis[se] = 1;
            if(G[fi].size() != 2)
                swap(fi, se);
            Ans.push_back(fi);
            Ans.push_back(se);
            break;
        }
    }
    while(G[se].size() != 1)
    {
        int sz = (int)G[fi].size();
        rep(i,0,sz-1)
        {
            if(se == G[fi][i].first && vis[G[fi][i].second] == 0)
            {
                int tmp = fi;
                fi = se;
                se = G[tmp][i].second;
                Ans.push_back(se);
                vis[se] = 1;
                break;
            }
            if(se == G[fi][i].second && vis[G[fi][i].first] == 0)
            {
                int tmp = fi;
                fi = se;
                se = G[tmp][i].first;
                Ans.push_back(se);
                vis[se] = 1;
                break;
            }
        }
    }
    int szz = Ans.size();
    rep(i,0,szz-1)
    {
        i == 0 || printf(" ");
        printf("%d", Ans[i]);
    }
    printf("\n");
    return 0;
}


D HDU 1255

(还没补上)



F CodeForces 1280A Cut and Paste

Problem Description

We start with a string s consisting only of the digits 1, 2, or 3. The length of s is denoted by |s|. For each i from 1 to |s|, the i-th character of s is denoted by si.
There is one cursor. The cursor’s location ℓ is denoted by an integer in {0,…,|s|}, with the following meaning:
If ℓ=0, then the cursor is located before the first character of s.
If ℓ=|s|, then the cursor is located right after the last character of s.
If 0<ℓ<|s|, then the cursor is located between sℓ and sℓ+1.
We denote by sleft the string to the left of the cursor and sright the string to the right of the cursor.
We also have a string c, which we call our clipboard, which starts out as empty. There are three types of actions:
The Move action. Move the cursor one step to the right. This increments ℓ once.
The Cut action. Set c←sright, then set s←sleft.
The Paste action. Append the value of c to the end of the string s. Note that this doesn’t modify c.
The cursor initially starts at ℓ=0. Then, we perform the following procedure:
Perform the Move action once.
Perform the Cut action once.
Perform the Paste action sℓ times.
If ℓ=x, stop. Otherwise, return to step 1.
You’re given the initial string s and the integer x. What is the length of s when the procedure stops? Since this value may be very large, only find it modulo 109+7.
It is guaranteed that ℓ≤|s| at any time.

Input

The first line of input contains a single integer t (1≤t≤1000) denoting the number of test cases. The next lines contain descriptions of the test cases.
The first line of each test case contains a single integer x (1≤x≤106). The second line of each test case consists of the initial string s (1≤|s|≤500). It is guaranteed, that s consists of the characters “1”, “2”, “3”.
It is guaranteed that the sum of x in a single file is at most 10e6. It is guaranteed that in each test case before the procedure will stop it will be true that ℓ≤|s| at any time.

Output

For each test case, output a single line containing a single integer denoting the answer for that test case modulo 10e9+7.

Example

input
4
5
231
7
2323
6
333
24
133321333
output
25
1438
1101
686531475

Note

Let’s illustrate what happens with the first test case. Initially, we have s= 231. Initially, ℓ=0 and c=ε (the empty string). The following things happen if we follow the procedure above:
Step 1, Move once: we get ℓ=1.
Step 2, Cut once: we get s= 2 and c= 31.
Step 3, Paste sℓ= 2 times: we get s= 23131.
Step 4: ℓ=1≠x=5, so we return to step 1.
Step 1, Move once: we get ℓ=2.
Step 2, Cut once: we get s= 23 and c= 131.
Step 3, Paste sℓ= 3 times: we get s= 23131131131.
Step 4: ℓ=2≠x=5, so we return to step 1.
Step 1, Move once: we get ℓ=3.
Step 2, Cut once: we get s= 231 and c= 31131131.
Step 3, Paste sℓ= 1 time: we get s= 23131131131.
Step 4: ℓ=3≠x=5, so we return to step 1.
Step 1, Move once: we get ℓ=4.
Step 2, Cut once: we get s= 2313 and c= 1131131.
Step 3, Paste sℓ= 3 times: we get s= 2313113113111311311131131.
Step 4: ℓ=4≠x=5, so we return to step 1.
Step 1, Move once: we get ℓ=5.
Step 2, Cut once: we get s= 23131 and c= 13113111311311131131.
Step 3, Paste sℓ= 1 times: we get s= 2313113113111311311131131.
Step 4: ℓ=5=x, so we stop.
At the end of the procedure, s has length 25.


题意

给定一个由1、2、3组成的串。有一个游标ℓ,游标位于两个字符之间,ℓ从0开始。
有一系列操作:
1、游标ℓ右移1
2、游标左侧的为c,右侧为s
3、把sℓ倍的s添加在c的右侧,c为新串
4、如果ℓ = x,结束。否则,回到1
给定原串s和结束位置x,问最后的字符串的长度,模1e9+7。

思路

思想:暴力模拟
题意中保证x之和不超过1e6,所以我们可以直接暴力求出前x位的串,并记录字符串长度。


Code

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VII;
typedef vector<VII> VIII;
typedef pair<int, int> PII;

#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)

const int N = 1e6 + 5;
const ll MOD = 1e9 + 7;

char s[N];

int main()
{
    int T;
    scanf("%d", &T);
    while(T --)
    {
        int n;
        scanf("%d", &n);
        scanf("%s", s+1);
        int len = strlen(s+1);
        ll ans = len % MOD;
        rep(i,1,n)
        {
            if(s[i] == '1')
            {
                continue;
            }
            else if(s[i] == '2')
            {
                rep(j,len+1,min(2*len-i, n))
                    s[j] = s[j-(len-i)];
                ans = (ans + (ans - i + MOD) % MOD) % MOD;
                len = min(2*len-i, n);
            }
            else if(s[i] == '3')
            {
                rep(j,len+1,min(2*len-i, n))
                    s[j] = s[j-(len-i)];
                rep(j,2*len-i+1,min(3*len-2*i, n))
                    s[j] = s[j-(len-i)];
                ans = (ans + (ans - i + MOD) % MOD * 2 % MOD) % MOD;
                len = min(3*len-2*i, n);
            }
        }
        printf("%lld\n", ans);
    }
    return 0;
}
发布了42 篇原创文章 · 获赞 6 · 访问量 4017

猜你喜欢

转载自blog.csdn.net/qq_43383246/article/details/103996704
今日推荐