http://cdn.ac.nbutoj.com/Problem/view.xhtml?id=1181

本文讨论了在英雄联盟(LOL)游戏中,如何利用贪心策略来确定在有限时间内击杀指定数量小兵以达到最小总威胁值的方法。通过给出具体的算法实现,解释了如何在给定初始威胁值和可击杀小兵数量的情况下,计算出最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 问题描述
  • The game LOL is an interesting game. Recently we are playing this game. Once I used a champion named Kog'Maw - The Mouth of the Abyss.
    For I have to support teammate, I only have a little time to do the last hit to kill the enemy's minions.
    There're N (2 <= N <= 10000) minions, each minion has a threat value TVi (1 <= TVi <= 9).
    And there's a formula to calculate the total threat value:



    I only can kill K (1 <= K < N) minions.
    When I kill one minion, the minions after it will move to front for one step. It means when I kill Minion[5], then Minion[6] will be Minion[5] and Minion[7] will be Minion[6] and so on.
    How to kill the minions that I can leave the minimum total threat value?
  • 输入
  • This problem contains several cases.
    Each case contains two numbers. The first number T is the total threat value at the very beginning (1 <= T < 10^10001). Then follows an integer K, means the number that minions I can kill.
  • 输出
  • For each case, you should output the minimum total threat value after I kill K minions.
  • 样例输入
  • 123456 4
    3212311 4
    
  • 样例输出
  • 12
    111
    
    法一:采用贪心策略,每次删除单调递减的第一个字符。
  • AC代码:
  • #include<iostream>
    #include<string>
    using namespace std;
    int main()
    {
    	string s;
    	int n;
    	while(cin>>s>>n)
    	{ 
    		for(int i=0;i<n;++i)
    		{
    			for(int j=0;j<s.size();++j)
    				if(j==s.size()-1||s[j]>s[j+1])
    				{
    					s.erase(s.begin()+j);
    					break;
    				}
    		}cout<<s<<endl;
    	}return 0;
    }

    法二:每次在规定范围内删除最小的字符
  • #include<iostream>
    #include<string.h>
    #include<string>
    using namespace std;
    string work(string s,int n)
    {
    	int m=s.size()-n;
    	int first=0,last=n;
    	string s1="";
    	while(m--)
    	{  
    		int minx=9999999,p;
    		for(int i=first;i<=last;++i)
    			if(s[i]<minx) {p=i;minx=s[i];}
    		    s1+=minx;
    		   first=p+1,last++;
    	}return s1;
    }
    int main()
    {
    	string s;
    	int n;
    	while(cin>>s>>n)
    	{
    		cout<<work(s,n)<<endl;
    	}return 0;
    }
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值