考試時的T1
水題,當時晚上沒睡好,把m寫成3(樣例裏面m == 3)
然後就非常帥氣地一分沒有
很水的一道模擬(貪心),模擬接水過程,由於接完水最終時間一定,所以我們不需要管接完了多少,我們只需要知道當前所有水龍頭中,總接水時間最短的是哪一個( 用小根堆維護),然後將所有已排好序中最小的哪一個接上去就行了,雖然原題中還特別聲明“特別地,同學們在打水前排好了隊,接水所用時間更長的先接。”但這其實沒用,因爲總的接水時間一定,每次貪心將最小的接上,則最後將最大的接上時,其當前所在序列的總接水時間一定是最小的(小根堆性質)
STL優先隊列_小根堆的定義:priority_queue< type , vector< type >, greater< type > >;
( type,填你想向優先隊列裏添加元素的類型 )
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long LL;
LL n,m;
LL xx;
LL w[1000002];
priority_queue<LL,vector<LL >,greater<LL > > xq;
inline LL read()
{
char ch;
LL data = 0;
LL f = 1;
while(ch < '0'||ch > '9')
{
ch = getchar();
if(ch == '-')
f = -1;
}
do{
data = data*10 + ch-'0';
ch = getchar();
}while(ch >= '0'&&ch <= '9');
return data*f;
}
inline void search()
{
while(n--)
{
LL x = xq.top();
xq.pop();
//cout<<xq.top()<<endl;
xq.push(x + w[n+1]);
}
while(!xq.empty())
{
xx = max(xq.top(),xx);
xq.pop();
//cout<<"xx: "<<xx<<endl;
}
}
int main()
{
n = read();
m = read();
//cout<<n<<" "<<m<<endl;
for(LL i =1;i<=n;i++)
{
w[i] = read();
//cout<<"w of "<<i<<" is "<<w[i]<<endl;
}
sort(w+1,w+1+n);
if(n <= m)
{
printf("%lld\n",w[n]);
return 0;
}
for(LL i = 1;i <= min(n,m);i++)
xq.push(0);
search();
printf("%lld\n",xx);
return 0;
}
THE END
By Peacefuldoge