# 贪心基础题目整理

1.钓鱼（简化移动时间）

#include <queue>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
{
int x = 0, c = getchar();
while(c < '0' || c > '9') c = getchar();
while(c >= '0' && c <= '9') x = x * 10 + c- '0', c = getchar();
return x;
}
struct pool {
int fish, id;
bool operator < (pool other) const {
return fish < other.fish;
}
}a[30];
priority_queue<pool>q;
int n, h, d[30], t[30], ans;
int main()
{
for(int i = 1; i <= n; ++i) a[i].fish = read(), a[i].id = i;
for(int i = 1; i <= n; ++i) d[i] = read();
for(int i = 1; i < n; ++i) t[i] = read();
for(int i = 1; i <= n; ++i)
{
h -= t[i - 1];
while(!q.empty()) q.pop();
for(int j = 1; j <= i; ++j) q.push(a[j]);
int now = 0;
for(int j = 1; j <= h; ++j)
{
pool s = q.top();
if(s.fish > 0)
now += s.fish, s.fish -= d[s.id];
q.pop();
q.push(s);
}
ans = max(ans, now);
}
printf("%d\n", ans);
return 0;
}

2.可扩充背包

#include <vector>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
{
x = 0; int c = getchar();
while(c < '0' || c > '9') c = getchar();
while(c >= '0' && c <= '9') x = x * 10 + c - 48, c = getchar();
}
int n, T, v;
long long h;
struct thg { int a, b;};
vector<thg> A,B;
bool cp1(const thg &x, const thg &y) {return x.a < y.a;}
bool cp2(const thg &x, const thg &y) {return x.b > y.b;}
bool Jud()
{
for(int i = 0; i < A.size(); ++i)
{
if(h < A[i].a) return false;
h += A[i].b - A[i].a;
}
for(int i = 0; i < B.size(); ++i)
{
if(h < B[i].a) return false;
h += B[i].b - B[i].a;
}
return true;
}
int main()
{
freopen("backpark.in", "r", stdin), freopen("backpark.out", "w", stdout);
while(T--)
{
A.clear(), B.clear();
for(int i = 0; i < n; ++i)
{
if(x.a < x.b) A.push_back(x);
else B.push_back(x);
}
sort(A.begin(), A.end(), cp1);
sort(B.begin(), B.end(), cp2);
puts(Jud() ? "Yes": "No");
}
fclose(stdin), fclose(stdout);
return 0;
}

3.求数列的子序列和的前k大，降序输出

#include <set>
#include <vector>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
typedef long long LL;
using namespace std;
{
Type x = 0; int c = getchar();
while(c < '0' || c > '9') c = getchar();
while(c >= '0' && c <= '9') x = x * 10 + c - 48, c = getchar();
return x;
}
struct rng
{
int l, r;
bool operator < (const rng oth) const
{return (l == oth.l) ? (r < oth.r) : l < oth.l;}
bool operator == (const rng oth) const
{return l == oth.l && r == oth.r;}
};
struct node
{
int l, r;
LL sum;
bool operator < (const node oth) const
{return sum < oth.sum;}
bool operator == (const node oth) const
{return sum == oth.sum;}
}tmp;
set<rng> h;
vector<node> A;
int n, a[100001], K;
int main()
{
freopen("ksum.in", "r", stdin), freopen("ksum.out", "w", stdout);
tmp.l = 1, tmp.r = n;
for(int i = 1; i <= n; ++i) a[i] = read<int>(), tmp.sum += a[i];
A.push_back(tmp), h.insert((rng){1, n});
make_heap(A.begin(), A.end());
for(int i = 1; i <= K; ++i)
{
tmp = A.front(); pop_heap(A.begin(), A.end()), A.pop_back();
printf("%I64d ", tmp.sum);
int l = tmp.l, r = tmp.r - 1;
if(h.find((rng){l, r}) == h.end())
{
h.insert((rng){l, r});
A.push_back((node){l, r, tmp.sum - a[tmp.r]});
push_heap(A.begin(), A.end());
}
++l, ++r;
if(h.find((rng){l, r}) == h.end())
{
h.insert((rng){l, r});
A.push_back((node){l, r, tmp.sum - a[tmp.l]});
push_heap(A.begin(), A.end());
}
}
fclose(stdin), fclose(stdout);
return 0;
}

posted @ 2017-10-27 17:11  keshuqi  阅读(478)  评论(0编辑  收藏  举报