Sorting a Three-Valued Sequence
分析:首先我们先对其进行排序,并看排序以后有多少个元素顺序是对的,接着我们看有多少个元素经过一次交换可以得到的,最后剩下的元素就是经过两次交换可以得到的了。
/*
PROB:sort3
ID:wanghan
LANG:C++
*/
#include "iostream"
#include "cstdio"
#include "cstring"
#include "string"
#include "algorithm"
using namespace std;
const int maxn=+;
int a[maxn],b[maxn];
int vis[maxn];
int n;
int main()
{
freopen("sort3.in","r",stdin);
freopen("sort3.out","w",stdout);
scanf("%d",&n);
for(int i=;i<=n;i++){
cin>>a[i];
b[i]=a[i];
}
sort(b+,b++n);
for(int i=;i<=n;i++){
if(a[i]==b[i]){
vis[i]=;
}
}
int cnt=;
for(int i=;i<=n-;i++){
if(!vis[i]){
for(int j=i+;j<=n;j++){
if(!vis[j]){
if(a[i]==b[j]&&(b[i]==a[j])){
vis[i]=,vis[j]=;
cnt++; break;
}
}
}
}
}
int num=;
for(int i=;i<=n;i++)
if(!vis[i])
num++;
cnt+=(num/)*;
cout<<cnt<<endl;
}
Sorting a Three-Valued Sequence的更多相关文章
- Test Precisely and Concretely
Test Precisely and Concretely Kevlin Henney IT IS IMPORTANT TO TEST for the desired, essential behav ...
- 洛谷P1459 三值的排序 Sorting a Three-Valued Sequence
P1459 三值的排序 Sorting a Three-Valued Sequence 166通过 369提交 题目提供者该用户不存在 标签USACO 难度普及- 提交 讨论 题解 最新讨论 那么 ...
- USACO 2.1 Sorting a Three-Valued Sequence
Sorting a Three-Valued Sequence IOI'96 - Day 2 Sorting is one of the most frequently performed compu ...
- 洛谷 P1459 三值的排序 Sorting a Three-Valued Sequence
P1459 三值的排序 Sorting a Three-Valued Sequence 题目描述 排序是一种很频繁的计算任务.现在考虑最多只有三值的排序问题.一个实际的例子是,当我们给某项竞赛的优胜者 ...
- 三值的排序 Sorting a Three-Valued Sequence(洛谷 P1459USACO2.1,IOI96Day2)
Sorting a Three-Valued Sequence IOI'96 - Day 2 Sorting is one of the most frequently performed compu ...
- D. Sequence Sorting dp
D. Sequence Sorting 题目大意:给你一个序列,有一种操作就是对所有相同的数可以挪到最前面,也可以挪到最后面,问最少操作次数. 首先,对于很多个相同的数,可以缩成两个位置,一个是就是这 ...
- Codeforces Round #591 (Div. 2, based on Technocup 2020 Elimination Round 1) D. Sequence Sorting
链接: https://codeforces.com/contest/1241/problem/D 题意: You are given a sequence a1,a2,-,an, consistin ...
- USACO Sorting a Three-Valued Sequence
题目描述 排序是一种很频繁的计算任务.现在考虑最多只有三值的排序问题.一个实际的例子是,当我们给某项竞赛的优胜者按金银铜牌排序的时候.在这个任务中可能的值只有三种1,2和3.我们用交换的方法把他排成升 ...
- 【USACO 2.1】Sorting A Three-Valued Sequence
/* TASK: sort3 LANG: C++ URL: http://train.usaco.org/usacoprob2?a=RkPIMxsFWzm&S=sort3 SOLVE: n个数 ...
- USACO Section 2.1 Sorting a Three-Valued Sequence
/* ID: lucien23 PROG: sort3 LANG: C++ */ #include <iostream> #include <fstream> #include ...
随机推荐
- python--输出自己需要的字符串连接的的方式
python中有很多字符串连接方式,今天在写代码,顺便总结一下,从最原始的字符串连接方式到字符串列表连接,大家感受下: 最原始的字符串连接方式:str1 + str2 python 新字符串连接语法: ...
- Wormholes(spfa判负环)
POJ - 3259—— Wormholes Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %I64d & % ...
- Java常用API(Scanner,Random)匿名对象
API:即Application programming Interface,应用编程接口. Java中封装了许许多多的API供用户使用,Scanner与Random便是其中之一,API实际就是类,已 ...
- nodejs启动前端项目步骤
在.nuxt目录下打开命令行: 一:npm rm node-sass 二:npm install node-sass 三:npm install 四:npm run dev
- 【Java TCP/IP Socket】基于线程池的TCP服务器(含代码)
了解线程池 在http://blog.csdn.net/ns_code/article/details/14105457(读书笔记一:TCP Socket)这篇博文中,服务器端采用的实现方式是:一个客 ...
- Filter和Interceptor的终归作用还是从入口修改或验证请求进来的数据
Filter是Java EE标准.Inteceptor是Spring 标准. Filter在servlet前面,Interveptor在servlet之后 Filter和Inteceptor都可以改变 ...
- BIM
BIM进入中国已经有十来个年头,随着对BIM概念的深入了解.当前国内BIM应用逐渐由三维模型的可视化应用升级为基于BIM模型的信息进行项目精细化动态管理. 传统粗放的项目管理方法是工程项目难以进行精细 ...
- Shiro源代码分析之两种Session的方式
1.Shiro默认的Session处理方式 <!-- 定义 Shiro 主要业务对象 --> <bean id="securityManager" class=& ...
- add swapspace file on ubuntu.
https://askubuntu.com/questions/566745/allocate-swap-after-ubuntu-14-04-lts-installation ----------- ...
- 关于android 使用bitmap的OOM心得和解决方式
android开发,从2010年開始学习到如今的独立完毕一个app,这漫长的四年,已经经历了非常多次bug的折磨.无数次的加班训练.然而,自以为自己已经比較了解android了,却近期在一个项目上.由 ...