6 solutions
-
1
// 思想: 先按时间升序 (金钱的顺序可升序可降序) // 再用 变量time 记录当前的时间,若没有项目有时间冲突,则全部游戏都可以顺利完成(废话) // 否则,从 i = 1 到 当前位置 枚举,看是否存在一个 项目金钱 比此项目金钱少 (可以用优先队列或小根堆优化) // 若存在,则选择放弃完成那个项目,再用st[N数组标记那个项目已经放弃(小根堆或优先队列则直接出队) // 若不存在,则放弃当前项目是最优选择 // 最后 m - res(放弃项目的金钱) 则是答案 #include<iostream> #include<algorithm> #include <vector> #include <queue> using namespace std; #define int long long #define x first #define y second typedef pair<int,int> PII; const int N = 550; vector<PII> v(N); int b[N], c[N]; bool st[N]; bool cmp(PII &a, PII &b) { if( a.x != b.x ) return a.x < b.x; return a.y > b.y; } signed main() { int m, n, res = 0; cin >> m >> n; for(int i = 1; i <= n; i ++ ) cin >> v[i].x; for(int i = 1; i <= n; i ++ ) cin >> v[i].y; //sort(v.begin()+1, v.begin()+1+n); sort(v.begin()+1, v.begin()+1+n, cmp); int time = 1; for(int i = 1; i <= n; i ++ ) { if( time > v[i].x ) { int idx = i; for(int j = 1; j < i; j ++ ) if( v[j].y < v[idx].y && !st[j] ) idx = j; st[idx] = true; res += v[idx].y; } else time ++; } cout << m - res; }
//小根堆版本 #include<iostream> #include<algorithm> #include <vector> #include <queue> using namespace std; #define int long long #define x first #define y second typedef pair<int,int> PII; const int N = 550; vector<PII> v(N); int b[N], c[N]; priority_queue<int,vector<int>,greater<int> >q; bool cmp(PII &a, PII &b) { if( a.x != b.x ) return a.x < b.x; return a.y > b.y; } signed main() { int m, n, res = 0; cin >> m >> n; for(int i = 1; i <= n; i ++ ) cin >> v[i].x; for(int i = 1; i <= n; i ++ ) cin >> v[i].y; //sort(v.begin()+1, v.begin()+1+n); sort(v.begin()+1, v.begin()+1+n, cmp); int time = 1; for(int i = 1; i <= n; i ++ ) { if( time > v[i].x ) { if( q.top() < v[i].y ) { res += q.top(); q.pop(); q.push(v[i].y); } else res += v[i].y; } else time ++, q.push(v[i].y); } cout << m - res; }
Information
- ID
- 94
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 3
- Tags
- # Submissions
- 95
- Accepted
- 52
- Uploaded By