1 条题解

  • 0
    @ 2021-6-15 14:22:08

    C++ :

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <algorithm>
    #include <deque>
    
    using namespace std;
    
    const int MAXN = 200000 + 1000;
    const int INF = 0x3f3f3f3f;
    
    int n;
    int cost1, time1, cost2, time2, pri;
    int s[MAXN];
    
    deque<pair<int, int> > que3, que2, que1;
    
    int calc(int x)
    {
    	int ret = 0;
    	que1.clear(), que2.clear(), que3.clear();
    	for(int i = 1; i <= n; i++)
    	{
    		while(!que3.empty() && que3.front().first <= i - time2)
    			que2.push_back(que3.front()), que3.pop_front();
    		while(!que2.empty() && que2.front().first <= i - time1)
    			que1.push_back(que2.front()), que2.pop_front();
    		int rem = s[i];
    		if(x > 0)
    		{
    			int del = min(rem, x);
    			rem -= del, x -= del;
    			ret += del * pri;
    		}
    		while(rem > 0 && !que1.empty())
    		{
    			int del = min(rem, que1.back().second);
    			rem -= del, que1.back().second -= del;
    			ret += del * cost1;
    			if(!que1.back().second)
    				que1.pop_back();
    		}
    		while(rem > 0 && !que2.empty())
    		{
    			int del = min(rem, que2.back().second);
    			rem -= del, que2.back().second -= del;
    			ret += del * cost2;
    			if(!que2.back().second)
    				que2.pop_back();
    		}
    		if(rem)
    			return INF;
    		que3.push_back(make_pair(i, s[i]));
    	}
    	return ret;
    }
    
    int ternary(int l, int r)
    {
    	int ret = INF;
    	while(l <= r)
    	{
    		int mid1 = (2 * l + r) / 3, mid2 = (l + 2 * r + 1) / 3;
    		int val1 = calc(mid1), val2 = calc(mid2);
    		if(val1 > val2 || val1 == INF)
    			l = mid1 + 1, ret = min(ret, val2);
    		else
    			r = mid2 - 1, ret = min(ret, val1);
    	}
    	return ret;
    }
    
    int main()
    {
    	ios::sync_with_stdio(false);
    //	 freopen("napkin.in", "r", stdin);
    //	 freopen("napkin.out", "w", stdout);
    
    	cin >> n >> time1 >> time2 >> cost1 >> cost2 >> pri;
    
    	if(time1 >= time2 && cost1 >= cost2)
    		time1 = INF;
    	else if(time1 <= time2 && cost1 <= cost2)
    		time2 = time1, cost2 = cost1, time1 = INF;
    	else if(time1 < time2)
    		swap(time1, time2), swap(cost1, cost2);
    
    	for(int i = 1; i <= n; i++)
    		cin >> s[i];
    
    	int s_sum = 0;
    	for(int i = 1; i <= n; i++)
    		s_sum += s[i];
    
    	int ans = ternary(1, s_sum);
    	cout << ans << endl;
    
    
    	return 0;
    }
    
    
    • 1

    信息

    ID
    992
    时间
    10000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者