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²)。
------------------------------------------------------------------------------------------------------------------------------------------------
【总结】
  没啥可说的。

------------------------------------------------------------------------------------------------------------------------------------------------

【代码】

 1 /*

 2 ID: icedrea1

 3 PROB: frac1

 4 LANG: C++

 5 */

 6 

 7 #include <iostream>

 8 #include <fstream>

 9 using namespace std;

10 

11 struct Node

12 {

13     int x,y;

14     Node *l,*r;

15     Node() {}

16     Node(int i,int j):x(i),y(j),l(0),r(0) {}

17     friend bool operator==(Node p,Node q) { return p.x*q.y==p.y*q.x; }

18     friend bool operator<(Node p,Node q) { return p.x*q.y<p.y*q.x; }

19     friend bool operator>(Node p,Node q) { return p.x*q.y>p.y*q.x; }

20     friend ostream& operator<<(ostream& out,Node p) { return cout<<p.x<<"/"<<p.y; }

21 }*A,*B;

22 

23 int N;

24 

25 int main()

26 {

27     ifstream in("frac1.in");

28     ofstream out("frac1.out");

29 

30     in>>N;

31     A=new Node(-1,1); B=new Node(2,1); A->r=B; B->l=A;

32 

33     for(int i=1;i<=N;++i) // 分母为i

34     {

35         Node *now=A;

36         for(int j=0;j<=i;++j) // 分子为j

37         {

38             Node *p=new Node(j,i);

39             while(*p>*now) now=now->r;

40             if(*p==*now) continue; else now=now->l;

41             p->r=now->r; p->r->l=p; p->l=now; now->r=p;

42         }

43         //for(Node *p=A->r;p!=B;p=p->r) cout<<p->x<<"/"<<p->y<<endl; cin.get();

44     }

45 

46     for(Node *p=A->r;p!=B;p=p->r) out<<p->x<<"/"<<p->y<<endl;

47 

48     in.close();

49     out.close();

50     return 0;

51 }

 

你可能感兴趣的:(action)