ht birthday party

Topic link: Portal

Problem solving, ideas are written in the code.

Recursive:

#include <iostream>
#include <cstring>
#include <cstdio>
 
using namespace std;
int n, m;
  
// Assume a sequence of 7 people A: 1, 2, 3, 4, 5, 6, 7
// m = 3
 
// There are only 5 people left in the first dequeue:
// 1, 2, 4, 5, 7
// Because the second count starts from the end:
// queue should be B: 7, 5, 4, 2, 1
 
// The BchangeintoA() function is to find the "subscript of the person whose index is i in the array B" in the "subscript of the array A"
// subscripts start at 1
// For example, 7, the subscript in B is 1, and the subscript in A is 7
// eg 2, .............4, ...........2
// alen length of array A
int BchangeintoA (int alen, int i, int blen) {
    i = blen + 1 - i;
    int temp = i + (i-1)/(m-1);
    return temp;
}
 
const int N = 1000100;
int F[N]; // F[N] is not -1, it means if the queue is N, the number of the singer
// Example: F[2] = 1 , if the sequence length is 2, the person number 1 sings
// all subscripts start from 1
 
int Work(int num) {
    // The function f is to solve f[num], that is, the number of the person who sings when the number of people in the queue is num
    if(F[num] != -1) {
        return F[num];
    }
     
    if(num < m){
        F[num] = 1;
        return 1;
    }
 
    Work(num-num/m);
    // Reduce and conquer, for example if it's a queue of 7 people, only 5 people are left after the count
 
    F[num] = BchangeintoA(num, F[num-num/m], num-num/m);
    // num >= m : every F[num] is pushed by F[num-num/m]
     
//    for(int i=1; i<=num; i++) {
//      cout << F[i] << " ";
//  }
//  cout << endl;
     
    return F[num];
}
 
int main(){
    int T;
    cin >> T;
    for(int t=1; t<=T; t++) {
        cin >> n >> m;
 
        memset(F, -1, sizeof(F)); // initialize f
         
        int x;
        for(int i=1; i<=n; i++) {
            scanf("%d",&x);
            printf("%d\n",Work(x));
        }
    }
    return 0;
}

Recursion:

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
 
const int N = 1000100;
int F[N];
int n, m;
  
int BchangeintoA (int alen, int i, int blen) {
    i = blen + 1 - i;
    int temp = i + (i-1)/(m-1);
    return temp;
}
 
int main(){
    int T;
    cin >> T;
    for(int t=1; t<=T; t++) {
        cin >> n >> m;
 
        memset(F, -1, sizeof(F)); // initialize f
        for(int i=1; i<m; i++) {
            F[i] = 1;
        }
         
        int x;
        int max = m-1;
         
        for(int i=1; i<=n; i++) {
            scanf("%d",&x);
            if(x <= max) {
                printf("%d\n",F[x]);
            }
            else {
                for(int i=max+1; i<=x; i++) {
                    F[i] = BchangeintoA(i, F[i-i/m], i-i/m);
                }
                max = x;
                printf("%d\n",F[max]);
            }
//          for(int i=1; i<=max; i++){
//              printf("%d ",F[i]);
//          }
//          printf("\n");
        }
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326076939&siteId=291194637