B. Train Seats Reservation
You are given a list of train stations, say from the station 11 to the station 100100.
The passengers can order several tickets from one station to another before the train leaves the station one. We will issue one train from the station 11 to the station 100100 after all reservations have been made. Write a program to determine the minimum number of seats required for all passengers so that all reservations are satisfied without any conflict.
Note that one single seat can be used by several passengers as long as there are no conflicts between them. For example, a passenger from station 11 to station 1010 can share a seat with another passenger from station 3030to 6060.
Several sets of ticket reservations. The inputs are a list of integers. Within each set, the first integer (in a single line) represents the number of orders, nn, which can be as large as 10001000. After nn, there will be nn lines representing the nn reservations; each line contains three integers s, t, ks,t,k, which means that the reservation needs kk seats from the station ss to the station tt .These ticket reservations occur repetitively in the input as the pattern described above. An integer n = 0n=0 (zero) signifies the end of input.
For each set of ticket reservations appeared in the input, calculate the minimum number of seats required so that all reservations are satisfied without conflicts. Output a single star '*' to signify the end of outputs.
样例输入
2
1 10 8
20 50 20
3
2 30 5
20 80 20
40 90 40
0
样例输出
20
60
*
#include
#include
#include
#define LL long long
using namespace std;
struct node{
LL s,t,k;
}p[1010];
int n;
bool cmp(node A, node B){
if(A.t == B.t) return A.s < B.s;
return A.t < B.t;
}
int main(){
while(~scanf("%d",&n)){
if(n == 0){
printf("*\n"); break;
}
for(int i = 0; i < n; i ++){
scanf("%lld%lld%lld",&p[i].s,&p[i].t,&p[i].k);
}
sort(p,p+n,cmp);
LL mxx = -1;
for(int i = 0; i < n; i ++){
LL sum = p[i].k,in=p[i].t;
for(int j = i + 1; j < n; j ++){
if(in > p[j].s){
sum += p[j].k;
}
}
if(sum > mxx) mxx = sum;
}
printf("%lld\n",mxx);
}
return 0;
}
F. Overlapping Rectangles
There are nn rectangles on the plane. The problem is to find the area of the union of these rectangles. Note that these rectangles might overlap with each other, and the overlapped areas of these rectangles shall not be counted more than once. For example, given a rectangle AA with the bottom left corner located at (0, 0)(0,0) and the top right corner at (2, 2)(2,2), and the other rectangle BB with the bottom left corner located at (1,1)(1,1) and the top right corner at (3,3)(3,3), it follows that the area of the union of AA and BB should be 77, instead of 88.
Although the problem looks simple at the first glance, it might take a while to figure out how to do it correctly. Note that the shape of the union can be very complicated, and the intersected areas can be overlapped by more than two rectangles.
Note:
(1) The coordinates of these rectangles are given in integers. So you do not have to worry about the floating point round-off errors. However, these integers can be as large as 1,000,0001,000,000.
(2) To make the problem easier, you do not have to worry about the sum of the areas exceeding the long integer precision. That is, you can assume that the total area does not result in integer overflow.
Several sets of rectangles configurations. The inputs are a list of integers. Within each set, the first integer (in a single line) represents the number of rectangles, n, which can be as large as 10001000. After n, there will be n lines representing the n rectangles; each line contains four integers <a,b,c,d> , which means that the bottom left corner of the rectangle is located at (a, b)(a,b), and the top right corner of the rectangle is located at (c, d)(c,d). Note that integers aa, bb, cc, dd can be as large as 1,000,0001,000,000.
These configurations of rectangles occur repetitively in the input as the pattern described above. An integer n = 0n=0 (zero) signifies the end of input.
For each set of the rectangles configurations appeared in the input, calculate the total area of the union of the rectangles. Again, these rectangles might overlap each other, and the intersecting areas of these rectangles can only be counted once. Output a single star '*' to signify the end of outputs.
样例输入
2
0 0 2 2
1 1 3 3
3
0 0 1 1
2 2 3 3
4 4 5 5
0
样例输出
7
3
*
线段树扫描法求面积并
#include
#include
#include
#define LL long long
#define ls rt << 1
#define rs rt << 1 | 1
using namespace std;
const int maxn = 1010;
struct LINE{
LL l,r,h;
int flag;
}line[maxn<<3];
struct node{
LL l,r,len;
int flag;
}p[maxn<<3];
LL x[maxn<<3];
int n,tot;
bool cmp(LINE A, LINE B){
return A.h < B.h;
}
void build(int l, int r, int rt){
p[rt].l = l; p[rt].r = r;
p[rt].len = p[rt].flag = 0;
if(l == r) return;
int m = (l + r) >> 1;
build(l,m,ls);
build(m+1,r,rs);
}
void pushup(int rt){
if(p[rt].flag) p[rt].len = x[p[rt].r + 1] - x[p[rt].l];//1*********
else if(p[rt].l == p[rt].r) p[rt].len = 0;
else p[rt].len = p[ls].len + p[rs].len;
}
void update(int l,int r, int rt, int flag){
if(p[rt].l == l && p[rt].r == r){
p[rt].flag += flag;
pushup(rt); //很重要
return ;
}
int m = (p[rt].l + p[rt].r) >> 1;
if(r <= m) update(l,r,ls,flag);
else if(l > m) update(l,r,rs,flag);
else{
update(l,m,ls,flag);
update(m+1,r,rs,flag);
}
pushup(rt);
}
int main(){
while(~scanf("%d",&n)){
if(n == 0){
printf("*\n"); break;
}
tot = 0;
for(int i = 0; i < n; i ++){
LL x1,x2,y1,y2;
scanf("%lld%lld%lld%lld",&x1,&y1,&x2,&y2);
line[tot].l = line[tot + 1].l = x1;
line[tot].r = line[tot + 1].r = x2;
line[tot].h = y1; line[tot + 1].h = y2;
x[tot] = x1; x[tot + 1] = x2;
line[tot].flag = 1; line[tot + 1].flag = -1;
tot += 2;
}
sort(line,line+tot,cmp);
sort(x,x+tot);
int k = 1;
for(int i = 1; i < tot; i ++){
if(x[i] != x[i-1]) x[k ++] = x[i];
}
build(0,k-1,1);
LL ans = 0;
for(int i = 0; i < tot; i ++){
int l = lower_bound(x,x+k,line[i].l) - x;
int r = lower_bound(x,x+k,line[i].r) - x - 1; //2****
/*这里求得区间必须是前闭后开的区间,对于普通线段树来说,更新[1,2]和[2,3]的时候
2会加2,但对于更新一条线段来说,则只需要增加1即可*/
update(l,r,1,line[i].flag);
ans += (line[i+1].h - line[i].h) * p[1].len;
}
printf("%lld\n",ans);
}
return 0;
}
L. The Heaviest Non-decreasing Subsequence Problem
Let SS be a sequence of integers s_{1}s1, s_{2}s2, ......, s_{n}sn Each integer is is associated with a weight by the following rules:
(1) If is is negative, then its weight is 00.
(2) If is is greater than or equal to 1000010000, then its weight is 55. Furthermore, the real integer value of s_{i}si is s_{i}-10000si−10000 . For example, if s_{i}si is 1010110101, then is is reset to 101101 and its weight is 55.
(3) Otherwise, its weight is 11.
A non-decreasing subsequence of SS is a subsequence s_{i1}si1, s_{i2}si2, ......, s_{ik}sik, with i_{1}
A heaviest non-decreasing subsequence of SS is a non-decreasing subsequence with the maximum sum of weights.
Write a program that reads a sequence of integers, and outputs the weight of its
heaviest non-decreasing subsequence. For example, given the following sequence:
8080 7575 7373 9393 7373 7373 1010110101 9797 -1−1 -1−1 114114 -1−1 1011310113 118118
The heaviest non-decreasing subsequence of the sequence is <73, 73, 73, 101, 113, 118><73,73,73,101,113,118> with the total weight being 1+1+1+5+5+1 = 141+1+1+5+5+1=14. Therefore, your program should output 1414 in this example.
We guarantee that the length of the sequence does not exceed 2*10^{5}2∗105
A list of integers separated by blanks:s_{1}s1, s_{2}s2,......,s_{n}sn
A positive integer that is the weight of the heaviest non-decreasing subsequence.
样例输入
80 75 73 93 73 73 10101 97 -1 -1 114 -1 10113 118
样例输出
14
去掉价值为0的,把价值为5的拆分成5个数即可,注意数组的范围,要开到*5倍
#include
#include
#include
using namespace std;
const int maxn = 1e6 + 10;
int k,a[maxn],dp[maxn];
int main(){
k = 1;
while(~scanf("%d",&a[k])){
if(a[k] >= 10000){
a[k] -= 10000;
for(int i = 1; i < 5; i ++){
a[k+i] = a[k];
}
k += 4;
}
else if(a[k] < 0) k --;
k ++;
}
memset(dp,0,sizeof(dp));
dp[1] = a[1];
int len = 1;
for(int i = 2; i < k; i ++){
if(dp[len] <= a[i]) dp[++ len] = a[i];
else{
int pos = lower_bound(dp+1,dp+len+1,a[i]) - dp;
while(dp[pos] <= a[i]) pos ++;
dp[pos] = a[i];
}
}
if(dp[len] < 0 && len==1) printf("0\n");
else printf("%d\n",len);
return 0;
}