6.29 CodeForce 818 A/B解题报告

A.Diplomas and Certificates

题面:

There are n students who have taken part in an olympiad. Now it’s time to award the students.

Some of them will receive diplomas, some wiil get certificates, and others won’t receive anything. Students with diplomas and certificates are called winners. But there are some rules of counting the number of diplomas and certificates. The number of certificates must be exactly k times greater than the number of diplomas. The number of winners must not be greater than half of the number of all students (i.e. not be greater than half of n). It’s possible that there are no winners.

You have to identify the maximum possible number of winners, according to these rules. Also for this case you have to calculate the number of students with diplomas, the number of students with certificates and the number of students who are not winners.

Input
The first (and the only) line of input contains two integers n and k (1 ≤ n, k ≤ 1012), where n is the number of students and k is the ratio between the number of certificates and the number of diplomas.

Output
Output three numbers: the number of students with diplomas, the number of students with certificates and the number of students who are not winners in case when the number of winners is maximum possible.

It’s possible that there are no winners

题目大意:

一共有n个人,最多不超过n/2个人获奖,获奖者有两类。这里称为A和B。B的人数是A的人数的K倍。问最多的获奖人数,并将A,B与未获奖人数输出。

大致思路;

纯水题,根据题意可以列一个一元方程。
而没有获奖者的条件也很容易判断。
直接解出答案。不过需要注意的是这个题需要long long ,int会爆

代码:

#include<iostream>
#include<cstdlib>
#include<cmath>
#include<cstdio>
using namespace std;
typedef long long ll;
int main()
{
    //freopen("in.txt","r",stdin);
    ll n,k;
    while(cin>>n>>k)
    {
        ll num1=0,num2=0,num3=n;
        ll div=k+1;
        if(n/2<div){//没有获奖者的条件
            cout<<0<<" "<<0<<" "<<n<<endl;
            continue;
        }
        ll maxcnt=n/2;
        ll ans=maxcnt/div;
        num1=ans;
        num2=ans*k;
        num3=n-num1-num2;
        cout<<num1<<" "<<num2<<" "<<num3<<endl;
    }
    return 0;
}

B. Permutation Game

题面:

n children are standing in a circle and playing a game. Children’s numbers in clockwise order form a permutation a1, a2, …, an of length n. It is an integer sequence such that each integer from 1 to n appears exactly once in it.

The game consists of m steps. On each step the current leader with index i counts out ai people in clockwise order, starting from the next person. The last one to be pointed at by the leader becomes the new leader.

You are given numbers l1, l2, …, lm — indices of leaders in the beginning of each step. Child with number l1 is the first leader in the game.

Write a program which will restore a possible permutation a1, a2, …, an. If there are multiple solutions then print any of them. If there is no solution then print -1.

Input
The first line contains two integer numbers n, m (1 ≤ n, m ≤ 100).

The second line contains m integer numbers l1, l2, …, lm (1 ≤ li ≤ n) — indices of leaders in the beginning of each step.

Output
Print such permutation of n numbers a1, a2, …, an that leaders in the game will be exactly l1, l2, …, lm if all the rules are followed. If there are multiple solutions print any of them.

If there is no permutation which satisfies all described conditions print -1.

题目大意:

一共有n个人,每个人有自己的编号,编号与顺序无关,编号范围是1-n
每个人有自己的L,轮到自己就数L个人,第L个成为下一个leader。
问有可能的编号情况,编号按顺序输出。编号情况不合法则输出-1。

大致思路:

判断是否合法的条件:
每个人的编号肯定是唯一的,不能重复
每个人只能有一个编号。
1-n所有编号都必须用上。
利用上述条件可以判断是否合法。
然后可以根据题意推出:
a[l[i]]=l[i+1]l[i]
这个式子执行m次可得到若干个a的值。
再将未分配的值随便放到未有确定值得人上,作为编号,并进行标记

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=110;
bool visit[maxn],use[maxn];
bool All_Visit(int num)
{
    for(int i=1;i<=num;++i)
        if((!visit[i])||(!use[i]))
            return false;
    return true;
}
int main()
{
    //freopen("in.txt","r",stdin);
    ios::sync_with_stdio(false);
    int n,m,l[maxn];
    int ans[maxn];
    while(cin>>n>>m)
    {
        bool flag=false;
        memset(visit,false,sizeof(visit));
        memset(use,false,sizeof(use));
        for(int i=0;i<m;++i)
            cin>>l[i];
        for(int i=0;i<m-1;++i){
            int x=l[i+1]-l[i];
            if(x<=0)
               x+=n;
            if(x>n)
                x%=n;
            if((use[x]||visit[l[i]])&&ans[l[i]]!=x){//情况是否合法
                flag=true;
                break;
            }
            ans[l[i]]=x;
            use[ans[l[i]]]=true;//标记数字使用情况
            visit[l[i]]=true;//标记每个人编号情况
        }
        if(flag){
            cout<<-1<<endl;
            continue;
        }
        for(int i=1;i<=n;++i){//分配未使用的数字
            if(!use[i]){
               // cout<<i<<endl;
                for(int j=1;j<=n;++j){
                    if(!visit[j]){
                        ans[j]=i;
                        visit[j]=true;
                        break;
                    }
                }
                use[i]=true;
            }
        }
        if(All_Visit(n)){//最后测试一遍数据是否合法
            for(int i=1;i<=n;++i){
                if(i!=1)
                    cout<<" ";
                cout<<ans[i];
            }
        }else
            cout<<-1;
        cout<<endl;
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值