Codeforces Round #494 (Div. 3) (A+B+C+D+E)

A题 题目链接(签到)

题意:把一个数列,分成几个的集合,要求每个集合无相同元素,问最少要分几个集合。 (1<=n<=100 1<=ai<=100)

思路: 直接找出出现次数最多的即可。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<math.h>
#include<set>
using namespace std;
#define LL long long
#define ULL unsigned long long
const int INF=30000000;
const double eps=1e-5;
const int maxn=1e5+50;
int book[105];
int main()
{
//    ios::sync_with_stdio(false);
//    cin.tie(0);
//    cout.tie(0);
    int n;
    int mmax=-1;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int t;
        scanf("%d",&t);
        book[t]++;
        mmax=max(mmax,book[t]);
    }
    cout<<mmax;
 //   system("pause");
    return 0;
}

B题 题目链接 (字符串构造)

题意:给你a个0,b个1,给定整数x,让你构造一个字符串,满足,s[i]!=s[i+1]的i共有x处 。 比如00100 中,s[i]!=s[i+1]的i共有2处 :2和3 (i从1开始计数)
保证字符串一定可以构造出来。

思路:题意转化成把a个0 b个1分成x+1组,每组只有0或1。
举例,假设x为6,x+1为7,我们有5个0,3个1,0的数目更多,先放0
所以我们要4组0 3组1,让每组交叉排列,最终间隔数就是x。

分成4组0,每组1个,有一个0多,就放在第一组,如果1分完组也有多余,也放给第一组。
所以就是00 1 0 1 0 1 0 一共7组,6个间隔,x就是6

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<math.h>
#include<set>
using namespace std;
#define LL long long
#define ULL unsigned long long
const int INF=30000000;
const double eps=1e-5;
const int maxn=1e5+50;
int main()
{
//    ios::sync_with_stdio(false);
//    cin.tie(0);
//    cout.tie(0);
    int a,b,x;
    scanf("%d%d%d",&a,&b,&x);
    x++;//分成x组
    int mmin=x/2;
    int mmax=x-mmin;
    if(a>=b)//0多 先放零
    {
        int div_a=a/mmax,div_b=b/mmin;//每组的数目
        int left_a=a-div_a*mmax,left_b=b-div_b*mmin;//剩余量
        for(int i=1;i<=left_a+div_a;i++) printf("0");//第一组把剩余量一起放进去
        x--;//放完第一组0
        for(int i=1;i<=left_b+div_b;i++) printf("1");//第一组把剩余量一起放进去
        x--;//放完第一组1
        //上面是把多余的1 0放在开头两组
        for(int i=1;i<=x;i++)
        {
            if(i&1)//奇数放0
            {
                for(int j=1;j<=div_a;j++) printf("0");
            }
            else
            {
                for(int j=1;j<=div_b;j++) printf("1");
            }
        }
    }
    else //1多 先放1  后面和先放0同理
    {
        int div_b=b/mmax,div_a=a/mmin;
        int left_a=a-div_a*mmin,left_b=b-div_b*mmax;
        for(int i=1;i<=left_b+div_b;i++) printf("1");
        x--;
        for(int i=1;i<=left_a+div_a;i++) printf("0");
        x--;
        //上面是把多余的1 0放在开头两组
       for(int i=1;i<=x;i++)
        {
            if(i&1)
            {
                for(int j=1;j<=div_b;j++) printf("1");
            }
            else
            {
                for(int j=1;j<=div_a;j++) printf("0");
            }
        }
    }
 //   system("pause");
    return 0;
}

C题 题目链接(前缀和+暴力枚举)

题意:给一个数列,给定整数k,求所有长度不小于k的区间的平均数中,的最大值

思路:前缀和,然后暴力枚举每个区间

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<math.h>
#include<set>
using namespace std;
#define LL long long
#define ULL unsigned long long
const int INF=30000000;
const double eps=1e-5;
const int maxn=5e3+50;
int psum[maxn];
int main()
{
//    ios::sync_with_stdio(false);
//    cin.tie(0);
//    cout.tie(0);
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++)
    {
        int t;
        scanf("%d",&t);
        psum[i]=psum[i-1]+t;
    }
    double ans=-1;
    for(int i=k;i<=n;i++)//枚举区间长度
    {
        for(int j=1;j<=n-i+1;j++)//枚举左端点
        {
            int r=i+j-1;//右端点
            double temp=1.0*(psum[r]-psum[j-1])/i;
            ans=max(ans,temp);
        }
    }
    printf("%.15f",ans);
 //   system("pause");
    return 0;
}

D题 题目链接 (贪心+思维)

题意:给定n个硬币 ,面值都是2的n次幂,给出q个询问,每次输入一个值v,问最少取多少硬币面值相加为v ,不能得到v则输出-1。
(1<=n,q<=2e5) (面额<=2e9 v<=1e9)

思路:由于这题硬币都是2的n次幂,所以对于任何一个v,我们直接从大到小找出面额小于等于v的硬币,能取几枚就取几枚。 因为是2的n次幂,不存在必须跳过某个可以取的数才能得到解的情况.

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<math.h>
#include<set>
#include<unordered_map>
using namespace std;
#define LL long long
#define ULL unsigned long long
const int INF=30000000;
const double eps=1e-5;
const int maxn=5e3+50;
//给定n个硬币 面值都是2的指数,给出多个询问,每次输入一个值v,问最少取多少硬币相加为v 不能得到v则输出-1
int n,q;
unordered_map<int,int> mp;
int main()
{
//    ios::sync_with_stdio(false);
//    cin.tie(0);
//    cout.tie(0);
    scanf("%d%d",&n,&q);
    int mmax=0;
    for(int i=1;i<=n;i++)
    {
        int t;
        scanf("%d",&t);
        mmax=max(mmax,t);
        mp[t]++;
    }
    while(q--)
    {
        int x;
        int ans=0;
        scanf("%d",&x);
        for(int i=mmax;i>=1;i>>=1)//i要大于等于1
        {
            if(mp[i])
            {
                int times=min(mp[i],x/i);//不能取的话 times就是0
                ans+=times;
                x-=times*i;
            }
        }
        if(x) puts("-1");//有剩余说明不行
        else printf("%d\n",ans);
    }
//    system("pause");
    return 0;
}

E题 题目链接 (树 构造)

题意:给定n个顶点,要求构造一棵树,满足直径为d, 每个点的度不超过k
(2<=n,k<=4e5)

思路:这题思路不难理解,先构造直径,然后枚举直径中间点,向外边挂点,深度不能超过这个中间点到直径两端的最短距离.

先上AC代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<math.h>
#include<set>
#include<unordered_map>
using namespace std;
#define LL long long
#define ULL unsigned long long
const int INF=30000000;
const double eps=1e-5;
const int maxn=4e5+50;
vector<int> g[maxn];
int du[maxn];
int n,k,d;
int cnt;
bool vis[maxn];
//给定n个顶点,要求构造一棵树,满足直径为d  每个点的度不超过k
//先构造一条直径 ,然后枚举直径中间的点进行dfs 深度不超过其到直径两端的最短距离

void dfs(int rt,int dep) {
    if(dep==0 || cnt==n) return;//dep到0就不能继续扩展了
    for(int i=1;i<=du[rt] && cnt<n;i++)
    {
        g[rt].push_back(++cnt);
        g[cnt].push_back(rt);
        du[cnt]--;
        dfs(cnt,dep-1);
    } 

void dfs2(int rt)
{
    vis[rt]=1;
    for(int i=0;i<(int)g[rt].size();i++)
    {
        int to=g[rt][i];
        if(!vis[to])
        {
            printf("%d %d\n",rt,to);
            dfs2(to);
        }
    }
}
int main()
{
//    ios::sync_with_stdio(false);
//    cin.tie(0);
//    cout.tie(0);
    scanf("%d%d%d",&n,&d,&k);
    fill(du,du+maxn,k);
    if(n<d+1)//点不够的情况
    {
        puts("NO");
        return 0;
    }
    if(k==1 && d>1)//度为1特判
    {
        puts("NO");
        return 0;
    }
    if(k==2 && n!=d+1)//度为2特判
    {
        puts("NO");
        return 0;
    }
    for(int i=1;i<=d;i++)
    {
        g[i].push_back(i+1);
        du[i]--;
        g[i+1].push_back(i);
        du[i+1]--;
    }
    cnt=d+1;
    for(int i=2;i<=d;i++)//枚举中间的点
    {
        int dep=min(i-1,d+1-i);
        dfs(i,dep);
    }
    if(cnt<n)//点太多了
    {
        puts("NO");
    }
    else
    {
        puts("YES");
        dfs2(1);
    }
 //   system("pause");
    return 0;
}

但是关于E题,有个地方想了很久也没搞清楚。 = =、

void dfs(int rt,int dep) {
    if(dep==0 || cnt==n) return;//dep到0就不能继续扩展了
    for(int i=1;i<=du[rt] && cnt<n;i++)
    {
        g[rt].push_back(++cnt);
        g[cnt].push_back(rt);
        du[cnt]--;
        dfs(cnt,dep-1);
    } }
  //把上面这个for循环改成下面这个while循环就会re,太奇怪了
void dfs(int rt,int dep)
{
    if(dep==0 || cnt==n) return;
    while(du[rt]>0)
    {
        g[rt].push_back(++cnt);
        g[cnt].push_back(rt);
        du[cnt]--;
        du[rt]--;
        if(cnt==n) break;
        dfs(cnt,dep-1);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值