2020牛客暑期多校训练营(第五场) D、E

Drop Voicing

2020牛客暑期多校训练营(第五场) D、E_第1张图片

解法:

观察可发现:

规律1:连续多次使用操作1,相当于将前n-1个数不断内循环调换

规律2:连续多次使用操作2,相当于将n个数不断内循环调换

规律3:多次使用操作1后,再使用操作2,将a[0]放去末尾 ,相当于,取前n-1中的任一个数放到a[n-1]

规律3可得:通过这两种操作的组合,可以将任意一个数放到任意一个位置

那么题目可转化为求:通过最少的组合操作调换,使得目标序列sorted

枚举n个最长上升子序列的长度取max

答案就是总长度减去max   ans=n-max

#include
using namespace std;
int n;
int a[505];

int gao(int i)
{
	vector v;
	for(int j=0;j

Bogo Sort

题意:给定置换规则p数组,求最多有多少不同的序列可通过多次置换变成序列

解法:在置换规则中一定存在多个置换环,即环内数的位置只可能在该环内循环变化,置换周期为环内元素个数

那么答案即为所有置换环大小的lcm

我是用的java大数类BigInteger


import java.io.IOException;
import java.math.BigInteger;
import java.util.Scanner;


public class Main {
    static int maxn = 200010;
    static int[] a=new int[maxn];
    static int[] vis=new int[maxn];
    static int[] to=new int[maxn]; 
    static BigInteger[] b = new BigInteger[maxn];
    static int tot=0;
    static BigInteger num=BigInteger.ONE;

    public static void main(String[] args) throws IOException {
        int n;
        Scanner in;
        Scanner sc = new Scanner(System.in);
         
        n=sc.nextInt();
        for(int i=0;i

C++ 大数模板:

struct bign{ 
    int d[maxn], len; 
   
    void clean() { while(len > 1 && !d[len-1]) len--; } 
   
    bign()          { memset(d, 0, sizeof(d)); len = 1; } 
    bign(int num)   { *this = num; }  
    bign(char* num) { *this = num; } 
    bign operator = (const char* num){ 
        memset(d, 0, sizeof(d)); len = strlen(num); 
        for(int i = 0; i < len; i++) d[i] = num[len-1-i] - '0'; 
        clean(); 
        return *this; 
    } 
    bign operator = (int num){ 
        char s[20]; sprintf(s, "%d", num); 
        *this = s; 
        return *this; 
    } 
   
    bign operator + (const bign& b){ 
        bign c = *this; int i; 
        for (i = 0; i < b.len; i++){ 
            c.d[i] += b.d[i]; 
            if (c.d[i] > 9) c.d[i]%=10, c.d[i+1]++; 
        } 
        while (c.d[i] > 9) c.d[i++]%=10, c.d[i]++; 
        c.len = max(len, b.len); 
        if (c.d[i] && c.len <= i) c.len = i+1; 
        return c; 
    } 
    bign operator - (const bign& b){ 
        bign c = *this; int i; 
        for (i = 0; i < b.len; i++){ 
            c.d[i] -= b.d[i]; 
            if (c.d[i] < 0) c.d[i]+=10, c.d[i+1]--; 
        } 
        while (c.d[i] < 0) c.d[i++]+=10, c.d[i]--; 
        c.clean(); 
        return c; 
    } 
    bign operator * (const bign& b)const{ 
        int i, j; bign c; c.len = len + b.len;  
        for(j = 0; j < b.len; j++) for(i = 0; i < len; i++)  
            c.d[i+j] += d[i] * b.d[j]; 
        for(i = 0; i < c.len-1; i++) 
            c.d[i+1] += c.d[i]/10, c.d[i] %= 10; 
        c.clean(); 
        return c; 
    } 
    bign operator / (const bign& b){ 
        int i, j; 
        bign c = *this, a = 0; 
        for (i = len - 1; i >= 0; i--) 
        { 
            a = a*10 + d[i]; 
            for (j = 0; j < 10; j++) if (a < b*(j+1)) break; 
            c.d[i] = j; 
            a = a - b*j; 
        } 
        c.clean(); 
        return c; 
    } 
    bign operator % (const bign& b){ 
        int i, j; 
        bign a = 0; 
        for (i = len - 1; i >= 0; i--) 
        { 
            a = a*10 + d[i]; 
            for (j = 0; j < 10; j++) if (a < b*(j+1)) break; 
            a = a - b*j; 
        } 
        return a; 
    } 
    bign operator += (const bign& b){ 
        *this = *this + b; 
        return *this; 
    } 
   
    bool operator <(const bign& b) const{ 
        if(len != b.len) return len < b.len; 
        for(int i = len-1; i >= 0; i--) 
            if(d[i] != b.d[i]) return d[i] < b.d[i]; 
        return false; 
    } 
    bool operator >(const bign& b) const{return b < *this;} 
    bool operator<=(const bign& b) const{return !(b < *this);} 
    bool operator>=(const bign& b) const{return !(*this < b);} 
    bool operator!=(const bign& b) const{return b < *this || *this < b;} 
    bool operator==(const bign& b) const{return !(b < *this) && !(b > *this);} 
   
    string str() const{ 
        char s[maxn]={}; 
        for(int i = 0; i < len; i++) s[len-1-i] = d[i]+'0'; 
        return s; 
    } 
}; 
   
istream& operator >> (istream& in, bign& x) 
{ 
    string s; 
    in >> s; 
    x = s.c_str(); 
    return in; 
} 
   
ostream& operator << (ostream& out, const bign& x) 
{ 
    out << x.str(); 
    return out; 
}

 

你可能感兴趣的:(2020牛客暑期多校训练营(第五场) D、E)