2017 ACM-ICPC 亚洲区(南宁赛区)网络赛

本文解析了两个算法问题:一是确定满足所有乘客预订需求的火车最少座位数;二是计算多个矩形重叠区域的总面积。通过具体实例展示了如何使用扫描线算法解决这些问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

02Train Seats Reservation

问答

只看题面
  •  33.87%
  •  1000ms
  •  131072K

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 3030 to 6060.

Input Format

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 nnreservations; 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.

Output Format

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
*
题目来源

2017 ACM-ICPC 亚洲区(南宁赛区)网络赛

这个B是我的暴力扫描线做得不太好,我承认,我是个sb,本来卡long long,后来卡扫描线,可以先下后上嘛,所以是左闭右开区间

#include <bits/stdc++.h>
using namespace std;
const int N=1005;
int n,a[N],b[N];
int main()
{
    while(~scanf("%d",&n))
    {
        if(!n)
        {
            printf("*\n");
            break;
        }
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        for(int i=0; i<n; i++)
        {
            int l,r,x;
            scanf("%d%d%d",&l,&r,&x);
            a[l]+=x;
            b[r]+=x;
        }
        long long f=0,ma=0;
        for(int i=1; i<=100; i++)
        {
            f-=b[i];
            f+=a[i];
            ma=max(f,ma);
        }
        printf("%lld\n",ma);
    }
    return 0;
}
    1. Overlapping Rectangles

问答

只看题面
  •  37.67%
  •  1000ms
  •  131072K

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.

Input Format

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><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.

Output Format

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
*
题目来源

2017 ACM-ICPC 亚洲区(南宁赛区)网络赛

这个F网上直接有代码啊,直接拉过看来就行的,暴力扫描线肯定不行。线段树搞一下

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define ls i<<1
#define rs i<<1|1
#define m(i) ((q[i].l + q[i].r)>>1)
using namespace std;
typedef long long ll;
const int N = 1001;
struct Edge
{
    double l,r;
    double h;
    int f;
}e[N<<1];
bool cmp(Edge a,Edge b)
{
    return a.h < b.h;
}
struct Node
{
    int l,r;
    int s;
    double len;
}q[N*8];
double x[2*N];
void build(int i,int l,int r)
{
    q[i].l = l,q[i].r = r;
    q[i].s = 0;q[i].len = 0;
    if (l == r) return;
    int mid = m(i);
    build(ls,l,mid);
    build(rs,mid+1,r);
}
void pushup(int i)
{
    if (q[i].s)
    {
        q[i].len = x[q[i].r+1] - x[q[i].l];
    }
    else if (q[i].l == q[i].r)
    {
        q[i].len = 0;
    }
    else
    {
        q[i].len = q[ls].len + q[rs].len;
    }
}
void update(int i,int l,int r,int xx)
{ 
    if (q[i].l == l&&q[i].r == r)
    {
        q[i].s += xx;
        pushup(i);
        return;
    }
    int mid = m(i);
    if (r <= mid) update(ls,l,r,xx);
    else if (l > mid) update(rs,l,r,xx);
    else
    {
        update(ls,l,mid,xx);
        update(rs,mid+1,r,xx);
    }
    pushup(i);
}
int main()
{
    int n;int kas = 0;
    while (scanf("%d",&n),n)
    {
        int tot = 0;
        for (int i = 0;i < n;++i)
        {
            double x1,x2,y1,y2;
            scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
            Edge &t1 = e[tot];Edge &t2 = e[1+tot];
            t1.l = t2.l = x1,t1.r = t2.r = x2;
            t1.h = y1;t1.f = 1;
            t2.h = y2;t2.f = -1;
            x[tot] = x1;x[tot+1] = x2;
            tot += 2;
        }
        sort(e,e+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(1,0,k-1);
        double ans = 0.0;
        for (int i = 0;i < tot;++i)
        {
            int l = lower_bound(x,x+k,e[i].l) - x;
            int r = lower_bound(x,x+k,e[i].r) - x - 1;
            update(1,l,r,e[i].f);
            ans += (e[i+1].h - e[i].h)*q[1].len;
        }
        printf("%.0f\n",ans);
    }
    puts("*");
}
    1. Minimum Distance in a Star Graph

问答

只看题面
  •  41.93%
  •  1000ms
  •  262144K

In this problem, we will define a graph called star graph, and the question is to find the minimum distance between two given nodes in the star graph.

Given an integer nn, an n-dimensionalndimensionalstar graph, also referred to as S_{n}Sn​​, is an undirected graph consisting of n!n! nodes (or vertices) and ((n-1)\ *\ n!)/2((n1)  n!)/2 edges. Each node is uniquely assigned a label x_{1}\ x_{2}\ ...\ x_{n}x1​​ x2​​ ... xn​​which is any permutation of the n digits {1, 2, 3, ..., n}1,2,3,...,n. For instance, an S_{4}S4​​ has the following 24 nodes {1234, 1243, 1324, 1342, 1423, 1432, 2134, 2143, 2314, 2341, 2413, 2431, 3124, 3142, 3214, 3241, 3412, 3421, 4123, 4132, 4213, 4231, 4312, 4321}1234,1243,1324,1342,1423,1432,2134,2143,2314,2341,2413,2431,3124,3142,3214,3

转载于:https://www.cnblogs.com/BobHuang/p/7591375.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值