牛客假日团队赛39L,K


L

农夫约翰有N头需要挤奶的奶牛(1 <= N <= 10,000),每头奶牛只需要一个单位时间就能挤奶。 
作为不耐烦的动物,如果农夫约翰等太久才挤牛奶,一些母牛将拒绝挤奶。更具体地说,母牛i生产g_i加仑的牛奶(1 <= g_i <= 1000),但前提是在时间d_i(1 <= d_i <= 10,000)的最后期限之前挤奶。时间从t = 0开始,因此最多x头母牛可以在截止时间t = x之前挤奶。 
请帮助农夫约翰确定如果他最佳地挤牛奶,他可以获得的最大牛奶量。

输入描述:

*第1行:N的值。

*第2..1 + N行:第i + 1行包含整数g_i和d_i。

输出描述:

*第1行:农夫约翰
可以获得的最大加仑牛奶
示例1

输入

4
10 3
7 5
8 1
2 1

输出

25

说明

输入的详细信息:
有4头母牛。如果在时间
3 挤奶,前者将产生10加仑的牛奶,依此类推。

输出详细信息:
约翰农民首先挤奶3头奶牛,因为
与母牛3的冲突而无法在截止日期前挤奶,所以放弃了母牛4
。农夫约翰随后挤奶了母牛1和2。

这道也不知道是啥算法,贪心吧,觉得有点点和贪心挂钩

一共有n头牛,每头牛都有一个截止时间,要求你最大奶量
感觉读题有点像是个背包问题
贪心吗就是能挤奶就挤奶
那么我们把奶牛按照截止时间从小到大排序,
在时间符合的时候呢我们就所有的都要,
但时间不符合时呢,我们就把最小的那个牛的奶不要(要是这个不符合的奶牛最小就不用管,放弃他,如果不是最小就放弃前面的一头奶牛,然后这头奶牛在前面那头的时间点挤奶)
这样就是一直在贪心嘛
不过这里就是我们又有一个不断自动排序的数据结构(想到优先队列,优先级高的先出,set堆自动排序)
这里我们用优先队列,比较好操作
 1 #include <iostream>
 2 #include <map>
 3 #include <cstdio>
 4 #include <algorithm>
 5 #include <queue>
 6 
 7 using namespace std;
 8 typedef long long ll;
 9 int sum,n;
10 struct node{
11     int g,d;
12     bool operator <(const node &other )const
13     {
14         return this->d<other.d;
15     }
16 }cow[10010];
17 priority_queue<int>q;//优先队列,默认降序
18 int main()
19 {
20     cin >> n;
21     for(int i=0;i<n;i++)
22     {
23         cin >> cow[i].g>>cow[i].d;
24     }
25     sort(cow,cow+n);
26     int j=1;
27     for(int i=0;i<n;i++)
28     {
29         if(j<=cow[i].d)
30         {
31             q.push(-cow[i].g);
32             j++;
33         }
34         else{
35             ll a = -q.top();
36             if(a<cow[i].g){
37                     a= -cow[i].g;
38                 q.pop();
39                 q.push(a);
40 
41             }
42         }
43     }
44     while(!q.empty())
45     {
46         sum-=q.top();
47         q.pop();
48     }
49     cout<<sum<<endl;
50     return 0;
51 }
View Code

K

题目描述

Bessie likes downloading games to play on her cell phone, even though she does find the small touch screen rather cumbersome to use with her large hooves.
She is particularly intrigued by the current game she is playing. The game starts with a sequence of N positive integers (2≤N≤248), each in the range 1…40. In one move, Bessie can take two adjacent numbers with equal values and replace them a single number of value one greater (e.g., she might replace two adjacent 7s with an 8). The goal is to maximize the value of the largest number present in the sequence at the end of the game. Please help Bessie score as highly as possible!

输入描述:

The first line of input contains N, and the next N lines give the sequence of N numbers at the start of the game.

输出描述:

Please output the largest integer Bessie can generate.
示例1

输入

4
1
1
1
2

输出

3

说明

In this example shown here, Bessie first merges the second and third 1s to obtain the sequence 1 2 2,
and then she merges the 2s into a 3. Note that it is not optimal to join the first two 1s.

翻译:

贝西喜欢下载游戏在手机上玩,即使她确实发现小触摸屏与大蹄子一起使用也很麻烦。
她对当前正在玩的游戏特别感兴趣。游戏以N个正整数(2≤N≤248)的序列开始,每个正整数的范围为1…40。
一步,贝茜可以取两个相等的相邻数字,并将它们替换为一个更大的单个数值(例如,她可以用8代替两个相邻的7)。
目标是在游戏结束时最大化序列中存在的最大数字的值。请帮助Bessie得分最高!

就是我们玩的那种消消乐的简化版
这道题目,我觉得挺简单的是,但是,但是我不会,,,,
看了大佬的题解,,,,

这个题是个dp,我没感觉到是个区间dp
因为这个题的数据范围,2<N<248
因此可以知道最高分就是 40 + log(248)向下取整
可以暴力枚举每个分数的所有情况
dp[i][j] 表示 i这个数从j开始到dp[i][j]合成的
转移方程dp[i][j] = dp[i-1][dp[i-1][j]];
i 要有两个i-1合成,这里dp[i-1][j]就是从j到dp[i-1][j]-1合成了i-1,然后从dp[i-1][j]到dp[i][j]-1合成了i-1;
然和从j到dp[i][j]-1合成了i

代码:
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <cmath>
 6 #include <map>
 7 
 8 using namespace std;
 9 typedef long long ll;
10 int dp[100][1010];
11 int a[1010];
12 int main()
13 {
14     int n;
15     cin >> n;
16     int ma =-1;
17     for(int i=1;i<=n;i++)
18     {
19         cin>> a[i];
20         dp[a[i]][i]=i+1;
21     }
22     for(int i=2;i<=50;i++)
23     {
24         for(int j=1;j<=n;j++)
25         {
26             if(dp[i][j]==0)
27             {
28                 dp[i][j] = dp[i-1][dp[i-1][j]];
29             }
30             if(dp[i][j])
31             ma = i;
32         }
33 
34     }
35     cout<< ma <<endl;
36     return 0;
37 }
View Code

待更新。。。。。。

猜你喜欢

转载自www.cnblogs.com/wsxmmfby-jy/p/12791453.html
今日推荐