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解题报告------------------------------------------------------------------------------------------ ...
随机推荐
- Android 中间白色渐变到看不见的线的Drawable
用gradient <gradient android:startColor="#00ffffff" android:centerColor="#ffffff&qu ...
- MySQL 相关文章参考
MySQL 中隔离级别 RC 与 RR 的区别http://www.cnblogs.com/digdeep/p/4968453.html MySQL+InnoDB semi-consitent rea ...
- php无法保存SESSION问题总汇
昨天客户又过来说网站的问题,说的也都是些毛毛雨的东西,管理那么多网站,再有这么些客户的存在,本人也是累了,但当登录后台的时候突然发现后台登录不了,查看了一下验证码服务器端的session为空值,之前登 ...
- Ubuntu搜狗输入法无法输入中文等问题
Linux版本的搜狗输入法经常崩溃,无法输入中文,今天作下记录,环境:Ubuntu14.04 64位 1.安装和卸载 Linux搜狗是基于框架fcitx的,先得安装框架Ubunt安装搜狗方法 也可以直 ...
- Notepad++配色方案
1.下载notepad++样式文件 styles.xml 2.将该文件拷贝到 C:\Users\Administrator\AppData\Roaming\Notepad++ 目录(将Administ ...
- CentOS 7与 Windows双系统丢失Windows启动项及默认启动项修改
1.Windows启动项消失的原因: 在安装Win7.8/10系统+CentOS7双系统后,默认会将mbr(Main Boot Record)改写为grub2,默认的CentOS7无法识别Wind ...
- AngularJS 重复HTML元素
data-ng-repeat指令会重复一个HTML元素 <!DOCTYPE html><html><head><meta http-equiv="C ...
- JS常见内置对象和方法
JS中常用的内置对象:Array对象.Date对象.正则表达式对象.string对象.Global对象 Array对象中常用方法: concat() 表示把几个数组合并成一个数组join() 设 ...
- cordforce Educational Codeforces Round 47 补题笔记 <未完>
题目链接 http://codeforces.com/contest/1009 A. Game Shopping 直接模拟即可,用了一个队列来存储账单 #include <iostream> ...
- 交换机基础配置之三层交换机实现vlan间通信
我们以上面的拓扑图做实验,要求为pc1,pc2,pc3配置为vlan10,pc4,pc5,pc6配置为vlan20,pc7,pc8,pc9配置为vlan30 server0和server1配置为vla ...