• URAL 1995 Illegal spices 贪心构造


    Illegal spices

    题目连接:

    http://acm.timus.ru/problem.aspx?space=1&num=1995

    Description

    Jabba: Han, my boy, you disappoint me. Why haven’t you paid me? And why did you fry poor Greedo?
    Han: Look, Jabba, next time you wanna talk to me, come see me yourself. Don’t send one of these twerps.
    Jabba: Han, I can’t make exceptions. What if everyone who smuggled for me dropped their cargo at the first sign of an imperial starship?
    Han Solo and his flight mechanic Chewbacca are experienced smugglers. They have spent several years working for a crime boss of the Tatooine planet Jabba the Hutt. But even the best of the best blow it sometimes.
    During a flight, captain Solo’s ship called «Millennium Falcon» met imperial customs officers. Han was transporting cargo consisting of n bags with spices. Each bag weighs an integer number of kilograms. Han noticed the customs officers from above and decided to dump the cargo into space.
    Captain Solo dumped the first bag. Then he decided to take a risk and leave part of the cargo in the secret section. Han was checking each bag, and during that he was counting how many bags he has already checked (including the first one) that were lighter than the current one. He left a bag on the ship only if that number was at least p percent from the total number of the bags that have already been checked. Using this strategy Solo hoped to leave the most important part of the cargo on the ship.
    As a result, he was left with only k bags that fit in the secret section easily. The customs officers couldn’t find anything and they left the «Millennium Falcon».
    Now Han understands that he has lost quite a large part of the cargo and that Jabba is going to be quite displeased. Unfortunately, he doesn’t remember the total weight of the transported goods. Help Han find the minimum possible total weight just to show him how big a loser he is.

    Input

    The first line contains integers n and k (1 ≤ k < n ≤ 10 5). The second line contains integer p (1 ≤ p ≤ 100).

    Output

    In the first line, print the answer to the problem. In the second line print n space-separated integers — the bags’ weights in kilograms in the order Captain Solo checked them, including the first bag. If there are multiple sequences that meet the problem statement, you can print any of them. It is guaranteed that at least one such sequence exists.

    Sample Input

    3 1
    50

    Sample Output

    4
    1 2 1

    Hint

    题意

    有n个物品,然后有k个东西留了下来

    如果x/(i-1)<p的话就会被留下来,其中x是前面小于这个数的数量

    要求你构造一组解,使得总和最小,保证存在一组解

    题解:

    贪心,首先前面的肯定全是1嘛,这些全扔了就好

    后面的我们保证不扔的话,那么就不断增大,直到满足就好了嘛

    显然后面是递增的,这个也很好写。

    代码

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 1e5+7;
    int w[maxn];
    int main()
    {
        int n,k,p;
    	scanf("%d%d%d",&n,&k,&p);
    	k = n - k;
    	int now = 1;
    	int num = 0;
    	for(int i=1;i<=n;i++)
    	{
    		if(i<=k)
    		{
    			w[i]=1;
    			num++;
    		}
    		else
    		{
    			if(100*num>=p*(i-1))w[i]=now+1;
    			else 
    			{
    				now++;
    				num=i-1;
    				w[i]=now+1;
    			}
    		}
    	}
    	long long ans = 0;
    	for(int i=1;i<=n;i++)
    		ans = ans + w[i];
    	cout<<ans<<endl;
    	for(int i=1;i<=n;i++)
    	{
    		if(i==1)printf("%d",w[i]);
    		else printf(" %d",w[i]);
    	}
    	printf("
    ");
    }
  • 相关阅读:
    畅通工程续 dijkstra
    能量项链 区间dp
    机器人军团
    skiing
    数论知识
    灯泡游戏
    60. 第k个排列
    17. 电话号码的字母组合
    101. 对称二叉树
    144. 二叉树的前序遍历
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5370308.html
Copyright © 2020-2023  润新知