Codeforces Round 1024 (Div. 2)

A. Dinner Time

只有 n%p==0 && q*(n/p)!=m 时为"NO"。

#include <bits/stdc++.h>
#define x first
#define y second
#define int long long

using namespace std;
typedef unsigned long long ULL ;
typedef pair<int,int> PII ;
typedef pair<long double,long double> PDD ;
const int N = 1200010 , M = N * 2 , mod = 998244353 ;
int n , m , p , q ;

void solve()
{
    cin >> n >> m >> p >> q ;
    int x = n / p , y = n % p ;
    if((y == 0 && x * q == m) || (y != 0)) cout << "YES\n" ;
    else cout << "NO\n" ;
}
signed main()
{
    std::ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0) ;
    int t = 1 ;
    cin >> t ;
    while (t --) solve() ;
    return 0;
}
B. The Picky Cat

全部看作正数,如果a[1]能够成为中值数,有两种情况。

1.大于a[1]的数大于n/2个,这样将多余的数设置为负数。

2.大于a[1]的数大于(n+1)/2-1个,拿出这部分数和a[1]设置为负数。

#include <bits/stdc++.h>
#define x first
#define y second
#define int long long

using namespace std;
typedef unsigned long long ULL ;
typedef pair<int,int> PII ;
typedef pair<long double,long double> PDD ;
const int N = 100010 , M = N * 2 , mod = 998244353 ;
int n ;
int a[N] ;

void solve()
{
    cin >> n ;
    for(int i = 1 ; i <= n ; i ++)
        cin >> a[i] ;
    int sa = 0 , sb = 0 ;
    for(int i = 2 ; i <= n ; i ++) {
        if(abs(a[i]) > abs(a[1])) sa ++ ;
    }
    if(sa >= n / 2 || sa >= (n + 1) / 2 - 1) cout << "YES\n" ;
    else cout << "NO\n" ;
}
signed main()
{
    std::ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0) ;
    int t = 1 ;
    cin >> t ;
    while (t --) solve() ;
    return 0;
}
C. Mex in the Grid

最中间填0,剩下数字按照蛇形填入。(写了一个很暴力的模拟,不知道有没有更好的方法。)

蛇形填入顺序,左,下,右,上。每转两次方向,步数加1。

#include <bits/stdc++.h>
#define x first
#define y second
#define int long long

using namespace std;
typedef unsigned long long ULL ;
typedef pair<int,int> PII ;
typedef pair<long double,long double> PDD ;
const int N = 510 , M = N * 2 , mod = 998244353 ;
int n ;
int a[N][N] ;
int dx[4] = {0 , 1 , 0 , -1} , dy[4] = {1 , 0 , -1 , 0} ;

void solve()
{
    cin >> n ;
    int c = 1 , x = (n + 1) / 2 , y = (n + 1) / 2 , d = 0 , u = 1 , pu = 0 , cd = 0;
    a[x][y] = 0 ;
    if(n % 2) d = 3 ;

    while (true) {
        x += dx[d] , y += dy[d] ;
        if(x >= 1 && x <= n && y >= 1 && y <= n) {
            a[x][y] = c ++ ;
            pu ++ ;
            if(pu == u) {
                d = (d + 1) % 4 ;
                pu = 0 ;
                cd ++ ;
            }
            if(cd == 2) {
                u ++ ;
                cd = 0 ;
            }
        }
        else break ;
        if(c == n * n) break;
    }

    for(int i = 1 ; i <= n ; i ++) {
        for(int j = 1 ; j <= n ; j ++)
            cout << a[i][j] << " " ;
        cout << "\n" ;
    }
}
signed main()
{
    std::ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0) ;
    int t = 1 ;
    cin >> t ;
    while (t --) solve() ;
    return 0;
}
D. Quartet Swapping

这题不要想当然直接奇数位偶数位分别排序,每次四个数是需要同时操作的,可以从逆序对的角度去考虑,奇偶位置逆序对的相对性是不变的。如果都是奇数个或偶数个逆序对,直接排序即可,否则就交换a[n],a[n-2],代价最小。

#include <bits/stdc++.h>
#define x first
#define y second
#define int long long

using namespace std;
typedef unsigned long long ULL ;
typedef pair<int,int> PII ;
typedef pair<long double,long double> PDD ;
const int N = 200010 , M = N * 2 , mod = 998244353 ;
int n ;
int c[N] , tr[N] ;

int lowbit(int x) {
    return x & -x ;
}
void update(int x) {
    for(int i = x ; i <= n ; i += lowbit(i))
        tr[i] ++ ;
}
int query(int x) {
    int res = 0 ;
    for(int i = x ; i ; i -= lowbit(i))
        res += tr[i] ;
    return res ;
}
int sum(vector<int> a) {
    for(int i = 0 ; i <= n ; i ++)
        tr[i] = 0 ;
    int res = 0 ;
    for(int i = a.size() - 1 ; i >= 0 ; i --) {
        res += query(a[i]) ;
        update(a[i]) ;
    }
    return res ;
}
void solve()
{
    cin >> n ;
    for(int i = 1 ; i <= n ; i ++)
        cin >> c[i] ;
    vector<int> a , b ;
    for(int i = 1 ; i <= n ; i ++) {
        if(i % 2) a.push_back(c[i]) ;
        else b.push_back(c[i]) ;
    }
    int sa = sum(a) , sb = sum(b) ;
    sort(a.begin() , a.end()) , sort(b.begin() , b.end()) ;
    for(int i = 1 ; i <= n ; i ++) {
        if(i % 2) c[i] = a[i / 2] ;
        else c[i] = b[(i / 2) - 1] ;
    }
    if((sa % 2) != (sb % 2)) swap(c[n] , c[n - 2]) ;

    for(int i = 1 ; i <= n ; i ++)
        cout << c[i] << " " ;
    cout << "\n" ;
}
signed main()
{
    std::ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0) ;
    int t = 1 ;
    cin >> t ;
    while (t --) solve() ;
    return 0;
}

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安特尼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值