K - Knapsack Collection Gym - 101482K(模拟)

2024-04-16 01:18

本文主要是介绍K - Knapsack Collection Gym - 101482K(模拟),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Gerald’s job is to welcome the teams for this year’s NWERC at the airport in Linköping. One of his duties is to stand at the luggage carousel and collect all the knapsacks that the teams are bringing. Gerald is a lazy person, so he just stands at the same position of the carousel and waits for bags to pass by so he can pick them up.
The baggage carousel consists of s luggage slots,
numbered in ascending order from 0 to s − 1. Since
the baggage carousel is cyclic, luggage slots s − 1 and
0 also lie side by side. The carousel turns in such a
way that if Gerald stands in front of slot i at some point in time, he will stand in front of slot
(i + 1) mod s one time unit later.
In the beginning Gerald prepares a huge baggage cart at some position and stands there to
wait for luggage. When a knapsack arrives in front of Gerald, he needs t time units to take it and put it on the baggage cart. After these t time units he is ready to pick up another knapsack. As long as there are remaining knapsacks on the luggage carousel, Gerald always takes the next one to arrive at his position as soon as he is ready after putting away the previous one.
Now Gerald wonders about the effect of his choice of position on the time it will take him to finish this task. It is up to you to help Gerald calculate the minimum, maximum, and average time to pick up all knapsacks, taken over all s possible slots, which can appear in front of Gerald after preparation. Time starts when he has prepared the baggage cart at some slot of the baggage carousel and ends after he has put the last knapsack on the cart.
Input
The input consists of:
• onelinewiththreeintegersn(1≤n≤2000),s(1≤s≤107)andt(1≤t≤107), where n is the number of knapsacks to pick up, s is the number of slots of the carousel, and t is the number of time units Gerald needs to pick up a knapsack from the carousel and put it on the cart;
• onelinewithnintegersk1,…,kn (0 ≤ ki ≤ s−1for1 ≤ i ≤ n),theslotsofthe knapsacks.
There may be several knapsacks stacked on top of each other in the same slot, but Gerald can still only pick up one knapsack at a time.
Output
Output three lines of output containing the minimum, maximum, and average time to pick up all the luggage, over all s positions. The average time should be output as a reduced fraction in the form p/q.
NWERC 2014 Problem K: Knapsack Collection 21
Picture by Bernhard J. Scheuvens via Wikimedia Commons.
Sample Input 1
Sample Output 1
7 10 10000000 0000001
70000001
70000009
350000027/5
Sample Input 2
Sample Input 3
Sample Output 2
Sample Output 3
10 10 3 0022446688
39 40 79/2
9 10000000 1 072345618
9
10000000
12500021249991/2500000

题意:
人坐在同一个节点,装一个物品时间为s。
机场取行李的地方是环形的,每秒走一个格子。
每个物品有对应出现的位置。
人坐在哪个位置取完物时间最大、时间最小、时间平均值。

思路:

  1. 假设确定了起点,那么很好办,每次找相距最近的一个点去拿物品即可。这个过程可以用multiset维护。
  2. 如果只算最小值,我们只需要算所有的有物品的格子。这不难思考。因为所有没有物品的格子,是要等一段时间才能到一个有物品的格子上。
  3. 如果算最大值,那么算上有物品的点取最大值,再算上每个物品后一个格子(等待最大时间到下一个物品)的最大值即可。
  4. 算总和,物品数目不大,这个过程是可以暴力维护的。而其他的点,其实可以用已经算出的点直接搞出来。假设 a [ i ] a[i] a[i]这个点的花费是 c o s t cost cost,那么 a [ i ] − 1 a[i]-1 a[i]1的值(假设这上面没有物品)就是 c o s t + 1 cost+1 cost+1,依次类推可以借助等差数列维护出所有没算出来的点。

ACNEW

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <set>
#include <queue>
#include <cmath>
#include <vector>
#include <map>
#include <ctime>using namespace std;typedef long long ll;
const int maxn = 2e3 + 7;multiset<int>st;
int a[maxn];
int n,s,t;ll gcd(ll n,ll m) {return m == 0 ? n : gcd(m,n % m);
}ll cal(int pos) {multiset<int>nowst;nowst = st;ll ans = 0;for(int i = 1;i <= n;i++) {auto it = nowst.lower_bound(pos);if(it != nowst.end()) {int num = *it;ans += num - pos + t;pos = (num + t) % s;} else {it = nowst.begin();int num = *it;ans += s - 1 - pos + num + 1 + t;pos = (num + t) % s;}nowst.erase(it);}return ans;
}void solve() {ll mi = 1e18,mx = 0,ans = 0;for(int i = 1;i <= n;i++) {if(i != 1 && a[i] == a[i - 1]) continue;ll tmp = cal(a[i]);mi = min(mi,tmp);mx = max(mx,tmp);ans += tmp;if(a[i] - a[i - 1] - 1 >= 1 && i != 1) {ll num = a[i] - a[i - 1] - 1;ans += tmp * num + (num + 1) * num / 2;}}ll num = s - 1 - a[n] + a[1];if(num >= 1) {ll tmp = cal(a[1]);ans += tmp * num + (num + 1) * num / 2;}for(int i = 1;i <= n;i++) {ll tmp = cal((a[i] + 1) % s);mx = max(mx,tmp);}ll Gcd = gcd(ans,s);ans /= Gcd;s /= Gcd;printf("%lld %lld %lld/%d\n",mi,mx,ans,s);
}int main() {scanf("%d%d%d",&n,&s,&t);for(int i = 1;i <= n;i++) {scanf("%d",&a[i]);st.insert(a[i]);}sort(a + 1,a + 1 + n);solve();return 0;
}
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <string>
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <set>using namespace std;typedef long long ll;const int maxn = 2005;
ll a[maxn];
multiset<ll>st;
ll n,s,t;
map<ll,ll>mp;ll gcd(ll n,ll m) {return m == 0 ? n : gcd(m,n % m);
}ll cal(ll tim) {multiset<ll>nowst;nowst = st;ll cost = 0;for(int j = 1;j <= n;j++) {auto it = nowst.lower_bound(tim);ll num;if(it == nowst.end()) {num = *nowst.begin();it = nowst.begin();}else {num = *it;}if(num >= tim) {cost += num - tim;}else {cost += (num - tim + s) % s;}cost += t;tim = num;tim = (tim + t) % s;nowst.erase(it);}return cost;
}void solve() {ll ans = 0;ll mi = 1e18,mx = 0;ll cnta1 = 0;for(int i = 1;i <= n;i++) {ll tmp = cal(a[i]);if(i == 1) {cnta1 = tmp;}if(a[i] == a[i - 1]) continue;mi = min(tmp,mi);mx = max(tmp,mx);ans += tmp;if(a[i] - a[i - 1] >= 2 && i != 1) {ll num = a[i] - a[i - 1] - 1;ans += tmp * num + num * (num + 1) / 2;}}ll num = s - a[n] + a[1] - 1;ans += cnta1 * num + num * (num + 1) / 2;for(int i = 1;i <= n;i++) {if(mp[(a[i] + 1) % s] == 1) continue;ll tmp = cal((a[i] + 1) % s);mx = max(mx,tmp);}ll tmp = gcd(s,ans);s /= tmp; ans /= tmp;printf("%lld %lld %lld/%lld\n",mi,mx,ans,s);
}int main() {a[0] = -1;scanf("%lld%lld%lld",&n,&s,&t);for(int i = 1;i <= n;i++) {scanf("%lld",&a[i]);st.insert(a[i]);mp[a[i]] = 1;}sort(a + 1,a + 1 + n);ll ans = 0;solve();
}//7 10 10000000
//0 0 0 0 0 0 1//7 100 10000000
//1 2 3 4 5 6 7//7 9 10000000
//1 2 3 4 5 5 7

这篇关于K - Knapsack Collection Gym - 101482K(模拟)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/907439

相关文章

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

usaco 1.2 Transformations(模拟)

我的做法就是一个一个情况枚举出来 注意计算公式: ( 变换后的矩阵记为C) 顺时针旋转90°:C[i] [j]=A[n-j-1] [i] (旋转180°和270° 可以多转几个九十度来推) 对称:C[i] [n-j-1]=A[i] [j] 代码有点长 。。。 /*ID: who jayLANG: C++TASK: transform*/#include<

hdu4431麻将模拟

给13张牌。问增加哪些牌可以胡牌。 胡牌有以下几种情况: 1、一个对子 + 4组 3个相同的牌或者顺子。 2、7个不同的对子。 3、13幺 贪心的思想: 对于某张牌>=3个,先减去3个相同,再组合顺子。 import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOExcepti

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟)

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟) 题目描述 给定一个链表,链表中的每个节点代表一个整数。链表中的整数由 0 分隔开,表示不同的区间。链表的开始和结束节点的值都为 0。任务是将每两个相邻的 0 之间的所有节点合并成一个节点,新节点的值为原区间内所有节点值的和。合并后,需要移除所有的 0,并返回修改后的链表头节点。 思路分析 初始化:创建一个虚拟头节点

Collection List Set Map的区别和联系

Collection List Set Map的区别和联系 这些都代表了Java中的集合,这里主要从其元素是否有序,是否可重复来进行区别记忆,以便恰当地使用,当然还存在同步方面的差异,见上一篇相关文章。 有序否 允许元素重复否 Collection 否 是 List 是 是 Set AbstractSet 否

每日一题|牛客竞赛|四舍五入|字符串+贪心+模拟

每日一题|四舍五入 四舍五入 心有猛虎,细嗅蔷薇。你好朋友,这里是锅巴的C\C++学习笔记,常言道,不积跬步无以至千里,希望有朝一日我们积累的滴水可以击穿顽石。 四舍五入 题目: 牛牛发明了一种新的四舍五入应用于整数,对个位四舍五入,规则如下 12345->12350 12399->12400 输入描述: 输入一个整数n(0<=n<=109 ) 输出描述: 输出一个整数

【算法专场】模拟(下)

目录 前言 38. 外观数列 算法分析 算法思路 算法代码 1419. 数青蛙 算法分析 算法思路 算法代码  2671. 频率跟踪器 算法分析 算法思路 算法代码 前言 在前面我们已经讲解了什么是模拟算法,这篇主要是讲解在leetcode上遇到的一些模拟题目~ 38. 外观数列 算法分析 这道题其实就是要将连续且相同的字符替换成字符重复的次数+

模拟实现vector中的常见接口

insert void insert(iterator pos, const T& x){if (_finish == _endofstorage){int n = pos - _start;size_t newcapacity = capacity() == 0 ? 2 : capacity() * 2;reserve(newcapacity);pos = _start + n;//防止迭代

浅谈PHP5中垃圾回收算法(Garbage Collection)的演化

前言 PHP是一门托管型语言,在PHP编程中程序员不需要手工处理内存资源的分配与释放(使用C编写PHP或Zend扩展除外),这就意味着PHP本身实现了垃圾回收机制(Garbage Collection)。现在如果去PHP官方网站(php.net)可以看到,目前PHP5的两个分支版本PHP5.2和PHP5.3是分别更新的,这是因为许多项目仍然使用5.2版本的PHP,而5.3版本对5.2并不是完

PHP实现二叉树遍历(非递归方式,栈模拟实现)

二叉树定义是这样的:一棵非空的二叉树由根结点及左、右子树这三个基本部分组成,根据节点的访问位置不同有三种遍历方式: ① NLR:前序遍历(PreorderTraversal亦称(先序遍历)) ——访问结点的操作发生在遍历其左右子树之前。 ② LNR:中序遍历(InorderTraversal) ——访问结点的操作发生在遍历其左右子树之中(间)。 ③ LRN:后序遍历(PostorderT