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. OpenGL学习 Our First OpenGL Program

    This shows you how to create the main window with the book’s application framework and how to render ...

  2. 【BZOJ5212】[ZJOI2018] 历史(LCT大黑题)

    点此看题面 大致题意: 给定一棵树每个节点\(Access\)的次数,求最大虚实链切换次数,带修改. 什么是\(Access\)? 推荐你先去学一学\(LCT\)吧. 初始化(不带修改的做法) 首先考 ...

  3. LA 3938 动态最大连续和

    题目链接:https://vjudge.net/contest/146667#problem/C 题意:动态的求一个区间的最大连续和. 分析: 看上去可以RMQ去做,但是,当分成两个部分,原来的部分的 ...

  4. BZOJ 3227: [Sdoi2008]红黑树(tree)

    BZOJ 3227: [Sdoi2008]红黑树(tree) 标签(空格分隔): OI-BZOJ OI-其它 Time Limit: 10 Sec Memory Limit: 128 MB Descr ...

  5. 2018.8.1 Java中的反射和同步详解

    为何要使用同步? java允许多线程并发控制,当多个线程同时操作一个可共享的资源变量时(如数据的增删改查), 将会导致数据不准确,相互之间产生冲突,因此加入同步锁以避免在该线程没有完成操作之前,被其他 ...

  6. python_2_变量的使用2

    ''' 多行注释(三个单引号,或者双引号) gf_of_oldboy="Chen rong hua"#变量的表示办法1,用下划线(老男孩的女朋友) GfOfOldboy=" ...

  7. PRmakefile文件

    Ubuntu下的makefile: # /******************************************************************************* ...

  8. Win7多用户同时登陆

    软件提供下载: http://pan.baidu.com/s/1o6FQv70

  9. 当Java遇见了Html--Servlet篇

    ###一.什么是servlet servlet是在服务器上运行的小程序.一个servlet就是一个 java类,并且通过"请求-响应"编程模型来访问的这个驻留在服务器内存里的程序. ...

  10. java循环删除List元素的方法总结

    1.for循环 2.迭代器 3.过渡法 import java.util.*; /** * Created by HP on 2018/8/2. */ public class Test { publ ...