USACO Section2.1 Ordered Fractions 解题报告
    frac1解题报告 —— icedream61 博客园(转载请注明出处)
------------------------------------------------------------------------------------------------------------------------------------------------
【题目】
	  给你N,对于所有的既约分数i/j(1<=j<=N,0<=i<=j),升序排列输出(重复则只留j最小的)。
【数据范围】
	  1<=N<=160
【输入样例】
	  5
【输出样例】
	  0/1
	  1/5
	  1/4
	  1/3
	  2/5
	  1/2
	  3/5
	  2/3
	  3/4
	  4/5
	  1/1
------------------------------------------------------------------------------------------------------------------------------------------------
【分析】
	  双向链表,往里一个一个插就好了。每个分母i,分子从0到i,扫一遍当前队列即可完成插入。因此,复杂度不到O(N²)。
------------------------------------------------------------------------------------------------------------------------------------------------
【总结】
	  没啥可说的。
------------------------------------------------------------------------------------------------------------------------------------------------
【代码】
/*
ID: icedrea1
PROB: frac1
LANG: C++
*/ #include <iostream>
#include <fstream>
using namespace std; struct Node
{
int x,y;
Node *l,*r;
Node() {}
Node(int i,int j):x(i),y(j),l(),r() {}
friend bool operator==(Node p,Node q) { return p.x*q.y==p.y*q.x; }
friend bool operator<(Node p,Node q) { return p.x*q.y<p.y*q.x; }
friend bool operator>(Node p,Node q) { return p.x*q.y>p.y*q.x; }
friend ostream& operator<<(ostream& out,Node p) { return cout<<p.x<<"/"<<p.y; }
}*A,*B; int N; int main()
{
ifstream in("frac1.in");
ofstream out("frac1.out"); in>>N;
A=new Node(-,); B=new Node(,); A->r=B; B->l=A; for(int i=;i<=N;++i) // 分母为i
{
Node *now=A;
for(int j=;j<=i;++j) // 分子为j
{
Node *p=new Node(j,i);
while(*p>*now) now=now->r;
if(*p==*now) continue; else now=now->l;
p->r=now->r; p->r->l=p; p->l=now; now->r=p;
}
//for(Node *p=A->r;p!=B;p=p->r) cout<<p->x<<"/"<<p->y<<endl; cin.get();
} for(Node *p=A->r;p!=B;p=p->r) out<<p->x<<"/"<<p->y<<endl; in.close();
out.close();
return ;
}
USACO Section2.1 Ordered Fractions 解题报告的更多相关文章
- USACO Section2.1 Healthy Holsteins 解题报告 【icedream61】
		
holstein解题报告 --------------------------------------------------------------------------------------- ...
 - USACO Section2.2 Preface Numbering 解题报告 【icedream61】
		
preface解题报告----------------------------------------------------------------------------------------- ...
 - USACO Section2.1 Hamming Codes 解题报告 【icedream61】
		
hamming解题报告----------------------------------------------------------------------------------------- ...
 - USACO Section2.1 The Castle 解题报告
		
castle解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...
 - USACO Section2.3 Controlling Companies 解题报告 【icedream61】
		
concom解题报告------------------------------------------------------------------------------------------ ...
 - USACO Section2.3 Money Systems 解题报告 【icedream61】
		
money解题报告------------------------------------------------------------------------------------------- ...
 - USACO Section2.3 Zero Sum 解题报告 【icedream61】
		
zerosum解题报告----------------------------------------------------------------------------------------- ...
 - USACO Section2.3 Cow Pedigrees 解题报告 【icedream61】
		
nocows解题报告------------------------------------------------------------------------------------------ ...
 - USACO Section2.3 Longest Prefix 解题报告 【icedream61】
		
prefix解题报告------------------------------------------------------------------------------------------ ...
 
随机推荐
- Microsoft EDP(enterprise database protection)配置策略中的三种Rule template
			
搭建Microsoft EDP环境: Microsoft 10 insider preview,Microsoft Intune,ie10(要安装插件silverlight) 这里暂时只说在进行配置策 ...
 - May 8th 2017 Week 19th Monday
			
Art lies in concealing art. 隐而不露即艺术. Sometimes, concealing is much more seductive than totally naked ...
 - 【PHP 模板引擎】Prototype 原型版发布!
			
在文章的开头,首先要向一直关注我的人说声抱歉!因为原本是打算在前端框架5.0发布之后,就立马完成 PHP 模板引擎的初版.但我没能做到,而且一直拖到了15年元旦才完成,有很严重的拖延症我很惭愧,再次抱 ...
 - Android(java)学习笔记62:android.intent.action.MAIN 与 android.intent.category.LAUNCHER 理解
			
1. 先看看网路上的说法: android.intent.action.MAIN 决定应用程序最先启动的 Activity android.intent.category.LAUNCHER 决定应用程 ...
 - 用MXnet实战深度学习之一:安装GPU版mxnet并跑一个MNIST手写数字识别
			
用MXnet实战深度学习之一:安装GPU版mxnet并跑一个MNIST手写数字识别 http://phunter.farbox.com/post/mxnet-tutorial1 用MXnet实战深度学 ...
 - ImportError: No module named images
			
[问题] 在使用学习wxPython时,一个Dem抱有如题所示错误 [解决] images 只不过是wxpython自带demo中的一个文件 体验wxpython IN action的时候Import ...
 - Ajax的学习笔记(一)
			
AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),ajax并不是一门单独的语言,而是一种技术,是指一种创建交互式网页应用的网页开发技术. ...
 - 【洛谷P1948】[USACO08JAN]电话线
			
电话线 题目链接:https://www.luogu.org/problemnew/show/P1948 二分答案+最短路 我们要求一条1~n的路径,使其中的第k+1大的数最小. 二分第k+1大的数的 ...
 - 安装gcc,g++
			
安装gcc.g++ sudo apt-get install make gcc g++ 查看g++版本 g++ --version
 - cutil.h问题
			
CUDA5.0没有cutil.h头文件,貌似用helper_cuda.h文件代替,暂时没出问题.