Another Birthday Present! CodeChef - SHUFFLE

传送门
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <deque>
#include <cstdlib>
#include <math.h>
#include <unordered_map>
#define lowbit(x) ((x) & -(x))
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
const int maxn = 1e5 + 7;
const int INF = 0x3f3f3f3f;
typedef long long ll;
using namespace std;
const ll mod = 1e9 + 7;
const double pi = acos(-1.0);
inline int read(){
    int x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9'){
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9'){
        x = (x<<1) + (x<<3) + (ch^48);
        ch = getchar();
    }
    return x * f;
}
struct node{
    int id, value;
}a[maxn];

bool cmp(node a, node b){
    return a.value < b.value;
}
bool cmp1(node a, node b){
    return a.value > b.value;
}
vector<int>v[maxn];
int vis[maxn];
int main(){
    //ios::sync_with_stdio(false);
    //freopen("text.txt","r",stdin);
    //freopen("out1.txt","w",stdout);
    int t, n, k;
    cin >> t;
    while (t --){
        n = read();
        k = read();
        set<int>S;
        S.clear();
        for (int i = 0; i < n; i ++){
            a[i].value = read();
            a[i].id = i;
            S.insert(a[i].value);
            v[i].clear();
            vis[i] = false;
        }
        set<int>::iterator it;
        int num = 0;
        unordered_map<int,int>mp;
        mp.clear();
        for (it = S.begin(); it != S.end(); it ++, num ++)
            mp[*it] = num;
        sort(a,a+n,cmp);
        for (int i = 0; i < n; i ++)
            v[mp[a[i].value]].push_back(i);
        int flag = 0;
        for (int i = 0; i < n; i ++){
            int x = mp[a[i].value];
            int len = v[x].size();
            for (int j = 0; j < len; j ++){
                if (abs(v[x][j] - a[i].id) % k == 0 && !vis[v[x][j]]){
                    vis[v[x][j]] = true;
                    flag ++;
                    break;
                }
            }
        }
        if (flag == n)
            cout << "yes" << endl;
        else {
            flag = 1;
            sort(a,a+n,cmp1);
            for (int i = 0; i < n; i ++)
                v[i].clear(), vis[i] = false;
            for (int i = 0; i < n; i ++)
                v[mp[a[i].value]].push_back(i);
            int flag = 0;
            for (int i = 0; i < n; i ++){
                int x = mp[a[i].value];
                int len = v[x].size();
                for (int j = 0; j < len; j ++){
                    if (abs(v[x][j] - a[i].id) % k == 0 && !vis[v[x][j]]){
                        vis[v[x][j]] = true;
                        flag ++;
                        break;
                    }
                }
            }
            if (flag == n)
                cout << "yes" << endl;
            else 
                cout << "no" << endl;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值