zoukankan      html  css  js  c++  java
  • dp 20190617

    A. Alternative Thinking

    这个标的是dp,但是我感觉就只能算思维题,不是特别难,

    你仔细想想就知道,你先求出01这样子满足条件的个数,如果要进行改变,最多只可以增加两个,也可以增加一个或者不增加。

    如果有连续的两个1或者0那么肯定至少可以增加一个,如果有两个不同的00 或者11  或者是两个11连续 或者两个00不连续 或者三个相同的0或者1都是可以增加两个的。

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <queue>
    #include <vector>
    #include <algorithm>
    #define inf 0x3ff3f3f
    using namespace std;
    const int maxn = 1e6 + 10;
    char a[maxn];
    int num[maxn];
    
    int main()
    {
        int n;
        scanf("%d", &n);
        scanf("%s", a + 1);
        int len = strlen(a + 1);
        int ans = 1;
        int f = 1;
        for(int i=1;i<=len;i++)    num[i] = a[i] - '0';
        int flag = num[1];
        for(int i=2;i<=len;i++)    if (num[i] == !flag) flag = num[i], ans++;
        for (int i = 1; i < len; i++) if (num[i] == num[i + 1]) f = 0;
        //printf("%d
    ", ans);
        int ex = 0;
        if(f) printf("%d
    ", ans);
        else
        {
            for(int i=1;i<len;i++)
            {
                if(num[i]==num[i+1])
                {
                    ex++;
                    int x = i;
                    while (num[x] == num[x + 1]) x++;
                    i = x;
                }
            }
            for (int i = 1; i < len - 1; i++) if (num[i] == num[i + 1] && num[i] == num[i + 2]) ex = 2;
            if (ex >= 2) printf("%d
    ", ans + 2);
            else printf("%d
    ", ans + 1);
        }
        return 0;
    
    }
    /*
    56
    10101011010101010101010101010101010101011010101010101010
     */
    A

    A. Pride

    这个题目不是很难,思维题,这个题目有几种情况要考虑,

    第一个是存在1的情况,那么答案就是n-flag,flag表示1的数量。

    第二个就是看他们的所有数gcd是不是等于1 如果不是那么就输出-1

    第三个就是gcd==1 所以但是没有1 这个时候我们就可以找到每一个一段进行gcd==1 把所有的段都保存下来,然后求最小的数值让他gcd==1

    找到之后就再加上n-1即可。

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <iostream>
    #define inf 0x3f3f3f3f
    using namespace std;
    const int maxn = 2e3 + 10;
    queue<int>que;
    int a[maxn];
    
    int gcd(int a,int b)
    {
        return b == 0 ? a : gcd(b, a%b);
    }
    
    int main()
    {
        int n;
        scanf("%d", &n);
        int flag = 0;
        for (int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
            if (a[i] == 1) flag++;
        }
        if(flag)
        {
            printf("%d
    ", n - flag);
            return 0;
        }
        flag = 0;
        int ans = a[1];
        int ax = a[1];
        for(int i=2;i<=n;i++)
        {
            if (gcd(a[i], a[i - 1]) == 1) flag = 1;
            ans = gcd(ans, a[i]);
            ax = gcd(ax, a[i]);
            if(ans==1)
            {
                que.push(i);
                ans = a[i + 1];
            }
        }
        if (flag) printf("%d
    ", n);
        else if (ax != 1) printf("-1
    ");
        else
        {
            int an = inf;
            while(!que.empty())
            {
                int num = 0;
                int u = que.front(); que.pop();
                int ex = a[u]; u--;
                while(ex!=1)
                {
                    ex = gcd(ex, a[u]);
                    u--;
                    num++;
                }
                an = min(an, num);
            }
            printf("%d
    ", n - 1 + an);
        }
        return 0;
    }
    A
  • 相关阅读:
    为什么一看编程技术文章就这么兴奋呢?
    今天弄会了如何使用VB6使用AutoCAD提供的dll操作AutoCAD了
    简单工厂模式没想像中的难啊
    Linq本质论,C#3.0的特性。写的太好了!收藏!
    struct和class的区别
    饿了
    解决Eclipse 3.3+lomboz无法启动的问题
    IE8通过Acid2了
    Linux档案/目录 权限及修改
    Linux 初识man、info
  • 原文地址:https://www.cnblogs.com/EchoZQN/p/11040393.html
Copyright © 2011-2022 走看看