Codeforces Round #560 div3 (C,D)

C

  • 题目大意: 给出一个字符串,可以删除任意位置上的字符,得到一个好字符串. 长度为偶数,且奇数位置i上的字符与\(i+1\)上的字符不相等. 求最小的操作次数
  • 思路: 暴力,遇到奇数位置与后面位置相同直接删除,注意是答案字符串上的奇数位置
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<map>
#define ll long long 
#define FOR(i,n) for(int i =1; i <= n;++i ) 
#define FOR0(i,n) for(int i =0; i < n;++i )  
#define inf 0x3f3f3f3f
using namespace std; 

const int maxn = 2e5+10;
char buf[maxn];
char ans[maxn];
int n;

int main(){
    cin >> n;
    if(n==0){
        cout << 0 << endl << endl;
        exit(0);    //长度为0
    }
    cin >>buf;
    int cur = 0;
    for(int i=0;i<n;++i){
        if(cur%2==0){
            while(buf[i+1]==buf[i]) i++;
        }
        ans[cur++] = buf[i];
    }
    ans[cur] = 0;
    int ccur =cur;// 保证长度为偶数
    ccur= ccur/2*2;
    ans[ccur] = 0;
    cout << n-ccur<<endl<<ans <<endl;
    return 0;
}

D

  • 题目大意: 给出一个序列,判断是否为一个数的几乎全部因数(除去1和该数本身).
    要判断是否满足所有因子,且所有因子都来自于一个数
  • 思路: 暴力,一个数的因子肯定成对出现. 给因子排序 判断是否所有\(a[i]*a[n-i+1]\) 都相等,如果相等在计算因子数量是否满足序列长度,当答案是平方数是要特殊判断一下.
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<map>
#define ll long long 
#define FOR(i,n) for(int i =1; i <= n;++i ) 
#define FOR0(i,n) for(int i =0; i < n;++i )  
#define inf 0x3f3f3f3f
using namespace std; 

const int maxn = 310;
ll a[maxn];
int n;
ll cans;
int judge(){
    if(n==1){
        cans = a[1]*(ll)a[1];
        return 1;
    }
    cans = a[1]*1LL*a[n];
    for(int i=2;i<=n/2;++i){
        if(a[i]*a[n-i+1]!=cans) return 0;
    }
    if(n%2==1 && a[n/2+1]*a[n/2+1]!=cans)   return 0;   //当n为奇数 特判中间数的平方
    return 1;
}
int main(){
    int orz;
    cin >> orz;
    while(orz--){
        cin >> n;
        FOR(i,n){
            cin >> a[i];
        }
        sort(a+1,a+1+n);
        if(judge()!=0){
            int cnt = 0;
            for(ll i=2;i*i<cans;++i){
                if(cans%i==0){
                    cnt++;
//                  cout << i << endl;
                }
            }
            cnt*=2;
            if((ll)sqrt(cans)*(ll)sqrt(cans)==cans) cnt++;
            if(cnt==n)  cout << cans << endl;
            else cout << -1 << endl;
        }else{
            cout << -1 <<endl;
        }       
    }

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/xxrlz/p/10868842.html