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 解题报告的更多相关文章

  1. USACO Section2.1 Healthy Holsteins 解题报告 【icedream61】

    holstein解题报告 --------------------------------------------------------------------------------------- ...

  2. USACO Section2.2 Preface Numbering 解题报告 【icedream61】

    preface解题报告----------------------------------------------------------------------------------------- ...

  3. USACO Section2.1 Hamming Codes 解题报告 【icedream61】

    hamming解题报告----------------------------------------------------------------------------------------- ...

  4. USACO Section2.1 The Castle 解题报告

    castle解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...

  5. USACO Section2.3 Controlling Companies 解题报告 【icedream61】

    concom解题报告------------------------------------------------------------------------------------------ ...

  6. USACO Section2.3 Money Systems 解题报告 【icedream61】

    money解题报告------------------------------------------------------------------------------------------- ...

  7. USACO Section2.3 Zero Sum 解题报告 【icedream61】

    zerosum解题报告----------------------------------------------------------------------------------------- ...

  8. USACO Section2.3 Cow Pedigrees 解题报告 【icedream61】

    nocows解题报告------------------------------------------------------------------------------------------ ...

  9. USACO Section2.3 Longest Prefix 解题报告 【icedream61】

    prefix解题报告------------------------------------------------------------------------------------------ ...

随机推荐

  1. GNU Nano编辑器

    ^表示 control建 ^X 退出 nano ^O 保存文件 ^R 插入其他文件内容至光标位置 ^W 查找字符串 ^Y 跳至前一屏 ^V 跳至后一屏 ^K 剪切光标所在行并保存到剪贴板,或剪切选中内 ...

  2. IOS 加载更多数据中

    /**点击添加*/ - (IBAction)loadBtnClick { //1.隐藏加载按钮 self.loadBtn.hidden=YES; //2.显示“正在加载” self.loadingVi ...

  3. bzoj1150 [CTSC2007]数据备份

    Description 你在一家 IT 公司为大型写字楼或办公楼(offices)的计算机数据做备份.然而数据备份的工作是枯燥乏味的,因此你想设计一个系统让不同的办公楼彼此之间互相备份,而你则坐在家中 ...

  4. _default_ VirtualHost overlap on port 80, the first has precedence

    去掉#NameVirtualHost *:80,然后重启httpd

  5. chrome 浏览器插件开发(二)—— 通信 获取页面变量 编写chrome插件专用的库

    在chrome插件的开发过程中,我遇到了一些问题,在网上找了不少文章,可能是浏览器升级的原因,有一些是有效的也有无效的.下面我简单的分享一下我遇到的坑,以及我把这些坑的解决方案整理而成的js库 —— ...

  6. 3、SpringBoot+MybatisPlus整合-------代码生成器

    <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</art ...

  7. javaWeb CSS 图像签名

    <html> <head> <meta charset="utf-8" /> <title>CSS布局之图像签名</title ...

  8. Webpack4 学习笔记二 CSS模块转换

    前言 此内容是个人学习笔记,以便日后翻阅.非教程,如有错误还请指出 webpack 打包css模块 webpack是js模块打包器, 如果在入口文件引入css文件或其它的less.sass等文件,需要 ...

  9. 洛谷 P3372 线段树1

    这是一道模板题 线段树介绍https://www.cnblogs.com/nvwang123/p/10420832.html #include<bits/stdc++.h> using n ...

  10. centos7部署harbor

    官网 https://github.com/goharbor/harbor 1.升级系统内核 rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrep ...