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求余数. 题目给你两个字符串,其中的字 ...
随机推荐
- 链表list容器中通过splice合并链表与merge的不同,及需要注意的问题
#include "stdafx.h" #include <iostream> #include <list> #include <algorithm ...
- [Javascript]3. Improve you speed! Performance Tips
/** Let inheritance help with memory efficiency */ function SignalFire(ID, startingLogs){ this.fireI ...
- JQuery 获取指定url对应的html内容
用jquery的ajax类似的请求就可以了:比如: $.get("test.php", function(data){ alert("Data Loaded: " ...
- Windows Phone 学习教程(一)
http://www.cnblogs.com/webabcd/category/385852.html Windows Phone 7 教程 Windows Phone 8.1 Windows Pho ...
- ORA-14450
ORA-14450 attempt to access a transactional temp table already in use Cause: An attempt was made to ...
- 每天一点css3聚沙成塔(一):transition
transition 语法: transition:[ transition-property ] || [ transition-duration ] || [ transition-timing- ...
- The FastCGI process exited unexpectedly
ERROR:HTTP Error 500.0 - Internal Server Error D:\Program Files\php\php-cgi.exe - The FastCGI proces ...
- Spring的PropertyPlaceholderConfigurer应用
Spring 利用PropertyPlaceholderConfigurer占位符 1. PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是BeanFa ...
- ububru下 redmine安装教程
1.安装之前确认你已经安装好了mysql 2.连接mysql创建数据库 mysql -u root –p CREATE DATABASE redmine CHARACTER SET utf8; CRE ...
- 【00】why集搜客网络爬虫?
与各种大企业相比,大数据对于没有数据资源的个体而言是奢侈品. 然而在“互联网思维”.“互联网+”引领下,我们应当勇于实践和颠覆传统,将数据平民化. 不管你是财经.金融.经管.社科专业的技术小白,正在做 ...