Slay the Dragon 二分 + 贪心

阅读: 评论:0

Slay the Dragon 二分 + 贪心

Slay the Dragon 二分 + 贪心

二分+贪心找到最小花费,主要是边界有点麻烦

// Problem: C. Slay the Dragon
// Contest: Codeforces - Educational Codeforces Round 114 (Rated for Div. 2)
// URL: 
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor ()#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<list>
#include<unordered_map>
using namespace std;
#define ff first
#define ss second
#define lowbit(x) (x&-x)
#define pf(a) printf("%dn",a)
#define mem(x,y) memset(x,y,sizeof(x))
#define dbg(x) cout << #x << " = " << x << endl
#define rep(i,l,r) for(int i = l; i <= r; i++)
#define fep(i,a,b) for(int i=b; i>=a; --i)
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;const int N=2e5+100;
int n, m;
ll a[N];
vector<int> v[N];void solve()
{scanf("%d", &n);ll sum = 0;a[0] = 1e18;rep(i,1,n) {scanf("%lld", &a[i]);sum += a[i];}	sort(a+1, a+1+n);scanf("%d", &m);while(m--){ll defen, atta, ans = 0;scanf("%lld %lld", &defen, &atta);int t = lower_bound(a+1, a+1+n, defen) - a;if(a[t] == defen){if(sum-a[t]>=atta) ans = 0;else ans = abs(a[t]+atta-sum);}else if(t > n) {ans += defen - a[n];if(sum - a[n] < atta) ans += (atta - sum + a[n]);}else{ll res = 0;res += abs(a[t-1]-defen);ll s1 = sum - a[t-1];if(s1 < atta) res += atta - s1;ll xx = 0;if(a[t] >= defen) {ll s3 = sum - a[t];if(s3 < atta) xx += atta - s3;}ll cnt = 0;cnt += abs(a[t]-defen);ll s2 = sum - a[t];if(s2 < atta) cnt += atta - s2;ans = min({res, cnt,xx});}printf("%lldn", ans);//cout << " t = " << t << " " << ans << endl;//cout << t << endl;}
}int main()
{solve();return 0;
}

本文发布于:2024-01-27 20:37:29,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/17063590492496.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:贪心   Slay   Dragon
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23