美的 Java笔试
1.略
2.有n棵树要修剪 每棵树原高度Hi应该修剪到(pi,qi),你一次可以修剪区间(i,j)-1高度,求最少修剪次数(卡 LL
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
vector>arr(n, vector(3));
for (int i = 0; i < arr.size(); i++)
{
cin >> arr[i][0] >> arr[i][1] >> arr[i][2];
}
vector>p(arr.size());
for (int i = 0; i < arr.size(); i++)
{
p[i].first = arr[i][2] - arr[i][1];
p[i].second = arr[i][2] - arr[i][0];
}
long long ans = 0;
int cutp = 0;
cutp = p[0].first;
ans += p[0].first;
for (int i = 0; i < arr.size()-1; i++)
{
if (p[i + 1].first>cutp) {
ans += p[i + 1].first-cutp;
cutp = p[i + 1].first;
}
if (p[i + 1].second < cutp)
{
cutp = p[i + 1].second;
}
}
cout << ans << endl;
},
3.异位字符串
哈希表秒了
2.有n棵树要修剪 每棵树原高度Hi应该修剪到(pi,qi),你一次可以修剪区间(i,j)-1高度,求最少修剪次数(卡 LL
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
vector>arr(n, vector(3));
for (int i = 0; i < arr.size(); i++)
{
cin >> arr[i][0] >> arr[i][1] >> arr[i][2];
}
vector>p(arr.size());
for (int i = 0; i < arr.size(); i++)
{
p[i].first = arr[i][2] - arr[i][1];
p[i].second = arr[i][2] - arr[i][0];
}
long long ans = 0;
int cutp = 0;
cutp = p[0].first;
ans += p[0].first;
for (int i = 0; i < arr.size()-1; i++)
{
if (p[i + 1].first>cutp) {
ans += p[i + 1].first-cutp;
cutp = p[i + 1].first;
}
if (p[i + 1].second < cutp)
{
cutp = p[i + 1].second;
}
}
cout << ans << endl;
},
3.异位字符串
哈希表秒了
全部评论
第二题力扣有类似的吗
相关推荐