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解题报告------------------------------------------------------------------------------------------ ...
随机推荐
- ImportError: No module named images
[问题] 在使用学习wxPython时,一个Dem抱有如题所示错误 [解决] images 只不过是wxpython自带demo中的一个文件 体验wxpython IN action的时候Import ...
- html css javascript 知识点总结 bom js 操作标签 文本 节点 表格各行变色 悬停变色 省市联动 正则
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- java 打印流 递归复制子文件子文件夹 不同编码文件复制到同一文件中 序列化流反序列化流
package com.swift.jinjie; import java.io.BufferedInputStream; import java.io.File; import java.io.Fi ...
- 修改第三方库内容,carsh提示"image not found"
在图示位置把提示的东西加上即可 参考: iOS app with framework crashed on device, dyld: Library not loaded, Xcode 6 Beta ...
- A的B次幂
Description 给出两个正整数A和B 请输出A的B次幂 结果可能很大,请对1000000007求模 Input A和B,两个整数均不大于10^18 Output A的B次幂对100000000 ...
- Open closed principle
#include <iostream> using namespace std; class Book { public: string getContents() { return &q ...
- 如何快速查看mysql数据文件存放路径?
进入mysql终端 mysql>show variables like '%datadir%'; 出来的结果即是!
- 使用inotify-tools与rsync构建实时备份系统
使用inotifywait监控文件变动 inotifywait是 inotify-tools 包中提供的一个工具,它使用 inotify API 来监控文件/目录中的变动情况. 在archlinux上 ...
- HTML语义化的重要性
语义化标签就是尽量使用有相对应的结构的含义的Html的标签 1.结构更好,更利于搜索引擎的抓取(SEO的优化)和开发人员的维护(可维护性更高,因为结构清晰,so易于阅读). 2.更有利于特殊终端的阅读 ...
- my share
网盘一: username:3a1bd0f6634d72a0423aa21c7d2dee1a password:adaa0dfa36f537a4469fcc6e78823e1c 网盘二: userna ...