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;
}

原文地址:https://www.cnblogs.com/xxrlz/p/10868842.html

时间: 2024-08-30 14:25:39

Codeforces Round #560 div3 (C,D)的相关文章

【赛时总结】◇赛时&#183;V◇ Codeforces Round #486 Div3

◇赛时·V◇ Codeforces Round #486 Div3 又是一场历史悠久的比赛,老师拉着我回来考古了--为了不抢了后面一些同学的排名,我没有做A题 ◆ 题目&解析 [B题]Substrings Sort +传送门+   [暴力模拟] 题意 给出n个字符串,你需要将它们排序,使得对于每一个字符串,它前面的字符串都是它的子串(对于字符串i,则字符串 1~i-1 都是它的子串). 解析 由于n最大才100,所以 O(n3) 的算法都不会爆,很容易想到暴力模拟. 如果字符串i是字符串j的子串

Codeforces Round #605(Div3)A~E

Codeforces Round #605(Div3)A~E A. Three Friends 题意: 给三个数\(a,b,c\),对每一个数字可以进行一次操作,加一减一或者不变. 求三个数两两组合的差值绝对值的最小值. 思路: 先排个序. 假设排序后三个数从小到大是\(a,b,c\). 那么他们的两两差就是\((c-b)+(c-a)+(b-a)=2c-2a\). 为了让他们的\(2c-2a\)最小,那就缩小\(c\),增大\(a\). #include<bits/stdc++.h> usin

CodeForces Round #527 (Div3) C. Prefixes and Suffixes

http://codeforces.com/contest/1092/problem/C Ivan wants to play a game with you. He picked some string ss of length nn consisting only of lowercase Latin letters. You don't know this string. Ivan has informed you about all its improper prefixes and s

Codeforces Round #560 (Div. 3) B题

题目网址:http://codeforces.com/contest/1165/problem/B 题目大意:给出n个数,问有多少个数满足连续大于等于自然数集中的数,即以此大于等于1,2,3…… 题解:直接sort一下,然后从小到大和1,2,3……比较 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=2e5+7; 4 int a[maxn]; 5 int main() 6 { 7 int n,tot=1,ans

Codeforces Round #560 (Div. 3) A题

题目网址:http://codeforces.com/contest/1165/problem/A 题目大意:给定一行01串,开头必是1,length为n,可对串进行变化,0变成1,1变成0,问经过最少的变化,该串mod 10^x==10^y.(^是乘方),输出变化次数. 题解:简单分析可知,比如串是1001000,则mod 1000 == 0,mod 10000 == 1000,所以直接在n-x之和判断即可. 1 #include<bits/stdc++.h> 2 #define ll lo

CodeForces Round #498 div3

A: 题目没读, 啥也不会的室友帮我写的. 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout); 4 #define LL long long 5 #define ULL unsigned LL 6 #define f

D. Circular Dance codeforces round#529(div3)

这道题类似与经典题目素数和环,每次找到后两个数字a,b,如果a没被访问过且a是b的父节点(a记得b),就继续把a当下一个节点搜,否则把b当下一个节点搜 值得注意的是当搜到只剩下一个节点的时候,要选取那个没有访问过的节点做最后一个节点,因此需要标记数组 #include<bits/stdc++.h> using namespace std; int a[200010],b[200010]; bool vis[200010]; int n; void dfs(int step,int num) {

Codeforces Round #555 div3 C2

题目大意 给出一个序列,可以从左或从右侧取数,要求取出的序列严格上升 思路 贪心取左右最小的,如果相等则之后只能从一侧取,直接选能取最长的一侧 Code: #include<bits/stdc++.h> #define ll long long #define inf 0x3f3f3f3f using namespace std; int ma[10]; int n; int a[(int)(2*1e5)+10]; int main(){ scanf("%d",&n

Codeforces Round #560 Div. 3

题目链接:戳我 于是...风浔凌弱菜又去写了一场div.3 总的来说,真的是比较简单.......就是.......不开long long见祖宗 贴上题解-- A 给定一个数,为01串,每次可以翻转一个位置上的数,问最少几步可以使得它模\(10^x\)余\(10^y\) 从后往前贪心即可. #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<c