poj 1065 Wooden Sticks

本文介绍了一个利用偏序定理结合贪心算法解决特定问题的实例。通过结构化数据处理与比较,实现了有效求解最大不相交子集的过程。代码中详细展示了如何使用C++进行实现,并通过具体例子讲解了算法背后的逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思想很容易想到,证明是看了别的解题报告:利用到偏序定理:贪心算法实现

#include <iostream>
#include <algorithm>
using namespace std;
/*328K	16MS*/
typedef struct _pair
{
        int l;
        int w;
}Pair;

int cmp(const void *a,const void *b)
{
    Pair a1=*(Pair *)a;
    Pair b1=*(Pair *)b;
    if(a1.l==b1.l)
       return a1.w-b1.w;
    return a1.l-b1.l;
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        Pair *p=new Pair[n];
        bool *b=new bool[n];
        memset(b,false,sizeof(bool)*n);

        for(int i=0;i<n;i++)
            cin>>p[i].l>>p[i].w;
        //计算反链
        qsort(p,n,sizeof(p[0]),cmp);
        int count=0; 
        for(int i=0;i<n;i++)
        {      
                if(b[i])    continue;
                if(!b[i])    count++;
                int k=i;
                for(int j=i+1;j<n;j++)
                {
                   //具有可比的都加到一个集合里面
                   if(p[j].w>=p[k].w&&!b[j])
                      {
                                 b[j]=true;
                                 k=j;
                      }    
                }
                b[i]=true;
        }
        
        cout<<count<<endl;
        delete []p;
        delete []b;
    }  
    system("pause");
    return 0;
}


### POJ 平台上的常见问题及解决方案 #### 贪心算法的应用场景 贪心算法是一种通过局部最优选择来达到全局最优解的方法。然而,这种方法并不总是能够提供正确的解答,尤其是在某些复杂情况下[^1]。为了有效利用贪心算法解决问题,必须深入理解问题的本质以及其适用条件。 #### POJ 上的经典贪心问题及其解决思路 以下是几个经典的 POJ 问题及其对应的解决方法: ##### 1. **POJ 1700 - Crossing River** 该问题是关于一组人过河的时间优化问题。核心在于如何合理安排人员渡河以最小化总时间。 - 解决方案:按照每次运送最快的人返回的原则设计贪心策略。具体实现可以通过模拟每一步的选择过程完成。 ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n; cin >> n; vector<int> times(n); for (auto &t : times) cin >> t; sort(times.begin(), times.end()); int time = 0; while (!times.empty()) { if (times.size() >= 2) { time += times[1]; // Send the two fastest people. if (times.size() > 2) { time += times.front(); // Return one of them back with the boat. } } else { time += times.back(); } times.erase(times.begin()); // Remove processed elements. } cout << time; } ``` ##### 2. **POJ 1017 - Packets** 此问题涉及将不同大小的数据包放入固定容量的盒子中,目标是最小化使用的盒子数量。 - 解决方案:采用优先队列或者排序的方式处理数据包尺寸,并尝试尽可能多地填充每个盒子。 ```cpp #include <bits/stdc++.h> using namespace std; int main(){ int k, m; cin>>k>>m; priority_queue<int,vector<int>,greater<int>> pq; // Min heap to store packets' sizes. for(int i=0;i<m;i++){ int size; cin>>size; pq.push(size); } int boxesUsed = 0; while(!pq.empty()){ int currentBoxCapacity = k; bool filled=false; while(!pq.empty() && !filled){ if(pq.top()<=currentBoxCapacity){ currentBoxCapacity -= pq.top(); pq.pop(); } else{ break; } if(currentBoxCapacity==0 || pq.empty()) filled=true; } ++boxesUsed; } cout<<boxesUsed; } ``` ##### 3. **POJ 1065 - Wooden Sticks** 这是一个典型的区间覆盖问题,目的是找到最少的数量使得所有木棍被完全覆盖。 - 解决方案:先按长度升序排列所有的木棍,再依次遍历并记录当前区间的最大右端点位置。 ```cpp #include<stdio.h> struct Stick { double l,w;}; bool cmp(const Stick& a,const Stick& b){return a.l<b.l;} Stick sticks[5000]; int main(){ int N,i,j,k,cnt; scanf("%d",&N); for(i=0;i<N;i++)scanf("%lf%lf",&sticks[i].l,&sticks[i].w); sort(sticks,sticks+N,cmp); cnt=k=1; for(i=1;i<N;i++) if(sticks[k-1].w<sticks[i].w){cnt++;k=i;} printf("%d\n",cnt); } ``` #### 关键注意事项 在使用贪心算法时需要注意以下几点: - 验证所选策略是否满足无后效性和可证明的正确性。 - 对于无法直接验证的情况,考虑动态规划或其他更通用的技术作为备选方案。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值