牛客多校第四场

D题

链接:https://www.nowcoder.com/acm/contest/142/D
来源:牛客网

手构矩阵找规律找的, 奇数是不可能的,偶数时,上三角矩阵全为-1,下三角矩阵全为1,斜线上1,0交替

#include <bits/stdc++.h>
using namespace std;
int n;
int main()
{
    int t;
    cin >> t;
    while(t --)
    {
        cin >> n;
        if(n&1) cout << "impossible" << endl;
        else
        {
            cout << "possible" << endl;
            for(int i = 1;i <= n;i ++)
            {
                for(int j = 1;j <= n;j ++)
                {
                    if(i == j) printf("%d%c",i&1,j == n? '\n': ' ');
                    else if(i < j) printf("%d%c",-1,j == n? '\n': ' ');
                    else printf("%d%c",1,j == n? '\n': ' ');
                }
                //cout << '\n';
            }
        }
    }
    return 0;
}

F题

链接:https://www.nowcoder.com/acm/contest/142/F
来源:牛客网

如果所以点都是完全对称的,那么总共有(n/2-1)*(m/2-1)种   

循环上三角矩阵,每个点找一变,如果有某个点不符合与它自己对称的三个点相同,那么更新最值

#include <bits/stdc++.h>
using namespace std;
char a[2018][2018];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,m;
        cin>>n>>m;
        for(int i=0;i<n;i++)
        {
            scanf("%s",a[i]);
        }
        int ansx=n/2-1,ansy=m/2-1;
        for(int i=0;i<n/2;i++)
        {
            for(int j=0;j<m/2;j++)
            {
                if(a[i][j]!=a[n-i-1][j]||a[i][j]!=a[n-i-1][m-j-1]||a[i][j]!=a[i][m-j-1])
                {
                    ansx=min(ansx,i);
                    ansy=min(ansy,j);
                }
            }
        }
        cout<<ansx*ansy<<endl;
    }
    return 0;
}

G题

链接:https://www.nowcoder.com/acm/contest/142/G
来源:牛客网
 

把数字出现的次数统计好,按照 次数降序,数字大小降序的排列方式排序,然后暴力枚举所有答案即可

(暴力不超时哟)

#include<bits/stdc++.h>
using namespace std;
map<int,int> mp;
vector<pair<int,int> > v;
int n, m;
int cmp(pair<int,int> a,pair<int,int> b)
{
    if(a.first != b.first)
        return a.first > b.first;
    else
        return a.second > b.second;
}
 
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
 
    int t;
    cin >> t;
    while(t --)
    {
        mp.clear();
        v.clear();
        cin >> n >>m;
        int k;
        for(int i = 1;i <= n;i ++)
        {
            cin >> k;
            mp[k] ++;
        }
 
        for(map<int,int>:: iterator it = mp.begin();it != mp.end();it ++)
        {
            pair<int,int> p(it->first,it->second);
            v.push_back(p);
        }
        sort(v.begin(),v.end(),cmp);
 
        //pair  fir表示数字  sec表示个数
        int res = -1;
        for(int i = 0;i < v.size();i ++)
        {
            int s = 0, t = v[i].first;
            for(int j = 0;j < v.size();j ++)
            {
                if(j == i) continue;
                
                if(v[j].second >= v[i].second) s += v[j].second - v[i].second + 1;
            }
            if(s <= m)
            {
                res = t;
                break;
            }
        }
        cout << res << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/soul_97/article/details/81268765