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\n",x);
    }
    return 0;
}

 

posted @ 2019-09-18 19:47  AiRomance  阅读(149)  评论(0编辑  收藏  举报