# 【noip模拟赛 王强的疑惑】 题解

50分可以通过子集枚举+线段覆盖（贪心）完成。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 20;
char ch = getchar(); int u = 0, f = 1;
while (!isdigit(ch)) {if (ch == '-')f = -1; ch = getchar();}
while (isdigit(ch)) {u = u * 10 + ch - 48; ch = getchar();}return u * f;
}
//int dp[maxn][maxn]...
struct ioi{
int l, r, num;
double p;
}b[maxn];
bool cmp(ioi a, ioi b)
{
if(a.l == b.l) return a.r < b.r;
return a.l < b.l;
}
int a[maxn], n, q, seq[maxn], last;
double now = 0, ans;
void print_subset(int s, int n)
{
memset(a, 0, sizeof(a));
for(int i = 1; i <= n; i++)
if(s & (1 << (i-1))) a[i] = 1;
}
int main()
{
freopen("math.in","r",stdin);
freopen("math.out","w",stdout);
for(int i = 1; i <= n; i++)
{
//cin>>b[i].l>>b[i].r>>b[i].p;
b[i].r += q; b[i].num = i;
}
sort(b+1, b+1+n, cmp);
for(int i = 1; i < (1 << n); i++)
{
print_subset(i, n);
now = 0; last = 0;
for(int j = 1; j <= n; j++)
{
if(a[j] == 1 && b[last].r <= b[j].l)
{
now += b[j].p;
last = j;
}
}
if(now > ans)
{
for(int j = 1; j <= n; j++)
seq[j] = a[j];
ans = now;
}
}
printf("%0.3lf\n",ans);
for(int i = 1; i <= n; i++)
{
if(seq[i] == 1)
printf("%d ", b[i].num);
}
return 0;
}


$if(a[j].r == i && a[j].l >= q)$

$DP[i] = max(DP[i], DP[a[j].l-q] + a[j].p)$

code：

#include <stack>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1e6 + 10;
char ch = getchar(); int u = 0, f = 1;
while (!isdigit(ch)) {if (ch == '-')f = -1; ch = getchar();}
while (isdigit(ch)) {u = u * 10 + ch - 48; ch = getchar();}return u * f;
}
double dp[maxn];
struct ioi{
int l, r, num;
double p;
}a[maxn];
int n, m, q, pos = 1, pre[maxn], ans[maxn], Ans[maxn], cnt;
bool cmp(ioi a, ioi b)
{
if(a.r != b.r)
return a.r < b.r;
else return a.l < b.l;
}
int main()
{
//freopen("math.in","r",stdin);
//freopen("math.out","w",stdout);
scanf("%d%d",&n,&q);
for(int i = 1; i <= n; i++)
{
scanf("%d%d%lf",&a[i].l,&a[i].r,&a[i].p);
a[i].num = i;
m = max(m, a[i].r);
}
sort(a+1, a+1+n, cmp);

for(int i = 1; i <= m; i++)
{
dp[i] = dp[i-1]; pre[i] = i - 1;
for(int j = pos; j <= n; j++)
{
if(a[j].r > i) break;
if(a[j].r == i)
{
if(a[j].l >= q && dp[i] < dp[a[j].l-q] + a[j].p)
{
dp[i] = dp[a[j].l-q] + a[j].p;
ans[i] = a[j].num;
pre[i] = a[j].l - q;
}
if(a[j].num == 1 && dp[i] < dp[a[j].l-q] + a[j].p)
{
dp[i] = dp[a[j].l-q] + a[j].p;
ans[i] = a[j].num;
pre[i] = 0;
}
pos = j;
}
}
}
int now = m;
while(now)
{
if(ans[now]) Ans[++cnt] = ans[now];
now = pre[now];
}
printf("%0.3lf\n",dp[m]);
for(int i = cnt; i >= 1; i--)
printf("%d ",Ans[i]);
return 0;
}



posted @ 2018-11-05 19:42  Misaka_Azusa  阅读(200)  评论(0编辑  收藏
Live2D