• 贪心


    https://ac.nowcoder.com/acm/contest/1126/K

    链接:https://ac.nowcoder.com/acm/contest/1126/K
    来源:牛客网

    上课的时候总有一些同学和前后左右的人交头接耳,这是令小学班主任十分头疼的一件事情。不过,班主任小雪发现了一些有趣的现象,当同学们的座次确定下来之后,只有有限的D对同学上课时会交头接耳。同学们在教室中坐成了M行N列,坐在第i行第j列的同学的位置是(i,j),为了方便同学们进出,在教室中设置了K条横向的通道,L条纵向的通道。于是,聪明的小雪想到了一个办法,或许可以减少上课时学生交头接耳的问题:她打算重新摆放桌椅,改变同学们桌椅间通道的位置,因为如果一条通道隔开了两个会交头接耳的同学,那么他们就不会交头接耳了。
    请你帮忙给小雪编写一个程序,给出最好的通道划分方案。在该方案下,上课时交头接耳的学生对数最少。

    输入描述:

    第一行,有5各用空格隔开的整数,分别是M,N,K,L,D(2 ≤ N,M ≤ 1000,0 ≤ K < M,0 ≤ L < N,D ≤ 2000)。
    接下来D行,每行有4个用空格隔开的整数,第i行的4个整数X

    i

    ,Y

    i

    ,P

    i

    ,Q

    i

    ,表示坐在位置(X

    i

    ,Y

    i

    )与(P

    i

    ,Q

    i

    )的两个同学会交头接耳(输入保证他们前后相邻或者左右相邻)。
    输入数据保证最优方案的唯一性。
     

    输出描述:

    第一行包含K个整数,a

    1

    a

    2

    ……a

    K

    ,表示第a

    1

    行和a

    1

    +1行之间、第a

    2

    行和第a

    2

    +1行之间、…、第a

    K

    行和第a

    K

    +1行之间要开辟通道,其中a

    < a

    i

    +1,每两个整数之间用空格隔开(行尾没有空格)。
    第二行包含L个整数,b

    1

    b

    2

    ……b

    k

    ,表示第b

    1

    列和b

    1

    +1列之间、第b

    2

    列和第b

    2

    +1列之间、…、第b

    L

    列和第b

    L

    +1列之间要开辟通道,其中b

    i

    < b

    i

    +1,每两个整数之间用空格隔开(行尾没有空格)。
    示例1

    输入

    复制
    4 5 1 2 3
    4 2 4 3
    2 3 3 3
    2 5 2 4

    输出

    复制
    2
    2 4

    说明

    上图中用符号*、※、+ 标出了3对会交头接耳的学生的位置,图中3条粗线的位置表示通道,图示的通道划分方案是唯一的最佳方案。
     
    //#include <bits/stdc++.h>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <iostream>
    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <stdio.h>
    #include <queue>
    #include <stack>;
    #include <map>
    #include <set>
    #include <string.h>
    #include <vector>
    #define ME(x , y) memset(x , y , sizeof(x))
    #define SF(n) scanf("%d" , &n)
    #define rep(i , n) for(int i = 0 ; i < n ; i ++)
    #define INF  0x3f3f3f3f
    #define mod 998244353
    #define PI acos(-1)
    using namespace std;
    typedef long long ll ;
    int mx[1009] , my[1009];
    
    struct node
    {
        int id , ans ;
    }ax[1009] , ay[1009];
    
    bool cmp(struct node a , struct node b)
    {
        return a.ans > b.ans;
    }
    
    int main()
    {
        int n , m , x , y , d ;
        scanf("%d%d%d%d%d" , &n , &m , &x , &y , &d);
    
        for(int i = 0 ; i < d ; i ++)
        {
            int x1 , y1 , x2 , y2 ;
            scanf("%d%d%d%d" , &x1 , &y1 , &x2 , &y2);
            if(x1 == x2)
            {
                int miny = min(y1 , y2);
                my[miny]++;
            }
            else if(y1 == y2)
            {
                int minx = min(x1 , x2);
                mx[minx]++;
            }
        }
        vector<int>v1 ;
        for(int i = 1 ; i <= n ; i++) ax[i] = {i , mx[i]};
        for(int i = 1 ; i <= m ; i++) ay[i] = {i , my[i]};
    
        sort(ax+1 , ax+n+1 , cmp);
        sort(ay+1 , ay+m+1 , cmp);
    
        for(int i = 1 ; i <= x ; i++)
        {
            v1.push_back(ax[i].id);
        }
        sort(v1.begin() , v1.end());
        vector<int>::iterator it;
        for(it = v1.begin() ; it != v1.end() - 1 ; it++)
            cout << *it << " "  ;
        cout << *it << endl;
        v1.clear();
        for(int i = 1 ; i <= y ; i++)
            v1.push_back(ay[i].id);
        sort(v1.begin() , v1.end());
    
        for(it = v1.begin() ; it != v1.end() - 1 ; it++)
            cout << *it << " " ;
        cout << *it << endl ;
    
        return 0 ;
    }
  • 相关阅读:
    More Effective C++: 02操作符
    More Effective C++: 01基础议题
    GCD学习(七) dispatch_apply
    GCD学习(六) dispatch_async 和dispatch_sync
    GCD学习(五) dispatch_barrier_async
    GCD 学习(四) dispatch_group
    关于 block的一些浅识
    异常日志记录 DDLog
    Effective Objective-C [下]
    Effective Objective-C [上]
  • 原文地址:https://www.cnblogs.com/nonames/p/11735815.html
Copyright © 2020-2023  润新知