当前位置: 首页 > 工具软件 > gdut-course > 使用案例 >

GDUT 第一次排位赛

潘璞瑜
2023-12-01

A. The Bucket List

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output

Farmer John is considering a change in how he allocates buckets for milking his cows. He thinks this will ultimately allow him to use a small number of total buckets, but he is not sure how many exactly. Please help him out! Farmer John has N cows (1≤N≤100), conveniently numbered 1…N. The ith cow needs to be milked from time si to time ti, and requires bi buckets to be used during the milking process. Several cows might end up being milked at the same time; if so, they cannot use the same buckets. That is, a bucket assigned to cow i′s milking cannot be used for any other cow’s milking between time si and time ti. The bucket can be used for other cows outside this window of time, of course. To simplify his job, FJ has made sure that at any given moment in time, there is at most one cow whose milking is starting or ending (that is, the si’s and ti’s are all distinct).

FJ has a storage room containing buckets that are sequentially numbered with labels 1, 2, 3, and so on. In his current milking strategy, whenever some cow (say, cow i) starts milking (at time si), FJ runs to the storage room and collects the bi buckets with the smallest available labels and allocates these for milking cow i.

Please determine how many total buckets FJ would need to keep in his storage room in order to milk all the cows successfully.

Input

The first line of input contains N. The next N lines each describe one cow, containing the numbers si, ti, and bi, separated by spaces. Both si and ti are integers in the range 1…1000, and bi is an integer in the range 1…10.

Output

Output a single integer telling how many total buckets FJ needs.

Example
input

3
4 10 1
8 13 3
2 6 2

output

4

Note

In this example, FJ needs 4 buckets: He uses buckets 1 and 2 for milking cow 3 (starting at time 2). He uses bucket 3 for milking cow 1 (starting at time 4). When cow 2 arrives at time 8, buckets 1 and 2 are now available, but not bucket 3, so he uses buckets 1, 2, and 4.

题目大意:

给你奶牛的挤奶的开始和结束的时间,然后判断最多要用同时用几个桶。

题目思路:

题目数据范围很小,我们可以直接用桶储存看在一分钟要挤几个奶,第二分钟。。。暴力到1000分钟,然后搜索出最大值即可。

代码:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <queue>
#include <utility>
#define mod 1000000007
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f


struct node{
 int s,e,v;
} t[105];

int bt[1005];
int yt[1005];

int main(){
    memset(yt,0,sizeof(yt));
    int n;
   scanf("%d",&n);
 for(int i=0;i<n;i++){
         scanf("%d %d %d",&t[i].s,&t[i].e,&t[i].v);
 }
   for(int i=1;i<=1000;i++){
    for(int j=0;j<n;j++){
        if(i>=t[j].s&&i<=t[j].e){
            yt[i]+=t[j].v;
        }
    }
   }
   int ans=-1;
   for(int i=0;i<=1000;i++){
           ans=max(ans,yt[i]);
   }
   cout<<ans<<endl;
}

I. Convention II

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output

Despite long delays in airport pickups, Farmer John’s convention for cows interested in eating grass has been going well so far. It has attracted cows from all over the world.

The main event of the conference, however, is looking like it might cause Farmer John some further scheduling woes. A very small pasture on his farm features a rare form of grass that is supposed to be the tastiest in the world, according to discerning cows. As a result, all of the N cows at the conference (1≤N≤105) want to sample this grass. This will likely cause long lines to form, since the pasture is so small it can only accommodate one cow at a time.

Farmer John knows the time ai that each cow i plans to arrive at the special pasture, as well as the amount of time ti she plans to spend sampling the special grass, once it becomes her turn. Once cow i starts eating the grass, she spends her full time of ti before leaving, during which other arriving cows need to wait. If multiple cows are waiting when the pasture becomes available again, the cow with the highest seniority is the next to be allowed to sample the grass. For this purpose, a cow who arrives right as another cow is finishing is considered “waiting”. Similarly, if a number of cows all arrive at exactly the same time while no cow is currently eating, then the one with highest seniority is the next to eat.

Please help FJ compute the maximum amount of time any cow might possibly have to wait in line (between time ai and the time the cow begins eating).

Input

The first line of input contains N. Each of the next N lines specify the details of the N cows in order of seniority (the most senior cow being first). Each line contains ai and ti for one cow. The ti’s are positive integers each at most 104, and the ai’s are positive integers at most 109.

Output

Please print the longest potential waiting time over all the cows.

Example

inputCopy

5
25 3
105 30
20 50
10 17
100 10

outputCopy

10

Note

In this example, we have 5 cows (numbered 1…5 according to their order in the input). Cow 4 is the first to arrive (at time 10), and before she can finish eating (at time 27) cows 1 and 3 both arrive. Since cow 1 has higher seniority, she gets to eat next, having waited 2 units of time beyond her arrival time. She finishes at time 30, and then cow 3 starts eating, having waited for 10 units of time beyond her starting time. After a gap where no cow eats, cow 5 arrives and then while she is eating cow 2 arrives, eating 5 units of time later. The cow who is delayed the most relative to her arrival time is cow 3.

这道题需要学会用STL里的优先队列。
下面这篇博客有关于优先队列很详细的介绍,大家可以看一下:
https://www.cnblogs.com/xzxl/p/7266404.html
这道题只需对结构体排个序,按时间顺序push入队,然后价值大的先出队,每次入队时都比较一下出队的所留时长与max,最后得到的便是结果。

#include <bits/stdc++.h>
#define maxn 10005
using namespace std;
struct node{
 int s,v,id;
friend bool operator<(node a,node b){
  return a.id>b.id;
 }
}a[100005];

bool cmp(node x,node y){
 if(x.s==y.s)
    return x.v<y.v;
 return x.s<y.s;
}

int main(){
  int n;
  cin>>n;
  for(int i=1;i<=n;i++){
    cin>>a[i].s>>a[i].v;
    a[i].id=i;
  }
  sort(a+1,a+n+1,cmp);
  priority_queue<node>que;
  int time=0,ans=0,k;
  for(int i=1;i<=n;i++){
    if(que.empty()){
      time=a[i].s+a[i].v;
      for(k=i+1;k<=n;k++){
        if(a[k].s<=time)
            que.push(a[k]);
        else break;
      }
    }
    else{
        node tmp=que.top();
        que.pop();
        ans=max(ans,time-tmp.s);
        time+=tmp.v;
        for(;k<=n;k++){
            if(a[k].s<=time)
                que.push(a[k]);
            else break;
        }
    }
  }
  cout<<ans<<endl;
}

E.Convention

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output

Cows from all over the world are arriving at the local airport to attend the convention and eat grass. Specifically, there are N cows arriving at the airport (1≤N≤105) and cow i arrives at time ti (0≤ti≤109). Farmer John has arranged M (1≤M≤105) buses to transport the cows from the airport. Each bus can hold up to C cows in it (1≤C≤N). Farmer John is waiting with the buses at the airport and would like to assign the arriving cows to the buses. A bus can leave at the time when the last cow on it arrives. Farmer John wants to be a good host and so does not want to keep the arriving cows waiting at the airport too long. What is the smallest possible value of the maximum waiting time of any one arriving cow if Farmer John coordinates his buses optimally? A cow’s waiting time is the difference between her arrival time and the departure of her assigned bus.

It is guaranteed that MC≥N.

Input

The first line contains three space separated integers N, M, and C. The next line contains N space separated integers representing the arrival time of each cow.

Output

Please write one line containing the optimal minimum maximum waiting time for any one arriving cow.

Example

inputCopy

6 3 2
1 1 10 14 4 3

outputCopy

4
这道题当时比赛的时候想不出来要怎么做QAQ,后来看了题解才恍然大悟。我们可以不去想要怎么求出最小的时间,而是去想满足题目的时间的最小值即可。所以这里便可以用二分不断搜索满足的条件,直到找到最小满足的时间就是答案了。
AC代码如下:

#include <bits/stdc++.h>
#define maxn 100005
using namespace std;

int n,m,c;
int t[maxn];
bool judge(int x){
  int l=0,r=0;
  int ans=0;
  while(l<n&&r<n){
    while(r-l+1<=c&&t[r]-t[l]<=x&&r<n){
        r++;
    }
    ans++;
    l=r;
  }
  if(ans>m) return false;
  else return true;
}


int main(){
  cin>>n>>m>>c;
  for(int i=0;i<n;i++){
    scanf("%d",&t[i]);
  }
  sort(t,t+n);
  int l=0,r=1000000000;
  while(l<r){
    int mid=(l+r)>>1;
    if(!judge(mid)) l=mid+1;
    else r=mid;
  }
  cout<<r<<endl;
}

B. Teamwork

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output

For his favorite holiday, Farmer John wants to send presents to his friends. Since he isn’t very good at wrapping presents, he wants to enlist the help of his cows. As you might expect, cows are not much better at wrapping presents themselves, a lesson Farmer John is about to learn the hard way.

Farmer John’s N cows (1≤N≤104) are all standing in a row, conveniently numbered 1…N in order. Cow i has skill level si at wrapping presents. These skill levels might vary quite a bit, so FJ decides to combine his cows into teams. A team can consist of any consecutive set of up to K cows (1≤K≤103), and no cow can be part of more than one team. Since cows learn from each-other, the skill level of each cow on a team can be replaced by the skill level of the most-skilled cow on that team.

Please help FJ determine the highest possible sum of skill levels he can achieve by optimally forming teams.

Input

The first line of input contains N and K. The next N lines contain the skill levels of the N cows in the order in which they are standing. Each skill level is a positive integer at most 105.

Output

Please print the highest possible sum of skill levels FJ can achieve by grouping appropriate consecutive sets of cows into teams.

Example

inputCopy

7 3
1
15
7
9
2
5
10

outputCopy

84

Note

In this example, the optimal solution is to group the first three cows and the last three cows, leaving the middle cow on a team by itself (remember that it is fine to have teams of size less than K). This effectively boosts the skill levels of the 7 cows to 15, 15, 15, 9, 10, 10, 10, which sums to 84.

这题有一个坑人的地方,就是上文有提到奶牛是一个consecutive set连续集,所以这里我们就不能给全部奶牛排序了。
这时候,想要怎样才能求到连续的最优解,我们应该第一反应就是动态规划(DP),通过不断求出局部最优,得到全局最优。
我们便可以写出
题目样例的dp推算:
0 1 30 0 0 0 0 0
0 1 30 37 0 0 0 0
0 1 30 45 0 0 0 0
0 1 30 45 54 0 0 0
0 1 30 45 54 0 0 0
0 1 30 45 54 63 0 0
0 1 30 45 54 63 0 0
0 1 30 45 54 63 68 0
0 1 30 45 54 63 72 0
0 1 30 45 54 63 72 83
0 1 30 45 54 63 72 84
将此写为代码即可
AC代码如下:

#include <bits/stdc++.h>
#define maxn 10005
using namespace std;
int dp[maxn];
int a[maxn];
int n,k;
int main()
{
    cin>>n>>k;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    memset(dp,0,sizeof(dp));
    for(int i=1;i<=n;i++){
        int maxx=a[i];
        dp[i]=dp[i-1]+a[i];
        for(int j=1;j<k;j++){
            if(i-j<=0) break;
            maxx=max(maxx,a[i-j]);
            dp[i]=max(dp[i],dp[i-j-1]+maxx*(j+1));
            /*for(int i=0;i<=n;i++){
                printf("%d ",dp[i]);
            }
            printf("\n");*/
        }
    }
    cout<<dp[n]<<endl;
}

本人蒟蒻一枚,大家有问题欢迎提出Qrz~

 类似资料: