区间问题
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
struct Node {
int l, r;
bool operator < (const Node &t ) const {
return r < t.r;
}
}segs[N];
int n;
int main() {
cin >> n;
int l, r;
for(int i=0; i<n; i++) {
cin >> l >> r;
segs[i] = {l, r};
}
sort(segs, segs+n);
int res=0, ed=-1e10;
for(int i=0; i<n; i++) {
if(segs[i].l > ed) {
res++;
ed = segs[i].r;
}
}
cout << res;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
struct seg{
int l, r;
bool operator < (const seg &t) const {
return r < t.r;
}
}segs[N];
int main() {
int n;
cin >> n;
for(int i=0; i<n; i++) {
int l, r;
cin >> l >> r;
segs[i] = {l ,r};
}
sort(segs, segs+n);
int res = 0, l = -2e9;
for(int i=0; i<n; i++) {
if(l < segs[i].l) {
res++;
l = segs[i].r;
}
}
cout << res;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
int n;
struct seg{
int l, r;
bool operator < (const seg &t) const {
return l < t.l;
}
}segs[N];
int main() {
cin >> n;
for(int i=0; i<n; i++) {
int l, r;
cin >> l >> r;
segs[i] = {l, r};
}
sort(segs, segs+n);
priority_queue<int, vector<int>, greater<int> > heap;
for(int i=0; i<n; i++) {
if(heap.empty() || heap.top() >= segs[i].l) heap.push(segs[i].r);
else {
heap.pop();
heap.push(segs[i].r);
}
}
cout << heap.size();
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
struct seg {
int l, r;
bool operator < (const seg &t) const {
return l < t.l;
}
}segs[N];
int n;
int main() {
int st, ed;
cin >> st >> ed >> n;
for(int i=0; i<n; i++) {
int l, r;
cin >> l >> r;
segs[i] = {l, r};
}
sort(segs, segs+n);
int res = 0;
bool success = false;
for(int i=0; i<n; i++) {
int j = i, r = -2e9;
while(j<n && segs[j].l <= st) {
r = max(r, segs[j].r);
j++;
}
if(r < st) {
res = -1;
break;
}
res++;
if(r>=ed) {
success = true;
break;
}
st = r;
i = j-1;
}
if(success) cout << res;
else cout << -1;
return 0;
}
Huffman树
#include <bits/stdc++.h>
using namespace std;
const int N = 10010;
int n;
int main() {
cin >> n;
priority_queue<int, vector<int>, greater<int> > heap;
while(n--) {
int x;
cin >> x;
heap.push(x);
}
int res = 0;
while(heap.size()>1) {
int a = heap.top(); heap.pop();
int b = heap.top(); heap.pop();
res += a + b;
heap.push(a+b);
}
cout << res;
return 0;
}
排序不等式
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e5+10;
int n;
int a[N];
int main() {
cin >> n;
for(int i=0; i<n; i++) scanf("%d", &a[i]);
sort(a, a+n);
LL res = 0;
for(int i=0; i<n; i++) res += a[i]*(n-i-1);
cout << res;
return 0;
}
绝对值不等式
推公式