错误程序,思路正确,但始终WA:
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<queue>
#include<stack>
#include<set>
#include<map>
using namespace std;
typedef long long LL;
const double pi = acos(-1.0);
const int maxn = 4e4;
int kase, n, m, last, a[maxn];
LL ans;
LL dp[maxn], money[maxn];
vector<pair<int,int> > lis[maxn];
struct earns
{
int date, salary, sum;
} earn[maxn + 1];
struct trades
{
int start, finish, rate;
} trade[maxn + 1];
map<int, int> mp;
void init();
void solve();
void outp();
int main()
{
int T;
while (~scanf("%d", &T))
{
kase = 0;
while (T--)
{
init();
solve();
outp();
}
}
return 0;
}
void solve()
{
for (int i = last-1; i >= 0; i--)
{
dp[i] = dp[i + 1];
for (int j = 0; j < lis[i].size(); j++)
{
//dp[i] = max(dp[i], dp[lis[i][j].first] + lis[i][j].second);
dp[i] = max(dp[i], dp[lis[i][j].first] + mp[lis[i][j].first]);
//cout<<i<<' '<<dp[i]<<' '<<dp[lis[i][j]]+mp[lis[i][j]]<<' '<<k<<endl;
}
//cout<<i<<' '<<dp[i]<<' '<<money[i]<<' '<<k<<endl;
}
for (int i = 0; i < last; i++)
{
ans += dp[i] * money[i];
}
}
void init()
{
last = 0;
earn[0].sum = 0;
ans = 0;
memset(a, 0, sizeof(a));
memset(dp, 0, sizeof(dp));
memset(money, 0, sizeof(money));
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
{
scanf("%d%d", &earn[i].date, &earn[i].salary);
a[last++] = earn[i].date;
}
for (int i = 1; i <= m; i++)
{
scanf("%d%d%d", &trade[i].start, &trade[i].finish, &trade[i].rate);
a[last++] = trade[i].start;
a[last++] = trade[i].finish;
}
sort(a, a + last);
last = unique(a, a + last) - a;
for (int i = 1; i <= n; i++)
{
earn[i].date = upper_bound(a, a + last, earn[i].date) - a;
//earn[i].sum=earn[i-1].sum+earn[i].salary;
}
for (int i = 1; i <= n; i++)
{
money[earn[i].date] += earn[i].salary;
}
for (int i = 0; i <= last; i++)
{
lis[i].clear();
}
for (int i = 1; i <= m; i++)
{
trade[i].start = upper_bound(a, a + last, trade[i].start) - a;
trade[i].finish = upper_bound(a, a + last, trade[i].finish) - a;
lis[trade[i].start].push_back(make_pair(trade[i].finish,trade[i].rate));
mp[trade[i].finish] = trade[i].rate;
//cout<<trade[i].finish<<' '<<mp[trade[i].finish]<<endl;
}
//last--;
}
void outp()
{
printf("Case #%d:\n%.2lf\n", ++kase, ans / 100.0);
/*for(int i=1;i<=n;i++) printf("%d %d\n",earn[i].date,earn[i].sum);
for(int i=1;i<=m;i++) printf("%d %d %d\n",trade[i].start,trade[i].finish,trade[i].rate);
puts("");*/
//for(int i=1;i<=n;i++) printf("%d %d\n",earn[i].date,money[earn[i].date]);
/*for(int i=1;i<=n;i++)
printf("%d ",dp[i]);
puts("");
for(int i=0;i<last;i++)
printf("%d ",a[i]);
puts("");
cout<<last<<endl;
puts("");*/
}
问题在于,map容器在数据的插入上涉及到集合的唯一性。map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能。
以数组的方式对其中元素进行赋值,可能会原有的映射关系。