FST是一名可怜的小朋友,他很强,但是经常fst,所以rating一直低迷。
但是重点在于,他非常适合ACM!并在最近的区域赛中获得了不错的成绩。
拿到奖金后FST决定买一台新笔记本,但是FST发现,在价格能承受的范围内,笔记本的内存和速度是不可兼得的。
可是,有一些笔记本是被另外一些“完虐”的,也就是内存和速度都不高于另外某一个笔记本,现在FST想统计一下有多少笔记本被“完虐”。
- 题意:给你n台电脑,接下来n行,每一行都会给出一个存储大小和一个速度。要求这些电脑中被完虐(内存速度都比另一个电脑小)的数量。
非线段树:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAX_SIZE = 1e5+5, INF = 0x3f3f3f3f;
struct Laptop
{
int speed, memory;
friend bool operator < (Laptop &source, Laptop &obj)
{
if(source.memory == obj.memory) return source.speed < obj.speed;
return source.memory < obj.memory;
}
}computer[MAX_SIZE];
int main()
{
int n, Fail_Num = 0;
cin >> n;
for(int i = 1; i <= n; i++) scanf("%d%d", &computer[i].memory, &computer[i].speed);
sort(computer+1, computer+1+n); //按照存储量进行排序
int Back_Max = computer[n].speed;
for(int i = n-1; i > 0; i--)
{
Back_Max = max(Back_Max, computer[i].speed);
if(computer[i].speed < Back_Max)
Fail_Num++;
}
cout << Fail_Num << endl;
return 0;
}
线段树:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAX_SIZE = 1e5+5, INF = 0x3f3f3f3f;
int tree[MAX_SIZE<<2];
struct Laptop
{
int speed, memory;
friend bool operator < (Laptop &source, Laptop &obj)
{
return source.memory < obj.memory;
}
}computer[MAX_SIZE];
inline void Push_Up(int Root)
{
tree[Root] = max(tree[Root<<1], tree[Root<<1|1]);
}
void Build(int left, int right, int Root)
{
if(left == right)
{
tree[Root] = computer[left].speed;
return ;
}
int mid = (left+right)>>1;
Build(left, mid, Root<<1);
Build(mid+1, right, Root<<1|1);
Push_Up(Root);
}
int Section_Query(int Left, int Right, int left, int right, int Root)
{
if(Left > Right || left > Right || right < Left) return -INF; //这里一定要判断一下,否则有可能出现段错误
if(Left <= left && Right >= right) return tree[Root];
int mid = (left+right)>>1;
return max(Section_Query(Left, Right, left, mid, Root<<1), Section_Query(Left, Right, mid+1, right, Root<<1|1));
}
int main()
{
int n, Fail_Num = 0;
cin >> n;
for(int i = 1; i <= n; i++) scanf("%d%d", &computer[i].memory, &computer[i].speed);
sort(computer+1, computer+1+n); //按照存储量进行排序
Build(1, n, 1); //按照CPU速度进行建树
for(int i = n-1; i > 0; i--)
{
int Back_Max = Section_Query(i, n, 1, n, 1); //找后续区间(包括自己)中的速度的最大值
//因为我们刚开始就是按照内存排的序,所以在后面的一定比在前面的内存大,接下来再看速度,如果后面包括自己在内没有比自己速度更快的
//说明后面的内存比这个大,但是速度都比这个慢,所以没有被完虐,反之说明这个电脑被完虐,答案数量加1
if(Back_Max > computer[i].speed)
Fail_Num++;
}
cout << Fail_Num << endl;
return 0;
}