7.7 Happy Zero [hao][kun][nan]

Posted by tromton on Sat, 15 Jun 2019 20:45:17 +0200

hao

[Problem Description]
Zuma was a once popular game in which several colored beads were initially arranged in a single track, and any three adjacent beads were not exactly the same color.From then on, you can launch beads into the orbit and join the original sequence.Once three or more beads of the same color become adjacent, they disappear immediately.This kind of elimination may occur in chains in which you will not be able to emit beads for the time being.
Developers are now ready to write a playback tool for players to play a game.They have completed the process recording function in the game, and the playback function is delegated to you.
The record of the game begins with the initial sequence of beads on the track, followed by a series of actions that the player will do next.Your task is to calculate the new bead sequence in time after each operation.
[Input Format]
The first line is a string of uppercase letters''A'~'Z', representing the initial sequence of beads on the track, with different letters representing different colors.
The second line is a number indicating that the entire playback process has one operation.
The next rows correspond to each operation in turn.Each operation is described by a number and an uppercase letter, separated by a space.Among them, is the color of the new beads.If there is a common bead before insertion, it indicates the order of the new bead on the track after insertion (before elimination occurs).
[Output Format]
Output coexists, giving the track after each operation (and possible elimination that may occur immediately)
NOIP Analog Question hao
Page 3 6
Bead sequence.
If there are no beads on the track, they are denoted as'-'.
[Sample input]
ACCBA
5
1 B
0 A
2 B
4 C
0 A
[Sample Output]
ABCCBA
AABCCBAA
ABBCCBA
-
A

Solution: Simulate, join, eliminate, join, eliminate...

#include <cstdio>  
#include <cstring>  
using namespace std;
#define N 20000 + 7
char ch[N], temp[N];  
int siz = 0, pos;  
bool check( int ptn) {  
    int l = ptn, r = ptn;
    char Ipt;
    Ipt = ch[ ptn ];
    while(ch[l] == Ipt && l) l--;
    if ( l || ch[l] != Ipt ) l++;
    while(ch[r] == Ipt && r < siz) r++;
    if ( r - l > 2 ){
        strcpy( temp, ch+r ), strcpy( ch + l, temp);
        siz = siz + l - r, pos = l;
        return true;
    }
    else
    return false;

}  
int main()  {  
    siz = 0;
    int n;  
    gets(ch);  
    while(ch[siz] >= 'A' && ch[siz] <= 'Z') siz++;  
    scanf("%d", &n);  
    while ( n-- ) {  
        char e;
        scanf("%d %c",&pos,&e);  
        strcpy( temp, ch + pos), strcpy(ch + pos + 1, temp);  
        ch[pos] = e, siz++;  
        while( check(pos) && siz);  
        if(siz)  puts(ch);  
        else  puts("-");  
    }  
    return 0;  
}  

kun

The stack is a powerful data structure, and one of its special functions is to sort arrays.For example, with the help of a stack, arrays 1,3,2 are ordered on or off the stack in order, which can be sorted from large to small: 1 on the stack, 3 on the stack, 3 on the stack, 2 on the stack, 2 on the stack, 1 on the stack.In the above example, the stack out sequence is 3,2,1, thus sorting the arrays.Unfortunately, sometimes, with the help of only one stack, the complete ordering of arrays cannot be achieved.For example, given an array of 2,1,3, with the help of a stack, the largest stack sequence of dictionary order that can be obtained is 3,1,2:2 stacking; 1 stacking; 3 stacking; 3 stacking; 1 stacking; 2 stacking.With the help of a stack, sort a given array from large to small in the stacking order.When full sorting is not possible, output the stack sequence with the largest dictionary order.
[Input Format]
Enter coexistence.
The first line contains an integer representing the length of the stacking sequence.
The second line contains an integer representing the stacking sequence.The input data guarantees that the given sequence is in full order to n, that is, there will be no duplicate numbers.
[Output Format]
Only one line, a total of integers, represents the stack sequence you calculated.
[Sample input]
3
2 1 3
[Sample Output]
3 1 2
[Data size and conventions]
For data,.
For data,.

Problem: Similar to car scheduling, it keeps lowering the maximum value and then stores it on a stack.

#include <stack>
#include <cstdio>
#include <iostream>
using namespace std;
#define N 1000000 + 7
stack<int> s;
int a[N], vis[N] = {0};
int main(){
    int n;
    scanf( "%d", &n);
    for ( int i = 1; i <= n; i++) scanf( "%d", &a[i]);
    for ( register int i = 1, mx = n; i <= n; i++){
        s.push( a[i] ), vis[ a[i] ] = 1;
        if ( a[i] == mx ) {
            s.pop(), printf( "%d ", a[i]);
            for( ; vis[mx]; ) mx--;
        }
        for ( ;!s.empty() && s.top() > a[i+1] && s.top() > mx; ) printf( "%d ", s.top() ), s.pop();
    }
    return 0;
}

nan

[Problem Description]
We have a sequence and now he has three numbers in it.Let's start with the third number:
1. The third number is, so let's write one after the sequence and change it to.
2. The fourth number is, so let's write one after the sequence and change it to.
So you can see that this sequence should be.
If we set where a number ends up, now I want to know how much it equals.
[Input Format]
The first row is an integer representing the number of data groups.
Next line is an integer per line.
[Output Format]
Rows, one integer per line, representing the value.
[Sample input]
3
3
10
100000
[Sample Output]
11
217
507231491

Solution: Block and find the law

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define LL long long
using namespace std;
#define mod  1000000007
#define N 1400007
#define Name "nan"
int a[N];
LL sum[N],Ret[N];
int main(){
    freopen(Name".in","r",stdin);
    freopen(Name".out","w",stdout);
    a[1] = 1, a[2] = a[3] = 2;
    LL last = 3;
    int cur = 3;
    for ( ; last+a[cur] <= N; ){
        for(register int i = last + 1 ; i <= last + a[cur]; i++) a[i] = cur;
        last += a[cur], cur++;
    }
    for(register int i = 1; i <= N; i++ ) sum[i] = sum[i-1] + a[i];
    last = 0;
    for (register int i = 1 ; i <= N; i++){
        LL l = last + 1, r = last + a[i];
        Ret[i] = ( ( (LL)a[i] * (LL)( l+r ) / 2 ) % mod * i % mod + Ret[i-1] )% mod;
        last = r;
    }
    int T;
    scanf("%d",&T);
    while( T-- ){
        int n;
        scanf( "%d", &n);
        int pti = upper_bound( sum+1, sum+N+1, n ) - sum - 1;
        LL ans = Ret[pti];
        if( sum[pti] < n ){
            LL len = n-sum[pti], l = sum[pti] + 1, r = sum[pti] + len;
            pti++;
            ans += ( (LL)len * (LL) ( l+r )/2) % mod * pti % mod;
            ans %= mod;
        }
        cout << ans << "\n";
    }
    return 0;
}