【链接】点击打开链接
【题意】
在这里写题意
【题解】
先每个单位都不建造bi;
打死一个ai之后,把bi加入到大根堆里面.
然后等到不够打死某个单位的时候;
从大根堆里面取出最大的那个bi;不断取,直到够打死ai为止。
【错的次数】
0
【反思】
m写成了n
【代码】
/* */ #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <vector> #include <map> #include <queue> #include <iomanip> #include <set> #include <cstdlib> #include <cmath> #include <bitset> using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define LL long long #define rep1(i,a,b) for (int i = a;i <= b;i++) #define rep2(i,a,b) for (int i = a;i >= b;i--) #define mp make_pair #define pb emplace_back #define fi first #define se second #define ld long double #define ms(x,y) memset(x,y,sizeof x) #define ri(x) scanf("%d",&x) #define rl(x) scanf("%lld",&x) #define rs(x) scanf("%s",x) #define rf(x) scnaf("%lf",&x) #define oi(x) printf("%d",x) #define ol(x) printf("%lld",x) #define oc putchar(' ') #define os(x) printf(x) #define all(x) x.begin(),x.end() #define Open() freopen("F:\rush.txt","r",stdin) #define Close() ios::sync_with_stdio(0) #define sz(x) ((int) x.size()) #define ld long double typedef pair<int, int> pii; typedef pair<LL, LL> pll; //mt19937 myrand(time(0)); //int get_rand(int n){return myrand()%n + 1;} const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 }; const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 }; const double pi = acos(-1.0); const int N = 5e4; int n, m; priority_queue <int, vector<int>, less <int> > q2; int a[N + 10], b[N + 10],rest; int getans() { int ans = 0; rest = n; rep1(i, 1, m) { if (rest >= a[i]) { rest -= a[i]; q2.push(b[i]); } else { a[i] -= rest; while (1) { if (q2.empty()) return -1; rest = q2.top(); q2.pop(); ans++; if (rest >= a[i]) { rest -= a[i]; a[i] = 0; break; } else a[i] -= rest; } q2.push(b[i]); } } return ans; } int main() { //Open(); //Close(); while (~ri(n)) { ri(m); rep1(i, 1, m) ri(a[i]); rep1(i, 1, m) ri(b[i]); while (!q2.empty()) q2.pop(); oi(getans()); puts(""); } return 0; }