1. 程式人生 > >gym101522 [小熊騎士限定]La Salle-Pui Ching Programming Challenge 培正喇沙編程挑戰賽 2017

gym101522 [小熊騎士限定]La Salle-Pui Ching Programming Challenge 培正喇沙編程挑戰賽 2017

char ron math cto cms efi col -i stream

西瓜隊(劃掉),Kuma Rider久違的第一場訓練,四小時瞎打.jpg

A.水題,排序

技術分享圖片
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<functional>
#include<vector>
using
namespace std; typedef long long ll; #define lowbit(i) ((i)&(-i)) const ll maxv = 1e5 + 5; ll n,d[maxv], c[maxv]; void update(ll x, ll v) { for (ll i = x; i < maxv; i+=lowbit(i)) { c[i] += v; } } ll getsum(ll x) { ll sum = 0; for (ll i = x; i > 0
; i -= lowbit(i)) { sum += c[i]; } return sum; } int main() { scanf("%lld", &n); for (ll i = 1; i <= n; i++) { scanf("%lld", &d[i]); } sort(d+1 , d+1 + n); ll ans = 0; for (ll i = 1; i <= n; i++) { ll num
= getsum(i-1); num = (i - 1) - num; //printf("%lld %lld\n", i, num); ans += num; update(d[i], 1); } ans *= 2; printf("%lld\n", ans); }
View Code

B.找到樹的直徑,倍增地跳躍計算答案

技術分享圖片
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<functional>
#include<vector>
using namespace std;
typedef long long LL;


int n;
vector<int>e[500005];
bool vis[500005];
int Root, Tail;

int BFS(int st)
{
    for (int i = 1; i <= n; i++)vis[i] = false;
    queue<int>q;
    q.push(st);
    vis[st] = true;
    int ret = st;
    while (!q.empty())
    {
        int t = q.front(); q.pop();
        ret = t;
        for (int i = 0; i < e[t].size(); i++)
        {
            int to = e[t][i];
            if (!vis[to])
            {
                q.push(to);
                vis[to] = true;
            }
        }
    }
    return ret;
}

int len;

void DFS(int nx, int pr, int st)
{
    len = max(len, st);
    for (int i = 0; i < e[nx].size(); i++)
    {
        int to = e[nx][i];
        if (to != pr)
        {
            DFS(to, nx, st + 1);
        }
    }
}

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n - 1; i++)
    {
        int u, v;
        scanf("%d %d", &u, &v);
        e[u].push_back(v);
        e[v].push_back(u);
    }
    Root = BFS(1);
    Tail = BFS(Root);
    len = 0;
    DFS(Root, -1, 1);
    //printf("%d  %d\n", Root, Tail);
    int ans = 0;
    int tl = 1;
    while (tl<len-1)
    {
        ans++;
        tl <<= 1;
    }
    printf("%d\n", ans);

}
View Code

C.水題

技術分享圖片
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
#include<string>
#include<vector>
#include<map>
using namespace std;



char s[10005];
int main()
{
    scanf("%s", s);
    int len = strlen(s);
    int lsc = 0, pcms = 0;
    for (int i = 0; i < len-2; i++)
    {
        if (s[i] == L&&s[i + 1] == S&&s[i + 2] == C)lsc++;
    }
    for (int i = 0; i < len - 3; i++)
    {
        if (s[i] == P&&s[i + 1] == C&&s[i + 2] == M&&s[i + 3] == S)pcms++;
    }
    if (pcms > lsc)printf("PCMS\n");
    else if (pcms < lsc)printf("LSC\n");
    else printf("Tie\n");


}
View Code

D.沒來得及寫的水題,待施工

E.還沒看,待補

F.還沒寫,待補

G.還沒寫,待補

H.幾何板子題,兩圓交點,我抄錯板子錯了好久quq

技術分享圖片
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<map>
#include<functional>
#include<vector>
using namespace std;
const double eps = 1e-5;

struct Point
{
    double x, y;
    Point(double _a, double _b) :x(_a), y(_b) {}
    Point() {}
};

Point intersection(const Point &u1, const Point &u2, const Point &v1, const Point &v2)
{
    Point res = u1;
    double t = ((u1.x - v1.x)*(v1.y - v2.y) - (u1.y - v1.y)*(v1.x - v2.x)) / ((u1.x - u2.x)*(v1.y - v2.y) - (u1.y - u2.y)*(v1.x - v2.x));
    res.x += (u2.x - u1.x)*t;
    res.y += (u2.y - u1.y)*t;
    return res;
}

double dis(const Point &p1, const Point &p2)
{
    return sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
}

void play(const Point &c, double r, const Point &l1, const Point &l2, Point &p1, Point &p2)
{
    Point p = c;
    p.x += l1.y - l2.y;
    p.y += l2.x - l1.x;
    p = intersection(p, c, l1, l2);
    double t = sqrt(r*r - dis(p, c)*dis(p, c)) / dis(l1, l2);
    p1.x = p.x + (l2.x - l1.x)*t;
    p1.y = p.y + (l2.y - l1.y)*t;
    p2.x = p.x - (l2.x - l1.x)*t;
    p2.y = p.y - (l2.y - l1.y)*t;
}

void play2(const Point &c1, double r1, const Point &c2, double r2, Point &p1, Point &p2)
{
    Point u, v;
    double t = (1 + (r1*r1 - r2*r2) / dis(c1, c2) / dis(c1, c2)) / 2;
    u.x = c1.x + (c2.x - c1.x)*t;
    u.y = c1.y + (c2.y - c1.y)*t;
    v.x = u.x + c1.y - c2.y;
    v.y = u.y - c1.x + c2.x;
    play(c1, r1, u, v, p1, p2);
}

double x1, x2, r1, yy, y2, r2;

int main()
{
    cin >> x1 >> yy >> r1;
    cin >> x2 >> y2 >> r2;
    Point ans1, ans2;
    if (r1 > r2) {
        swap(x1, x2);
        swap(yy, y2);
        swap(r1, r2);
    }
    double ds = dis(Point(x1, yy), Point(x2, y2));
    if (r2 >= ds)
    {
        printf("%.6lf %.6lf\n", x1, yy);
        return 0;
    }
    play2(Point(x1, yy), r1, Point(x2, y2), r2, ans1, ans2);
    printf("%.6lf %.6lf\n", ans1.x, ans1.y);
    return 0;
}
View Code

I.瞎搞

技術分享圖片
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<map>
#include<functional>
#include<vector>
using namespace std;
typedef long long ll;
const double eps = 1e-5;
ll ax[1001000];
int main(void) {
    ll n;
    scanf("%lld", &n);
    for (int i = 0; i < n; i++) {
        scanf("%lld", &ax[i]);
    }
    if (n == 1) {
        printf("0\n");
        return 0;
    }
    else if (n == 2) {
        printf("%lld\n", min(abs(ax[1] - ax[0]),abs(ax[1]+ax[0])));
        return 0;
    }
    vector<ll>v;
    ll sum = 0;
    for (int i = 0; i<n - 1; i++) {
        sum += abs(ax[i + 1] - ax[i]);
        v.push_back(abs(ax[i + 1] + ax[i]) - abs(ax[i + 1] - ax[i]));
    }
    sort(v.begin(), v.end());
    if (v[0] < 0) {
        sum += v[0];
    }
    if (v[1] < 0) {
        sum += v[1];
    }
    printf("%lld\n", sum);
}
View Code

J.還沒寫,待補

K.水題,四個角滿足即可

技術分享圖片
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<functional>
#include<vector>
using namespace std;
typedef long long LL;

int n;

int main()
{
    int n, m, k;
    int cnt = 0;
    scanf("%d %d %d", &n, &m, &k);
    for (int i = 1; i <= k; i++)
    {
        int x, y;
        scanf("%d %d", &x, &y);
        if (x == 1 && y == m)
            cnt++;
        else if (x == 1 && y == 1)
            cnt++;
        else if (x == n&&y == 1)
            cnt++;
        else if (x == n&&y == m)
            cnt++;
    }
    int tmp = 4;
    if (n == 1 && m == 1)
        tmp = 1;
    else if (n == 1 || m == 1)
        tmp = 2;
    printf("%d\n", tmp - cnt);
    return 0;
}
View Code

L.還沒寫,待補

gym101522 [小熊騎士限定]La Salle-Pui Ching Programming Challenge 培正喇沙編程挑戰賽 2017