| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:排序最快是nlogn吧???In Reply To:这道题还听有人说是dp,原来一道超级水题。 Posted by:ccsu2008021220 at 2009-10-07 01:17:10 > 做题之前听说是dp,怀着dp的心情去解,后来越来越发现不对劲,值感觉这道题没学算法也能AC掉,就是一个简单的排序在加选取策略就a了,复杂度也就O(n).
> 贴代码:
> #include<iostream>
> #include<algorithm>
> using namespace std;
> struct lct{
> int x,y;
> }a[50001];
> struct pt_cmp
> {
> bool operator()(const lct &a,const lct &b)const
> {
> if ( a.x == b.x ) return a.y < b.y;
> return a.x < b.x;
> }
> };
> int main()
> {
> int n;
> while(cin>>n&&n)
> {
> for(int i=1;i<=n;i++)
> cin>>a[i].x>>a[i].y;
> sort(a+1,a+n+1,pt_cmp());
> int l=a[n].x,c=a[n].y;
> int ans=1;
> for(int j=n-1;j>=1;j--)
> {
> if(a[j].x==l) continue;
> else
> if(a[j].y>c)
> {
> c=a[j].y;
> l=a[j].x;
> ans++;
> }
> }
> cout<<ans<<endl;
> }
> return 0;
> }
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator