zoukankan      html  css  js  c++  java
  • P1338 末日的传说,P1372 P1414 又是毕业季——贪心

    一个1到n序列,合理排序逆序对数要求是m,而且字典序要求最小;

    这个题,因为数字只能用一次,所以我们可以知道什么位置放什么数逆序对的个数会增加或减少多少;

    先求出最多能产生的数量,每次先输出最小的数,用总数减去减少的逆序对数;

    如果不够的时候就要用大数排在前面;

    vector,记得输出一个删一个;

    #include<cstdio>
    #include<vector>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    ll n,m;
    ll now;
    vector<ll> v;
    int main()
    {
        scanf("%lld%lld",&n,&m);
        for(ll i=1;i<=n;i++) v.push_back(i);
        ll s=n*(n-1)/2;
        for(int i=n-1;i>=0;i--)
        {
            s-=i;
            if(m>s)
            {
                now=m-s;
                m=s;
            }
            else now=0;
            printf("%lld ",v[now]);
            v.erase(v.begin()+now);
        } 
        return 0;
    }

    又是毕业季我什么也不想说

    1到n这些数中,找到k个,使他们的GCD最大,输出GCD;

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int n,k;
    int main()
    {
        scanf("%d%d",&n,&k);
        printf("%d",n/k);
        return 0;
    }

    又是毕业季II

    这回数字不连续而且k的值是变的;

    把所有的数的公约数的数量记一下就好了

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    const int maxn=1e6+10;
    int n,x;
    int c[maxn];
    int maxx;
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&x);
            maxx=max(maxx,x);
            int m=sqrt(x);
            for(int j=1;j<=m;j++)
            {
                if(x%j==0)
                {
                    c[j]++;
                    if(x/j!=j) c[x/j]++;
                }
            }
        }
        x=maxx;
        for(int i=1;i<=n;i++)
        {
            while(c[x]<i) x--;
            printf("%d
    ",x);
        }
        return 0;
    }
  • 相关阅读:
    HTML5编写规范
    v-if和v-show的区别
    为什么选择MpVue进行小程序的开发
    小程序的前世今生
    MpVue开发之框架的搭建
    MpVue开发之swiper的使用
    (三十二)单例设计模式
    再学习之Spring(面向切面编程).
    多线程编程学习五(线程池的创建)
    再学习之Spring(依赖注入).
  • 原文地址:https://www.cnblogs.com/WHFF521/p/11544733.html
Copyright © 2011-2022 走看看