栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Codeforces1545 A. AquaMoon and Strange Sort(思维,排序、奇偶性)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Codeforces1545 A. AquaMoon and Strange Sort(思维,排序、奇偶性)

题意:

解法:
每次交换,下标奇偶性变化
要保证每个数最后,下标的奇偶性不变.
统计每个数排序之前的奇偶个数
统计每个数排序之后的奇偶个数
如果某个数的奇偶性不相同则无解

容易证明上述做法的正确性.
code:
#include
#define MULTI_CASE
#define PI pair
// #define int long long
using namespace std;
// const int mod=998244353;
const int mod=1e9+7;
const int maxm=2e6+5;
int a[maxm];
int n;

void solve(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    //每次交换,下标奇偶性变化
    //要保证每个数最后,下标的奇偶性不变.
    //统计每个数排序之前的奇偶个数
    //统计每个数排序之后的奇偶个数
    //如果某个数的奇偶性不相同则无解
    mapmp[2];
    for(int i=1;i<=n;i++){
        mp[i%2][a[i]]++;
    }
    sort(a+1,a+1+n);
    for(int i=1;i<=n;i++){
        mp[i%2][a[i]]--;
    }
    for(int x=0;x<2;x++){
        for(auto i:mp[x]){
            if(i.second){
                cout<<"NO"<>T;while(T--)
    #endif
    solve();
}
void Init(){
    ios::sync_with_stdio(0);cin.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("../in.txt","r",stdin);
    freopen("../out.txt","w",stdout);
    #endif
}
signed main(){
    Init();
    Main();
    return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/304473.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号