#1280 : Rikka with Sequence II [meet int the middle]

Candy?發表於2017-04-30

tle棄療了
等著回學校找個標程拍一下吧

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
inline int read(){
    char c=getchar(); int x=0,f=1;
    while(c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
    return x*f;
}

int n, a[50], m, mid;
struct meow {
    int w; double v;
    meow() {}
    meow(int a, double b) : w(a), v(b) {}
    //bool operator <(const meow &a) const {return w == a.w ? v < a.v : w < a.w;}
} li[50];
vector<double> q1[50], q2[50];

void dfs(int u, int tar, int now_w, double now_v, vector<double> *q) { //printf("dfs %d %d\n", u, tar);
    if(u == tar || (u != 1 && u != mid+1)) q[now_w + 20].push_back(now_v);
    // printf("hi %d  %lf\n", now_w, now_v);
    if(u == tar) return;
    for(int i=u; i<tar; i++) dfs(i+1, tar, now_w + li[i].w, now_v + li[i].v, q);
}

int ans;
void solve(int l, int r) { //printf("\nsolve %d %d\n", l, r);
    int _ans = 0;
    m = 0; double aver = (double)(a[l] + a[r]) / 2.0;
    for(int i=1; i<l; i++) li[++m] = meow(-1, a[i] - aver);
    for(int i=r+1; i<=n; i++) li[++m] = meow(1,  a[i] - aver);
    //for(int i=1; i<=m; i++) printf("li %d  %d  %lf\n", i, li[i].w, li[i].v);
    mid = m>>1;
    for(int i=0; i<=40; i++) q1[i].clear(), q2[i].clear();
    //printf("mmmm %d  %d\n", m, mid);
    if(mid) dfs(1, mid+1, 0, 0, q1);
    if(m) dfs(mid+1, m+1, 0, 0, q2);
    for(int i=0; i<=40; i++) {
        int j = 40 - i;
        vector<double> &a = q1[i], &b = q2[j];
        if(a.empty() || b.empty()) continue;

        sort(a.begin(), a.end()); sort(b.begin(), b.end());
        //printf("now %d  %d\n", i-20, j-20);
        //for(int i=0; i<a.size(); i++) printf("%lf ", a[i]); puts("");
        //for(int i=0; i<b.size(); i++) printf("%lf ", b[i]); puts("");
        int p1 = 0, p2 = b.size() - 1;
        for(p1 = 0; p1 < a.size(); p1++) {
            while(p2 >= 0 && a[p1] + b[p2] > 0) p2 --;
        }
        _ans += p2 + 1;
    }
    _ans += q1[20].size() + q2[20].size();
    //printf("_ans %d\n", _ans);
    _ans++;
    ans += _ans;
}

int main() {
    freopen("in", "r", stdin);
    n = read();
    for(int i=1; i<=n; i++) a[i] = read();
    sort(a+1, a+1+n);
    for(int l=1; l<=n; l++) for(int r=l; r<=n; r++) solve(l, r);
    //solve(2, 2);
    printf("%d\n", ans);
}

相關文章