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求余数. 题目给你两个字符串,其中的字 ...
随机推荐
- Exchange Server 2010升级到Exchange Server 2013概览
- 跨服务器查询sql语句样例
若2个数据库在同一台机器上:insert into DataBase_A..Table1(col1,col2,col3----)select col11,col22,col33-- from Data ...
- Android系统匿名共享内存Ashmem(Anonymous Shared Memory)在进程间共享的原理分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6666491 在前面一篇文章Android系统匿 ...
- Android学习笔记—Windows下NDK开发简单示例
该示例假设Android开发环境已经搭建完成,NDK也配置成功: 1.在Eclipse上新建Android工程,名称为ndkdemo.修改res\layout\activity_main.xml &l ...
- C#枚举器接口IEnumerator的实现
原文(http://blog.csdn.net/phpxin123/article/details/7897226) 在C#中,如果一个类要使用foreach结构来实现迭代,就必须实现IEnumera ...
- photoshop动作面板批量处理图片边框技巧
1,想给图片加上边框,在不改变图片大小的前提下,可以这样做:ctrl+a,全选图片,然后“编辑”-----“描边”,在跳出来的选项卡里面可以设置边框颜色,大小,位置,及混合模式, ,我们设置好了,就可 ...
- 高性能PHP日志插件--Seaslog
日志系统作为记录系统运行的信息,包括 用户输入,安全日志等,日志系统是不能影响用户的使用. 为什么需要记录日志? 既然日志系统增加了整个系统的开销,为什么我还需要它,这是因为日志能帮我们记录运行的很多 ...
- nodejs的简单服务器程序
下面是参考<Jquery.Jquery UI 及Jquery Mobile>一书中的nodej服务器程序 var http = require('http'), url = require ...
- 10--动作系统(四)动作类中的reverse方法
上一篇文章在使用持续动作过程中遇到不少问题,以获取动作类的反系动作尤为突出.所以今天把动作类找了个遍,先将大部分动作类是否实现reverse方法总结如下: T表示实现F表示没有实现. 观察可以发现带T ...
- INSERTION_SORT插入排序C++实现
大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang 以下用C++实现插入排序的升序和降序排序 算法来自<算法导论> #include< ...