Sicily 2014年每周一赛第一场

比较苦逼、爆0了、、总结一下吧、

1000. Cutting Sausages
 
     
     
 
Time Limit: 1sec    Memory Limit:256MB
Description
Mirko has given up on the difficult coach job and switched to food tasting instead. Having skipped breakfast like a professional connoisseur, he is visiting a Croatian cured meat festival. The most renowned cook at the festival, Marijan Bajs, has prepared N equal sausages which need to be distributed to M tasters such that each taster gets a precisely equal amount. He will use his trusted knife to cut them into pieces. 
In order to elegantly divide the sausages, the number of cuts splitting individual sausages must be as small as possible. For instance, if there are two sausages and six tasters (the first test case below), it is sufficient to split each sausage into three equal parts, making a total of four cuts. On the other hand, if there are three sausages and four tasters (the second test case below), one possibility is cutting off three quarters of each sausage. Those larger parts will each go to one of the tasrers, while the fourth taster will get the three smaller pieces (quarters) left over.
Mirko wants to try the famous sausages, so he volunteered to help Bajs. Help them calculate the minimum total number of cuts needed to carry out the desired division. 
 
Input

The first and only line of input contains two positive integers, N and M (1 ≤ N, M ≤ 100), the number of sausages and tasters, respectively.

Output

The first and only line of output must contain the required minimum number of cuts.

Sample Input
 Copy sample input to clipboard
样例1:
2 6
样例2:
3 4
样例3:
6 2
Sample Output
样例1:
4
样例2:
3
样例3:
0

题意就是给你n个香肠,让你平均的分给m个人。问你最少要切几刀?   题目很水!最近做数学题目做的多了。

以为推一个公式呢。其实不是,就是用一个循环无限分的情况!

首先n%m==0 ans=0;
n>m时 每人得到一个完整的香肠,剩下的n=n%m;的进行平均分配!下一种情况。
n<m时,如果m%n==0,则每个香肠切m/n-1,否则为m/n;
然后将剩下的香肠用上面的方法分给剩下的m=m%n个人,直到m==0结束,即所有人都分完为止。
代码:
#include<stdio.h>
int main()
{
    int N, M;
    while(~scanf("%d%d",&N,&M))
    {
        int ans = 0;
        while(N % M != 0) 
        {
            if(N > M)
                N = N % M;
            int s = M%N == 0 ? M / N -1 : M / N;
            M = M % N;
            ans = ans + s * N;
            if(M == 0)
                break;
        }
        printf("%d\n",ans);
    }
}

我的错的推公式的代码:

#include <cstdio>
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0||m==0)
        {
            printf("0\n");continue;
        }
        int ans=0;
        if(n<m)
        {
            if(m%n==0)
                ans=(m/n-1)*n;
            else
            {
                ans=n*(m/n)+n%(m%n);
            }
        }
        else
        {
            if(n%m==0)
                ans=0;
            else
            {
                n=n%m;
                if(m%n==0)
                    ans=(m/n-1)*n;
                else
                {
                    ans=n*(m/n)+n%(m%n);
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

下面做的c题。感觉是一道贪心题目,但是数据很大。超时了。想不到用什么数据结构优化。


10359. Valuable Jewellery

Constraints

Time Limit: 2 secs, Memory Limit: 256 MB

Description

The difficult economic situation in the country and reductions in government agricultural subsidy funding 

have caused Mirko to change his career again, this time to a thief. His first professional endeavour is a 

jewellery store heist.
The store contains N pieces of jewellery, and each piece has some mass Mi and value Vi . Mirko has K bags 

to store his loot, and each bag can hold some maximum mass Ci . He plans to store all his loot in these bags, 

but at most one jewellery piece in each bag, in order to reduce the likelihood of damage during the escape.
Find the maximum total jewellery value that Mirko can “liberate”.

Input

The first line of input contains two numbers, N and K (1 ≤ N, K ≤ 300 000).
Each of the following N lines contains a pair of numbers, Mi and Vi (1 ≤ Mi , Vi ≤ 1 000 000).
Each of the following K lines contains a number, Ci (1 ≤ Ci ≤ 100 000 000).
All numbers in the input are positive integers.

Output

The first and only line of output must contain the maximum possible total jewellery value.

Sample Input

样例1:
2 1
5 10
100 100
11
样例2:
3 2
1 65
5 23
2 99
10
2

Sample Output

样例1:
10
样例2:
164

Hint

Clarification of the second example: Mirko stores the first piece of jewellery into the second bag and 

the third piece into the first bag.

题意很好懂,给出n个珠宝的价格和重量。和k个能装t重量的袋子。条件是一个袋子只能装一个珠宝。问怎样装使得价值最大。

想到的就是按价值从大到小排序,价值相同的按重量从大到小排序,然后袋子能装的重量从小到大排序,然后依次选能够装进去的,标记。最后算出ans。

但是超时了。求大神指点。

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

struct Node
{
    int wi,vi;
    int ok;
};
Node a[303000];
int bag[303000];
int comp(Node x,Node y)
{
    if(x.vi!=y.vi)
        return x.vi>y.vi;
    if(x.wi!=y.wi)
        return x.wi>y.wi;
}

int main()
{
    int n,k;
    while(~scanf("%d%d",&n,&k))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&a[i].wi,&a[i].vi);
            a[i].ok=0;
        }
        for(int i=0;i<k;i++)
            scanf("%d",&bag[i]);
        sort(bag,bag+k);
        sort(a,a+n,comp);
        int ans=0;
        for(int i=0;i<k;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(bag[i]>=a[j].wi&&a[j].ok==0)
                {
                    ans+=a[j].vi;a[j].ok=1;
                    break;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值