考虑某一种状态,无论如何调整卡片位置,都不会减少逆序对数量,这就是我们要找的最优解。

显然在对于一个颜色的数字有序时,达到了上述状态。

于是,我们根据一个颜色的值排序后再计算逆序对就得到了答案。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
typedef pair<int,int> card; const int maxn=100000+1;
card c1[maxn],c2[maxn];
bool cmp(const card &a,const card &b)
{
return a.first<b.first|| (a.first==b.first&&a.second<b.second);
} bool cmp1(card a,card b)
{
return a.second<b.second;
}
void merges(vector<card> & cc,int l,int r,long long int &ans)
{
if(l==r)return;
if(l==r-1)
if(cmp1(cc[r],cc[l])){swap(cc[r],cc[l]);ans++;return ;}
else return ;
int mid=(l+r)/2;
vector<card>left,right;
for(int i=l;i<=mid;i++)left.push_back(cc[i]);
for(int i=mid+1;i<=r;i++)right.push_back(cc[i]);
merges(left,0,mid,ans);merges(right,0,r-mid-1,ans);
int l1=0,l2=0,len=0;
while(l1<left.size()&&l2<right.size())
{
if(cmp1(right[l2],left[l1])){cc[len++]=right[l2++];ans+=left.size()-l1;}
else{cc[len++]=left[l1++];}
}
while(l1<left.size())
{
cc[len++]=left[l1++];
}
while(l2<right.size())
{
cc[len++]=right[l2++];
}
} int main()
{freopen("john.in","r",stdin);
//freopen("john.out","w",stdout);
long long int ans1=0,ans2=0;
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d%d",&c1[i].first,&c1[i].second);
c2[i].first=c1[i].second;
c2[i].second=c1[i].first;
}
sort(c1,c1+n,cmp);
sort(c2,c2+n,cmp);
vector<card> s1,s2;
for(int i=0;i<n;i++)
{
s1.push_back(c1[i]);
s2.push_back(c2[i]);
}
merges(s1,0,n-1,ans1);
merges(s2,0,n-1,ans2);
printf("%lld\n",min(ans1,ans2));
return 0;
}

  

2011–2012, Northern Subregional J. John’s Inversions的更多相关文章

  1. 2010–2011, NEERC, Northern Subregional C.Commuting Functions

    C.Commuting Functions 由于要求答案字典序最小,我们肯定希望从g(1)开始对函数g进行赋值,于是又公式f(g(x))=g(f(x)) 设f(x)=i 我们推导出 由于f是双射,当i ...

  2. [SinGuLaRiTy] COCI 2011~2012 #2

    [SinGuLaRiTy-1008] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 测试题目 对于所有的题目:Time Limit:1s   ...

  3. 模拟赛小结:2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest

    2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest 2019年10月11日 15:35-20:35(Solved 8,Penalty 675 ...

  4. 2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest (9/12)

    $$2015-2016\ ACM-ICPC,\ NEERC,\ Northern\ Subregional\ Contest$$ \(A.Alex\ Origami\ Squares\) 签到 //# ...

  5. 【2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest D】---暑假三校训练

    2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest D Problem D. Distribution in Metagonia Input ...

  6. VRay 2.0 SP1 2.10.01 for 3ds max 9/2008/2009/2010/2011/2012 32/64位 顶渲简体中文版+英文版[中国室内设计论坛-室内人]

    VRay 2.0 SP1 2.10.01 for 3ds max 9/2008/2009/2010/2011/2012 32/64位 顶渲简体中文版+英文版[中国室内设计论坛-室内人] 对最新版本的V ...

  7. ACM ICPC 2016–2017, NEERC, Northern Subregional Contest Problem J. Java2016

    题目来源:http://codeforces.com/group/aUVPeyEnI2/contest/229510 时间限制:2s 空间限制:256MB 题目大意: 给定一个数字c 用 " ...

  8. 2016-2017 ACM-ICPC, NEERC, Northern Subregional Contest

    A. Anniversary Cake 随便挑两个点切掉就好了. #include<bits/stdc++.h> using namespace std; const int Maxn=2 ...

  9. 2017-2018 ACM-ICPC, NEERC, Northern Subregional Contest

    A. Auxiliary Project 完全背包. #include<stdio.h> #include<iostream> #include<string.h> ...

随机推荐

  1. 简述站点访问控制、基于用户的访问控制、httpd虚拟主机、持久链接等应用配置实例

    1 站点访问控制 可基于两种机制指明对哪些资源进行何种访问控制: 文件系统路径 URL路径 注意: 从上到下匹配,匹配到一个就立即执行 如果没有子目录的访问控制,但是有父目录的访问控制,则子目录继承父 ...

  2. 90-Standard Deviation 标准离差指标.(2015.7.4)

    Standard Deviation 标准离差指标 ~计算: StdDev = SQRT (SUM (CLOSE - SMA (CLOSE, N), N)^2)/N 注解: SQRT - 正方体根: ...

  3. LeetCode(53) Maximum Subarray

    题目 Find the contiguous subarray within an array (containing at least one number) which has the large ...

  4. codeforces 407 div1 A题(Functions again)

    codeforces 407 div1 A题(Functions again) Something happened in Uzhlyandia again... There are riots on ...

  5. 集训第六周 数学概念与方法 UVA 11181 条件概率

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18546 题意:有n个人会去超市,其中只有r个人会买东西,每个人独自买东西的概 ...

  6. (转载)C++ string中find() ,rfind() 等函数 用法总结及示例

    string中 find()的应用  (rfind() 类似,只是从反向查找) 原型如下: (1)size_t find (const string& str, size_t pos = 0) ...

  7. Springboot 缓存使用

    . CachingProvider . CacheManager . Cache . Entry . Expiry 1. 开启基于注解的缓存 @EnableCaching 下面列出几个核心的注解 @C ...

  8. 九度oj 题目1490:字符串链接

    题目1490:字符串链接 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:2610 解决:1321 题目描述: 不用strcat 函数,自己编写一个字符串链接函数MyStrcat(char ...

  9. Jupyter notebook使用笔记

    常用快捷键 For a Cell,   Blue -> selecting. Green -> editing. Esc -> exist edit When the cell is ...

  10. 【spring boot 系列】spring data jpa 全面解析(实践 + 源码分析)

    前言 本文将从示例.原理.应用3个方面介绍spring data jpa. 以下分析基于spring boot 2.0 + spring 5.0.4版本源码 概述 JPA是什么? JPA (Java ...