USACO Section 2.1 Sorting a Three-Valued Sequence
/*
ID: lucien23
PROG: sort3
LANG: C++
*/ #include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
void exchange(int nums[], int begin, int end, int N, int x);
int sum = 0;
int main()
{
ifstream infile("sort3.in");
ofstream outfile("sort3.out");
if(!infile || !outfile)
{
cout << "file operation failure!" << endl;
return -1;
} int N;
infile >> N;
int *nums = new int[N];
int count1, count2, count3;
count1 = count2 = count3 = 0;
for (int i=0; i<N; i++)
{
infile >> nums[i];
switch (nums[i])
{
case 1:
count1++;
break;
case 2:
count2++;
break;
case 3:
count3++;
break;
default:
break;
}
} exchange(nums, 0, count1, N, 1);
exchange(nums, count1, count1+count2, N, 2); outfile << sum << endl; return 0;
} /*
*交换时将大的交换到后面,小的交换到前面
*/
void exchange(int nums[], int begin, int end, int N, int x)
{
int count0 = 0;
vector<int> vecNum;
for (int i=begin; i<end; i++)
{
if (nums[i] != x)
{
count0++;
vecNum.push_back(nums[i]);
nums[i] = x;
}
}
sum += count0; sort(vecNum.begin(), vecNum.end());
for (int i=end, j=0; i<N && count0>0; i++)
{
if (nums[i] == 1)
{
nums[i] = vecNum[j];
j++;
count0--;
}
}
}
USACO Section 2.1 Sorting a Three-Valued Sequence的更多相关文章
- USACO Section 2.1 Sorting a Three-Valued Sequence 解题报告
题目 题目描述 给N个整数,每个整数只能是1,2,或3.现在需要对这个整数序列进行从小到大排序,问最少需要进行几次交换.N(1 <= N <= 1000) 样例输入 9 2 2 1 3 3 ...
- USACO Section 1.3 题解 (洛谷OJ P1209 P1444 P3650 P2693)
usaco ch1.4 sort(d , d + c, [](int a, int b) -> bool { return a > b; }); 生成与过滤 generator&& ...
- 【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 3.3: Riding the Fences
典型的找欧拉路径的题.先贴下USACO上找欧拉路径的法子: Pick a starting node and recurse on that node. At each step: If the no ...
- USACO Section 3.3 Camlot(BFS)
BFS.先算出棋盘上每个点到各个点knight需要的步数:然后枚举所有点,其中再枚举king是自己到的还是knight带它去的(假如是knight带它的,枚举king周围的2格(网上都这么说,似乎是个 ...
- [IOI1996] USACO Section 5.3 Network of Schools(强连通分量)
nocow上的题解很好. http://www.nocow.cn/index.php/USACO/schlnet 如何求强连通分量呢?对于此题,可以直接先用floyd,然后再判断. --------- ...
- USACO Section 5.3 Big Barn(dp)
USACO前面好像有类似的题目..dp(i,j)=min(dp(i+1,j),dp(i+1,j+1),dp(i,j+1))+1 (坐标(i,j)处无tree;有tree自然dp(i,j)=0) .d ...
- USACO Section 1.3 Prime Cryptarithm 解题报告
题目 题目描述 牛式的定义,我们首先需要看下面这个算式结构: * * * x * * ------- * * * <-- partial product 1 * * * <-- parti ...
- USACO Section 1.1 Your Ride Is Here 解题报告
题目 问题描述 将字符串转变为数字,字母A对应的值为1,依次对应,字母Z对应的值为26.现在有一个字符串,将其中的每个字符转变为数字之后进行累乘,最终的结果对47求余数. 题目给你两个字符串,其中的字 ...
随机推荐
- Tcp实现简单的大小写转换功能
有这样一个需求: 客户端给读物段发送文本,服务端会将文本转换为大写再返回客户端 而且客户端可以不断的进行文本转换,当客户端输入over时,转换结束. 分析: 既然是操作设备上的数据,那么久可以使用io ...
- C#高性能大容量SOCKET并发(十一):编写上传client
client封装总体框架 client编程基于堵塞同步模式,仅仅有数据正常发送或接收才返回,假设错误发生则抛出异常,基于TcpClient进行封装,主要类结构例如以下图: TcpClient:NET系 ...
- [core Java学习笔记][第一二三章基本语法]
基本语法 1 Java 简单的类型 1.1 一些常量 正无穷大 Double.POSITVE_INFINITY 负无穷大 Double.NEGATIVE_INFINITY 不存在 Double.NaN ...
- ado.net数据库操作(2)
5.1使用SQLDataReader进行数据库查询 <%@ Import Namespace="System.Data" %> <%@ Import NameSp ...
- 注册界面的优化之ActionBar组件的应用之(二)ActionBar组件的事件处理
开发步骤: 重写父类中的一个方法onOptionsItemSelected实现ActionBar中的选项单击事件 //Register_Activity.java public class Regis ...
- IOS 实现QQ好友分组展开关闭功能
贴出核心代码 主要讲一下思路. - (void)nameBtnClick:(myButton *)sender { //获取当前点击的分组对应的section self.clickIndex = s ...
- (转) 学习C++ -> 向量(vector)
vector是向量类型,它是一种对象实体,具有值,所以可以看作是变量. 它可以容纳许多其他类型的相同实体,如若干个整数,所以称其为容器. vector类与一般的Array类的区别在于: 1 ...
- php的json_encode函数问题
php的json_encode函数问题: $ary = []; $ary[0] = 'a'; $ary[1] = 'b'; echo json_encode($ary) . '<br>'; ...
- python运维开发(二十)----models操作、中间件、缓存、信号、分页
内容目录 select Form标签数据库操作 models操作F/Q models多对多表操作 Django中间件 缓存 信号 分页 select Form标签补充 在上一节中我们可以知道Form标 ...
- 异常处理与调试4 - 零基础入门学习Delphi53
调试(Debug) 让编程改变世界 Change the world by program 调试(Debug) 在应用程序开发中检测.处理程序中的错误是一个非常重要的环节.在Delphi的集成开发环境 ...