poj1065

【题意】

有n根木棒,已知他们的质量和长度,需对他们进行加工,开机1分钟,加工一根一分钟,

且每次开机后,加工的第i+1根必须比第i根的长度和质量都要大才行,问最短加工时间。

【输入】

第一行T表示T组数据。

每组数据第一行一个N,表示几根木棒。

然后N个二元组,表示每根木棒的长度与质量。

【输出】

对于每组数据,输出最短加工时间。


贪心。

排下序,每次尽可能加工多。


program poj1065;
type
  stick=record
          w,l:longint;
        end;
var
  t,n,i,j,k,q,ans,total,now:longint;
  dl:array [0..5001] of stick;
  yes:array [0..5001] of boolean;
procedure swap (var a,b:stick);
var
  i:stick;
begin
  i:=a;
  a:=b;
  b:=i;
end;
procedure qsort (s,e:longint);
var
  i,j:longint;
  k:stick;
begin
  if s>=e then exit;
  i:=s;
  j:=e;
  k:=dl[(s+e) div 2];
  while i<=j do
    begin
      while (dl[i].w<k.w)or((dl[i].w=k.w)and(dl[i].l<k.l)) do inc(i);
      while (dl[j].w>k.w)or((dl[j].w=k.w)and(dl[j].l>k.l)) do dec(j);
      if i>j then break;
      swap(dl[i],dl[j]);
      inc(i);
      dec(j);
    end;
  qsort(s,j);
  qsort(i,e);
end;
begin
  read(t);
  for q:=1 to t do
    begin
      fillchar(yes,sizeof(yes),false);
      ans:=0;
      read(n);
      for i:=1 to n do
        read(dl[i].w,dl[i].l);
      qsort(1,n);
      total:=0;
      now:=0;
      while total<n do
        begin
          inc(ans);
          for i:=1 to n do
            if (not yes[i])and(dl[i].l>=now) then
              begin
                yes[i]:=true;
                inc(total);
                now:=dl[i].l;
              end;
          now:=0;
        end;
      writeln(ans);
    end;
end.


### 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、付费专栏及课程。

余额充值