月度归档: 2019 年 6 月

[CF4D]Mysterious Present 题解

[CF4D]Mysterious Present 题解

题目地址:Codeforces:Problem – 4D – Codeforces、洛谷:CF4D Mysterious Present – 洛谷 | 计算机科学教育新生态

题目描述

Peter decided to wish happy birthday to his friend from Australia and send him a card. To make his present more mysterious, he decided to make a chain. Chain here is such a sequence of envelopes A = {a1,  a2,  …,  an}, where the width and the height of the i-th envelope is strictly higher than the width and the height of the (i  -  1)-th envelope respectively. Chain size is the number of envelopes in the chain.

Peter wants to make the chain of the maximum size from the envelopes he has, the chain should be such, that he’ll be able to put a card into it. The card fits into the chain if its width and height is lower than the width and the height of the smallest envelope in the chain respectively. It’s forbidden to turn the card and the envelopes.

Peter has very many envelopes and very little time, this hard task is entrusted to you.

题意简述

给定$n$个矩形,长宽分别为$w_i, h_i$。定义矩形的严格递增序列为一个序列$A = \{a_1, a_2, a_3, \dots, a_m\}$满足$w_{a_1} < w_{a_2} < w_{a_3} < \cdots < w_{a_m}, h_{a_1} < h_{a_2} < h_{a_3} < \cdots < h_{a_m}$。请从给定矩形中选出满足$w_i > w, h_i > h$的一些矩形,组成一个最长的严格递增序列。如果找不出任何符合条件的序列,则输出0。

输入输出格式

输入格式:
The first line contains integers n, w, h (1  ≤ n ≤ 5000, 1 ≤ w,  h  ≤ 10^6) — amount of envelopes Peter has, the card width and height respectively. Then there follow n lines, each of them contains two integer numbers wi and hi — width and height of the i-th envelope (1 ≤ wi,  hi ≤ 10^6).

输出格式:
In the first line print the maximum chain size. In the second line print the numbers of the envelopes (separated by space), forming the required chain, starting with the number of the smallest envelope. Remember, please, that the card should fit into the smallest envelope. If the chain of maximum size is not unique, print any of the answers.

If the card does not fit into any of the envelopes, print number 0 in the single line.

输入输出样例

输入样例#1:

2 1 1
2 2
2 2

输出样例#1:

1
1 

输入样例#2:

3 3 3
5 4
12 11
9 8

输出样例#2:

3
1 3 2 

题解

一眼过去,LIS的即视感,而且数据范围提示$O(n^2)$的普通LIS能跑过,那应该就是它了。
考虑到严格递增矩形序列一定是长宽都递增,那么按长为第一关键字,宽为第二关键字排序后直接在新序列上做LIS即可。

LIS的$O(n^2)$DP算法如下:
对于每一个位置$i$,向前找满足$w_j < w_i, h_j < h_i$的位置,并取$dp(j)$最大的位置,完成转移,即
$$ dp(i) = \max_{w_j < w_i, h_j < h_i} \{dp(j)\} + 1 $$

代码

// Code by KSkun, 2019/6
#include <cstdio>
#include <cctype>

#include <algorithm>
#include <vector>

typedef long long LL;

inline char fgc() {
    static char buf[100000], *p1 = buf, *p2 = buf;
    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
        ? EOF : *p1++;
}

inline LL readint() {
    LL res = 0, neg = 1; char c = fgc();
    for(; !isdigit(c); c = fgc()) if(c == '-') neg = -1;
    for(; isdigit(c); c = fgc()) res = res * 10 + c - '0';
    return res * neg;
}

inline char readsingle() {
    char c;
    while(!isgraph(c = fgc())) {}
    return c;
}

const int MAXN = 5005;

int n, w, h, dp[MAXN], pre[MAXN];
std::vector<int> ans;

struct Node {
    int w, h, no;
    bool operator<(const Node &rhs) const {
        return w == rhs.w ? h < rhs.h : w < rhs.w;
    }
} env[MAXN];

int main() {
    n = readint(); w = readint(); h = readint();
    for(int i = 1; i <= n; i++) {
        env[i].w = readint(); env[i].h = readint();
        env[i].no = i;
    }
    std::sort(env + 1, env + n + 1);
    for(int i = 1; i <= n; i++) {
        if(env[i].w <= w || env[i].h <= h) continue;
        int mxn = 0;
        for(int j = 1; j < i; j++) {
            if(env[j].w <= w || env[j].h <= h) continue;
            if(env[i].w > env[j].w && env[i].h > env[j].h && dp[j] > dp[mxn]) {
                mxn = j;
            }
        }
        dp[i] = dp[mxn] + 1; pre[i] = mxn;
    }
    int mxn = 0;
    for(int i = 1; i <= n; i++) {
        if(env[i].w <= w || env[i].h <= h) continue;
        if(dp[i] > dp[mxn]) mxn = i;
    }
    if(mxn == 0) {
        puts("0"); return 0;
    }
    printf("%d\n", dp[mxn]);
    int t = mxn;
    while(t != 0) {
        ans.push_back(env[t].no);
        t = pre[t];
    }
    std::reverse(ans.begin(), ans.end());
    for(int i = 0; i < ans.size(); i++) {
        printf("%d ", ans[i]);
    }
    return 0;
}
[CF3B]Lorry 题解

[CF3B]Lorry 题解

题目地址:Codeforces:Problem – 3B – Codeforces、洛谷:CF3B Lorry – 洛谷 | 计算机科学教育新生态

题目描述

A group of tourists is going to kayak and catamaran tour. A rented lorry has arrived to the boat depot to take kayaks and catamarans to the point of departure. It’s known that all kayaks are of the same size (and each of them occupies the space of 1 cubic metre), and all catamarans are of the same size, but two times bigger than kayaks (and occupy the space of 2 cubic metres).

Each waterborne vehicle has a particular carrying capacity, and it should be noted that waterborne vehicles that look the same can have different carrying capacities. Knowing the truck body volume and the list of waterborne vehicles in the boat depot (for each one its type and carrying capacity are known), find out such set of vehicles that can be taken in the lorry, and that has the maximum total carrying capacity. The truck body volume of the lorry can be used effectively, that is to say you can always put into the lorry a waterborne vehicle that occupies the space not exceeding the free space left in the truck body.

题意简述

有一辆能够装载容量为$v$的货车,共有$n$个货物待装。货物分为两种类型,类型1所占容量为1,类型2所占容量为2,同种类型的货物的价值可能不同。求货车最多可以装载货物的最大价值之和。

输入输出格式

输入格式:
The first line contains a pair of integer numbers n and v (1 ≤ n ≤ 10^5; 1 ≤ v ≤ 10^9), where n is the number of waterborne vehicles in the boat depot, and v is the truck body volume of the lorry in cubic metres. The following n lines contain the information about the waterborne vehicles, that is a pair of numbers ti, pi (1 ≤ ti ≤ 2; 1 ≤ pi ≤ 10^4), where ti is the vehicle type (1 – a kayak, 2 – a catamaran), and pi is its carrying capacity. The waterborne vehicles are enumerated in order of their appearance in the input file.

输出格式:
In the first line print the maximum possible carrying capacity of the set. In the second line print a string consisting of the numbers of the vehicles that make the optimal set. If the answer is not unique, print any of them.

输入输出样例

输入样例#1:

3 2
1 2
2 7
1 3

输出样例#1:

7
2

题解

首先看到这个题就有一种强烈的直觉,即按单位容量的价值(即所谓的“性价比”)排序,先从大到小贪心地装,此时会遇到三种情况:货物的总占用容量小于货车容量,或者货车容量刚好用完,或者还剩1没有用。
对于第一种情况,直接全装进去就完事了。
对于第二种情况,直接按贪心的方案输出即可,因为根据贪心的思路,用单位价值较低的货物替换只会让方案变得更劣。
对于第三种情况,有两种选择:用未装进去的占容量为1的货物填这个空,或者把前面的某个占容量为1的货物替换为一个占容量为2的货物。第一个选择中,填空的货物肯定是未装之中价值最大的那个,而第二个选择中,肯定是用价值最大的2货物替换价值最小的1货物,这些都可以贪心地做。判断一下哪种选择更优即可。
思路比较好想,不过细节比较多,代码稍微有点难度。

代码

// Code by KSkun, 2019/6
#include <cstdio>
#include <cctype>

#include <algorithm>

typedef long long LL;

inline char fgc() {
    static char buf[100000], *p1 = buf, *p2 = buf;
    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
        ? EOF : *p1++;
}

inline LL readint() {
    LL res = 0, neg = 1; char c = fgc();
    for(; !isdigit(c); c = fgc()) if(c == '-') neg = -1;
    for(; isdigit(c); c = fgc()) res = res * 10 + c - '0';
    return res * neg;
}

inline char readsingle() {
    char c;
    while(!isgraph(c = fgc())) {}
    return c;
}

const int MAXN = 100005;

int n, v;

struct Node {
    int no, t, p;
    bool operator>(const Node &rhs) const {
        return double(p) / t > double(rhs.p) / rhs.t;
    }
} a[MAXN];

int main() {
    n = readint(); v = readint();
    for(int i = 1; i <= n; i++) {
        a[i].t = readint();
        a[i].p = readint();
        a[i].no = i;
    }
    std::sort(a + 1, a + n + 1, std::greater<Node>());
    int mn1 = 1e9, mn1n = 0, sum = 0, i;
    for(i = 1; i <= n; i++) {
        if(v < a[i].t) break;
        if(a[i].t == 1 && a[i].p < mn1) {
            mn1 = a[i].p; mn1n = i;
        }
        v -= a[i].t;
        sum += a[i].p;
    }
    int mx1 = 0, mx1n = 0, mx2 = 0, mx2n = 0;
    for(int j = i; j <= n; j++) {
        if(a[j].t == 1 && a[j].p > mx1) {
            mx1 = a[j].p; mx1n = j;
        }
        if(a[j].t == 2 && a[j].p > mx2) {
            mx2 = a[j].p; mx2n = j;
        }
    }
    if(v == 0 || (i == n && v >= a[n].t)) {
        printf("%d\n", sum);
        for(int j = 1; j < i; j++) printf("%d ", a[j].no);
    } else {
        int ans1 = 0, ans2 = 0;
        if(mx1 != 0) ans1 = sum + mx1;
        if(mx2 != 0) ans2 = sum - mn1 + mx2;
        int ans = std::max(ans1, ans2);
        ans = std::max(ans, sum);
        printf("%d\n", ans);
        for(int j = 1; j < i; j++) {
            if(ans != sum && ans != ans1 && ans == ans2 && j == mn1n) continue;
            printf("%d ", a[j].no);
        }
        if(ans != sum && ans == ans1) printf("%d ", a[mx1n].no);
        if(ans != sum && ans != ans1 && ans == ans2) printf("%d ", a[mx2n].no);
    }
    return 0;
}
[CF1C]Ancient Berland Circus 题解

[CF1C]Ancient Berland Circus 题解

题目地址:Codeforces:Problem – 1C – Codeforces、洛谷:CF1C Ancient Berland Circus – 洛谷 | 计算机科学教育新生态

题目描述

Nowadays all circuses in Berland have a round arena with diameter 13 meters, but in the past things were different.

In Ancient Berland arenas in circuses were shaped as a regular (equiangular) polygon, the size and the number of angles could vary from one circus to another. In each corner of the arena there was a special pillar, and the rope strung between the pillars marked the arena edges.

Recently the scientists from Berland have discovered the remains of the ancient circus arena. They found only three pillars, the others were destroyed by the time.

You are given the coordinates of these three pillars. Find out what is the smallest area that the arena could have.

题意简述

给你一个正多边形中某三个点的坐标,求符合条件的最小正多边形的面积。保证答案中的正多边形边数不超过100。

输入输出格式

输入格式:
The input file consists of three lines, each of them contains a pair of numbers –– coordinates of the pillar. Any coordinate doesn’t exceed 1000 by absolute value, and is given with at most six digits after decimal point.

输出格式:
Output the smallest possible area of the ancient arena. This number should be accurate to at least 6 digits after the decimal point. It’s guaranteed that the number of angles in the optimal polygon is not larger than 100.

输入输出样例

输入样例#1:

0.000000 0.000000
1.000000 1.000000
0.000000 1.000000

输出样例#1:

1.00000000

题解

给了三个点,正好能构成一个三角形。这个三角形肯定是正多边形的一部分,接下来考虑如何通过三角形内部的信息求出正多边形的信息。
考虑这个三角形的外接圆即是正多边形的外接圆,可以先把外接圆半径通过正弦定理求出来,即$\frac{a}{\sin A} = 2R \Rightarrow R = \frac{a}{2 \sin A}$。
有了外接圆半径后,我们可以求出三条边在外接圆中对着的圆心角,这可以通过余弦定理求出,即$cos \theta_a = \frac{2r^2-a^2}{2r^2} \Rightarrow \theta_a = \arccos(1-\frac{a^2}{2r^2})$。
考虑枚举多边形的边数,从而计算出多边形每条边对的圆心角,通过判断三条边对的圆心角是否是这个多边形圆心角的整数倍即可判断这三个点是否可以是这个正多边形的三个顶点。选择边数最少的正多边形即可求出最小的面积。
面积的求法,考虑把正$n$边形分成以外接圆圆形为顶点的$n$个小三角形,每个三角形的面积是$\frac{1}{2} R^2 \sin\frac{2\pi}{n}$,乘上边数$n$就得到了总面积。

理论上到此为止这个题已经做完了,但是你会发现直接这么写会WA。接下来是我在写的时候踩的坑,仅供读者参考:

  1. 操作中精度误差比较大,EPS设置$10^{-6}$太小,需要调整到$10^{-4}$。
  2. 如果直接利用余弦定理计算圆心角,容易遇到拿去算$\arccos$的值因为精度误差产生越界值,例如$-1$因为精度误差变成了$-1.00000000003$。考虑到三角形中边对的内角一定小于$\pi$,不会产生越界值,可以余弦定理算内角之后,利用内角是圆周角的性质,乘以2算出圆心角。这样做可以避免圆心角计算出一个nan的情况。

作为高考之后写的第一个练手题,这个题实在坑人,写了一下午2333

代码

// Code by KSkun, 2019/6
#include <cstdio>
#include <cmath>

#include <algorithm>

const double EPS = 1e-4;
const double PI = acos(-1);

inline int dcmp(double x) {
    if(fabs(x) < EPS) return 0;
    else return x < 0 ? -1 : 1;
}

struct Vector {
    double x, y;
    Vector operator+(const Vector &rhs) const {
        return Vector {x + rhs.x, y + rhs.y};
    }
    Vector operator-(const Vector &rhs) const {
        return Vector {x - rhs.x, y - rhs.y};
    }
};

inline double dot(Vector a, Vector b) {
    return a.x * b.x + a.y * b.y;
}

inline double length(Vector x) {
    return sqrt(dot(x, x));
}

inline double angle(Vector a, Vector b) {
    return acos(dot(a, b) / length(a) / length(b));
}

typedef Vector Point;

Point a, b, c;

inline bool checkInt(double x) {
    return dcmp(x - round(x)) == 0;
}

int main() {
    scanf("%lf%lf%lf%lf%lf%lf", &a.x, &a.y, &b.x, &b.y, &c.x, &c.y);
    double al = length(b - c), bl = length(a - c), cl = length(a - b),
        r = cl / sin(angle(a - c, b - c)) / 2;
    double aan = acos((bl * bl + cl * cl - al * al) / 2 / bl / cl) * 2, 
        ban = acos((al * al + cl * cl - bl * bl) / 2 / al / cl) * 2,
        can = 2 * PI - aan - ban;
    for(int i = 3; i <= 100; i++) {
        double in = PI * 2 / i, c1 = aan / in, c2 = ban / in, c3 = can / in;
        if(!checkInt(c1) || !checkInt(c2) || !checkInt(c3) || round(c1 + c2 + c3) != i) continue;
        double s = r * r * sin(in) / 2 * i;
        printf("%.8lf", s); break;
    }
    return 0;
}