zoukankan      html  css  js  c++  java
  • NOI.AC 20181103 题解

    CF 1037B  Reach Median

     

    班上 n个同学(n 是奇数)排成一排站队,为了美观,需要大家高度的中位数是 x。

    你可以让同学们在脚下垫木板或者稍微蹲一点来达成这个目标。对任意一位同学的身高减少或者增加1的代价都是1。问

    你最少花费多少代价可以让最后大家高度的中位数是x。

    solution:
    基于一个贪心,现将序列排序。设当前的中位数为MID,位置是pos

    那么如果MID比x大,那么将MID=x计算代价,我们的中位数要左移然后就考虑将pos左侧所有小于x的数全部赋值为x计算代价

    如果MID比x小,那么将MID=x计算代价,我们中位数要右移就考虑将pos右侧所有大于x的数全部赋值为x计算代价

    然后输出答案即可。

     Code:

    # include <bits/stdc++.h>
    # define int long long
    using namespace std;
    const int MAXN=2e5+10;
    int a[MAXN],n,x;
    inline int read()
    {
        int X=0,w=0; char c=0;
        while (!(c>='0'&&c<='9')) w|=c=='-',c=getchar();
        while (c>='0'&&c<='9') X=(X<<1)+(X<<3)+(c^48),c=getchar();
        return w?-X:X; 
    }
    inline void write(int x)
    {
        if (x<0) { x=-x;putchar('-');}
        if (x>9) write(x/10);
        putchar('0'+x%10);
     } 
    inline void writeln(int x){write(x);putchar('
    ');}
    
    signed main()
    {
        n=read(); x=read();
        for (int i=1;i<=n;i++) a[i]=read();
        sort(a+1,a+1+n);
        int pos=(n+1)/2,ans=0;
        if (x<a[pos]) {
            for (int i=1;i<=pos;i++)
             if (a[i]>x) ans+=a[i]-x;
        }    else {
            for (int i=pos;i<=n;i++)
             if (a[i]<x) ans+=x-a[i];
        }
        writeln(ans);
        return 0;
    }

    CF 1037E  Reach Median

    题目描述

    There are nn persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.

    We want to plan a trip for every evening of mm days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:

    • Either this person does not go on the trip,
    • Or at least kk of his friends also go on the trip.

    Note that the friendship is not transitive. That is, if aa and bb are friends and bb and cc are friends, it does not necessarily imply that aa and cc are friends.

    For each day, find the maximum number of people that can go on the trip on that day.

    输入输出格式

    输入格式:

    The first line contains three integers nn , mm , and kk ( 2 leq n leq 2 cdot 10^5, 1 leq m leq 2 cdot 10^52n2105,1m2105 , 1 le k < n1k<n ) — the number of people, the number of days and the number of friends each person on the trip should have in the group.

    The ii -th ( 1 leq i leq m1im ) of the next mm lines contains two integers xx and yy ( 1leq x, yleq n1x,yn , x e yxy ), meaning that persons xx and yy become friends on the morning of day ii . It is guaranteed that xx and yy were not friends before.

    输出格式:

    Print exactly mm lines, where the ii -th of them ( 1leq ileq m1im ) contains the maximum number of people that can go on the trip on the evening of the day ii .

    输入输出样例

    输入样例#1: 复制
    4 4 2
    2 3
    1 2
    1 3
    1 4
    
    输出样例#1: 复制
    0
    0
    3
    3
    
    输入样例#2: 复制
    5 8 2
    2 1
    4 2
    5 4
    5 2
    4 3
    5 1
    4 1
    3 2
    
    输出样例#2: 复制
    0
    0
    0
    3
    3
    4
    4
    5
    
    输入样例#3: 复制
    5 7 2
    1 5
    3 2
    2 5
    3 4
    1 2
    5 3
    1 3
    
    输出样例#3: 复制
    0
    0
    0
    0
    3
    4
    4
    

    说明

    In the first example,

    • 1,2,31,2,3 can go on day 33 and 44 .

    In the second example,

    • 2,4,52,4,5 can go on day 44 and 55 .
    • 1,2,4,51,2,4,5 can go on day 66 and 77 .
    • 1,2,3,4,51,2,3,4,5 can go on day 88 .

    In the third example,

    • 1,2,51,2,5 can go on day 55 .
    • 1,2,3,51,2,3,5 can go on day 66 and 77 .

    一共有n个人,他们开始互不认识,而每天早上不认识的两个人会变成朋友。一共有m天,每天晚上有的人要去旅行,去旅行的人必须满足ta有至少k个朋友也去旅行

    求每天去旅行的最大人数

    输入格式:

    第一行3个整数,表示n,m,kn,m,k

    往下m行,每行两个整数x,yx,y,表示这天早上xx和yy会变成朋友

    输出格式:

    mm行,每行一个整数,表示每天晚上最多能去旅游的人数

    solution:

    考虑离线做首先维护一个du,即图上节点的入度如果入度小于k那么就删除这个节点然后维护删去这个点以后发生的事情(比如其他边被删去,其他点又被删去)

    这样维护就是一边bfs就行(dfs也行)

    具体来说就是先建好图吧最终的状态O(n)统计一遍然后,依次删边,删边的时候注意将du[u]--,du[v]--,如果du[u]或者du[v]等于0那么把这个点删去然后维护全局的答案

    然后由于这个点删去了,那么和这个点有关系的所有边都得删去了,然后又造成一部分点的 du 减少于是又得维护这些点的状态删去或者不删。。这就要bfs了。。。

    注意删过的点和边不用重复删,就是弄两个数组看看对应的点和边还在不在就行了。

    Code:

    # include <bits/stdc++.h>
    # define int long long
    using namespace std;
    const int MAXN=2e5+10;
    struct Edge{ int u,v;};
    struct A{ int node,id;};
    vector<A>E[MAXN];
    vector<Edge>R;
    int ret,n,m,k,du[MAXN];
    bool ok[MAXN],able[MAXN];
    inline int read()
    {
        int X=0,w=0; char c=0;
        while (!(c>='0'&&c<='9')) w|=c=='-',c=getchar();
        while (c>='0'&&c<='9') X=(X<<1)+(X<<3)+(c^48),c=getchar();
        return w?-X:X;
    }
    void del(int u)
    {
        if (ok[u]==false) return;
        ret--; ok[u]=false; du[u]=0;
        queue<int>q; q.push(u);
        while (!q.empty()) {
            int u=q.front();q.pop();
            for (int i=0;i<E[u].size();i++) {
                int v=E[u][i].node;
                if (ok[v]==false||able[E[u][i].id]==false) continue;
                du[v]--; 
                able[E[u][i].id]=false;
                if (du[v]<k) ok[v]=false,ret--,q.push(v); 
            }
        }
    } 
    void erase(int id)
    {
        if (able[id]==false) return;
        int x=R[id].u,y=R[id].v;
        able[id]=false;
        du[x]--;if (du[x]<k&&ok[x]) del(x);
        du[y]--;if (du[y]<k&&ok[y]) del(y); 
    }
    signed main()
    {
        n=read();m=read();k=read();
        for (int i=1;i<=m;i++) {
            int u=read(),v=read();
            E[u].push_back((A){v,i-1});
            E[v].push_back((A){u,i-1});
            du[u]++; du[v]++;
            R.push_back((Edge){u,v});
        }
        ret=n;
        memset(ok,true,sizeof(ok));
        memset(able,true,sizeof(able));
        for (int i=1;i<=n;i++) if (du[i]<k) del(i);
        stack<int>Ans;    
        for (int w=R.size()-1;w>=0;w--) {
            Ans.push(ret);
            erase(w);
        }
        while (!Ans.empty()) printf("%lld
    ",Ans.top()),Ans.pop();
        return 0;
    }

    运气大战

    你的班上nn个同学要去参加一项集体比赛。每个人有实力值和运气值。每个人的实力值是确定的,但是运气值是飘忽不定的。一个人的发挥是他的实力值wiwi 和运气值的乘积,即wi⋅rciwi⋅rci。班级的发挥是所有人发挥之和。每个人有一个初始运气值riri,但是每次比赛的时候,每个人的运气值是所有人运气值的一个排列,并且要满足,排列之后ii的运气值不是riri。即满足,ii的运气值是rcirci,{ci}{ci}是1−n1−n的排列,且满足ci≠ici≠i。

    现在有QQ场比赛,每场比赛前会交换两人的初始运气值。问你每场比赛班级可以获得的最大发挥是多少。

    输入格式

    第一行两个整数 nn 和 QQ。

    第二行nn个整数w1,...,wnw1,...,wn。

    第三行nn个整数r1,...,rnr1,...,rn。

    接下来QQ行,每行两个整数ai,biai,bi表示每次要交换运气值的两个人的编号。

    输出格式

    QQ行,每行一个答案,表示这场比赛班级的最大发挥。

    样例1

    输入

    5 5
    25 1 16 26 19 
    11 27 4 8 20 
    1 5
    1 5
    2 5
    1 2
    5 4

    输出

    1527
    1543
    1543
    1536
    1536

    样例2

    输入

    50 50
    1913 135 637 739 1617 550 1918 688 1022 111 182 1127 1603 1315 36 1530 988 921 804 1856 767 747 556 413 588 395 1193 425 1205 168 453 1671 1813 251 1916 666 971 1651 1772 1877 792 63 980 740 1097 1523 765 1069 699 177
    666 130 1955 1869 1271 1827 1762 833 1830 1521 1751 16 1260 907 792 1524 1732 780 122 1083 1651 1702 11 384 1664 1921 429 1388 615 1325 1988 15 1096 1607 1181 330 755 865 257 1959 1152 365 1826 851 355 359 1745 989 1179 877
    43 44
    10 32
    18 38
    19 44
    17 27
    12 35
    38 49
    6 49
    6 44
    26 42
    44 7
    8 29
    45 36
    25 4
    39 4
    26 44
    40 37
    8 28
    17 19
    39 11
    13 15
    14 34
    47 3
    44 6
    40 48
    26 37
    4 12
    40 9
    40 16
    6 34
    38 39
    31 13
    19 41
    8 3
    7 20
    39 47
    2 25
    12 38
    9 47
    35 28
    46 21
    9 26
    26 10
    28 23
    8 33
    10 37
    23 13
    48 49
    19 8
    10 24
    View Code

    输出

    68387076
    68387076
    68387076
    68387076
    68387076
    68387076
    68387076
    68387076
    68387076
    68387076
    68387076
    68387274
    68387274
    68387274
    68387274
    68387274
    68387274
    68387274
    68387274
    68387274
    68387274
    68387274
    68387274
    68387274
    68387274
    68387274
    68387274
    68387274
    68387274
    68387274
    68387274
    68387274
    68386602
    68386602
    68386602
    68386602
    68386602
    68386602
    68386414
    68386414
    68386414
    68386414
    68386414
    68386414
    68386414
    68386414
    68386414
    68386414
    68387086
    68387086
    View Code

    数据范围

    20% n10,q10n≤10,q≤10

    另30% n1000,q100n≤1000,q≤100

    另20% n30000,q500n≤30000,q≤500

    100% 2n30000,1q10000,1wi,ri1e6,1aibin

    本题时限:5s

    由于排序不等式,我们尽量想顺序放。两边都排序。

    由于 n 个不能配的干扰,又不能完全顺序放。

    有个结论,最后匹配出第 i 个人的运气值是第 j 个的话,|i-j|le2ij2。这个结论从最小化逆序对的个数来看,自己把附近几个线连起来画一画证明一下。

    这样就可以用 dp[i]表示到 i 为止所有配好的最优答案。计算的时候需要用到前三轮的答案然后讨论一下。这个是 O(nq)的,可以过70%。

    用线段树记录区间答案。区间记录这样的信息:把这个区间前0-2个和后0-2个元素去掉的答案,用3x3的矩阵维护。这样复杂度是O(qlogn)。

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <cstring>
    #include <cstdlib>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    #define A first
    #define B second
    typedef long long ll;
    
    int n,q;
    vector<pair<int,int> > L, R;
    int whereL[30013], whereR[30013];
    ll cost[30013][2];
    ll dp[30013];
    
    const ll INF = -3.1e16;
    
    inline ll match(int a, int b) {
        if (a<=0 || b<=0 || a>n || b>n) return INF;
        return (ll) L[a].A*R[b].A;
    }
    
    inline void update(int i) {
        if (i<=0 || i>n) return;
        cost[i][0] = match(i,i+1)+match(i+1,i);
        cost[i][1] = max(match(i,i+1)+match(i+1,i+2)+match(i+2,i),match(i,i+2)+match(i+1,i)+match(i+2,i+1));
    }
    
    int main() {
        scanf("%d%d",&n,&q);
        for (int i=0;i<=n;i++) {
            L.push_back(make_pair(0,i));
            R.push_back(make_pair(0,i));
        }
        for (int i=1;i<=n;i++) scanf("%d",&L[i].A);
        for (int i=1;i<=n;i++) scanf("%d",&R[i].A);
        sort(L.begin(),L.end());
        sort(R.begin(),R.end());
        for (int i=1;i<=n;i++) {
            whereL[L[i].B] = i;
            whereR[R[i].B] = i;
        }
        for (int i=1;i<=n;i++) update(i);
        for (int Q=0;Q<q;Q++) {
            int a,b;
            scanf("%d%d",&a,&b);
            swap(R[whereR[a]].B,R[whereR[b]].B);
            swap(whereR[a],whereR[b]);
            for (int i=-2;i<=0;i++) {
                update(whereL[a]+i);
                update(whereR[a]+i);
                update(whereL[b]+i);
                update(whereR[b]+i);
            }
            dp[n+1] = 0;
            for (int i=n;i;i--) {
                dp[i] = max(dp[i+2]+cost[i][0],dp[i+3]+cost[i][1]);
                if (L[i].B!=R[i].B) dp[i] = max(dp[i],(ll) L[i].A*R[i].A+dp[i+1]);
            }
            printf("%lld
    ",dp[1]);
        }
    
        return 0;
    }
  • 相关阅读:
    Markdown
    DNS解析流程
    maven 的各种命令
    ES6初体验——(1)let和const命令
    table相关的选择器 & children()与find()的区别 & 选择器eq(n)与nth-child(n)的差异
    Java MD5加密类
    POI操作Excel异常Cannot get a text value from a numeric cell
    MyEclipse+SSH开发环境配置
    JdbcTemplate详解
    Spring配置声明
  • 原文地址:https://www.cnblogs.com/ljc20020730/p/9900925.html
Copyright © 2011-2022 走看看