zoukankan      html  css  js  c++  java
  • CodeForces 382C Arithmetic Progression (排序+分类讨论)

    题意:给出一个长度为n的序列,表示有n张卡片,上面的数字,现在还有一张卡片,上面没有数字,问说可以写几种数字在这张卡片上面,

    使得n+1张卡片上的数字可以排列成一个等差数列,有无限多种时输出-1.

    析:首先排序是肯定的,然后再分成几种,如果只有一个数,那么就一定是-1,如果是两个数时,在前面和后面一定可以加一个,这个也要注意相等的情况,

    然后再考虑中间的情况,如果它们的绝对差是偶数,那么中间也可以再放一个,再就是大于等于3个数时候,这个也要考虑是不是全相等,然后再考虑这个

    序列是不是可以加一个数成一个等差,再考虑前面和后面。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    typedef long long LL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f;
    const LL LNF = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 1e5 + 100;
    const int mod = 1e9 + 7;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline int Min(int a, int b){ return a < b ? a : b; }
    inline int Max(int a, int b){ return a > b ? a : b; }
    inline LL Min(LL a, LL b){ return a < b ? a : b; }
    inline LL Max(LL a, LL b){ return a > b ? a : b; }
    inline int gcd(int a, int b){ return b ? gcd(b, a%b) : a; }
    inline int lcm(int a, int b){ return a * b / gcd(a, b); }
    inline bool is_in(int r, int c){
        return r >= 0 && r < n && c >= 0 && c < m;
    }
    int a[maxn];
    
    int main(){
        while(scanf("%d", &n) == 1){
            for(int i = 0; i < n; ++i)  scanf("%d", a+i);
            sort(a, a+n);
            if(1 == n)  printf("-1
    ");
            else if(n == 2){
                int k = a[1] - a[0];
                if(!k)  printf("1
    %d
    ", a[0]);
                else if(k&1){
                    printf("2
    ");
                    printf("%d %d
    ", a[0]-k, a[1]+k);
                }
                else{
                    printf("3
    ");
                    printf("%d %d %d
    ", a[0]-k, a[0]+k/2, a[1]+k);
                }
            }
            else{
                int cnt = 0, k = INF;
                for(int i = 1; i < n; ++i)
                    k = Min(k, a[i]-a[i-1]);
                bool ok = true, flag = false;
                for(int i = 1; i < n; ++i){
                    if(k == a[i]-a[i-1])  continue;
                    if(flag){ ok = false;  break;  }
                    else if(k * 2 == a[i]-a[i-1]){ flag = true, cnt = a[i-1] + k; }
                    else { ok = false;  break; }
                }
    
                if(!ok)  printf("0
    ");
                else if(!k)  printf("1
    %d
    ", a[0]);
                else if(flag)  printf("1
    %d
    ", cnt);
                else{
                    printf("2
    ");
                    printf("%d %d
    ", a[0]-k, a[n-1]+k);
                }
            }
        }
        return 0;
    }
    
  • 相关阅读:
    hdu 6702 ^&^ 位运算
    hdu 6709 Fishing Master 贪心
    hdu 6704 K-th occurrence 二分 ST表 后缀数组 主席树
    hdu 1423 Greatest Common Increasing Subsequence 最长公共上升子序列 LCIS
    hdu 5909 Tree Cutting FWT
    luogu P1588 丢失的牛 宽搜
    luogu P1003 铺地毯
    luogu P1104 生日
    luogu P1094 纪念品分组
    luogu P1093 奖学金
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/5978851.html
Copyright © 2011-2022 走看看