[Usaco12mar] A cow in a skyscraper cows in a skyscraper

Source: Internet
Author: User

Title Description

A little known fact about Bessie and friends are that they love stair climbing races. A better known fact is that cows really don ' t like going down stairs. The cows finish racing to the top of their favorite skyscraper, they had a problem. Refusing to climb back down using the stairs, the cows is forced to use the elevator in order to get back to the ground F Loor.

The elevator has a maximum weight capacity of W (1 <= W <= 100,000,000) pounds and cow I weighs c_i (1 <= c_i < ; = W) pounds. Please help Bessie figure out how to get all the N (1 <= n <=) of the cows to the ground floor using the least Nu Mber of elevator rides. The sum of the weights of the cows on each elevator ride must is no larger than W.

Give n items, volume of w[i], it is divided into several groups, requires the total volume of each group <=w, ask the smallest group. (n<=18)

Problem analysis

Simulated annealing

In other words, greed is wrong, although at one glance it seems to be no problem.
Greedy: 75 points
Naked greed is obviously wrong, proves slightly. But we found an interesting thing, the cow's weight from the big to the small order greedy, in the general strength of the data is correct, a bit like to put a stone in the jar and then put the sand and then water than the first to put sand stone to better.  In addition to this data is very small, n^2 greed can be performed 1e5 levels of time. Synthetic, annealing.
But if the answer is a special structure, it will make the annealing efficiency reduced, difficult to get the correct solution, then a small trick can be taken:a small shaking of the range of questions asked。
For this problem is a slight adjustment of the range of W. Given the greedy error, I set E to 0.9.Under the more error-based algorithm, it is possible to increase the likelihood of positive solution by cooling slightly faster .  Not much to say, on the code.with a small tune for half a day WA to WA to Wawa cryCode
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<ctime>using namespacestd;Const intMAXN = -;intN,w,ans,tim;intA[MAXN],S[MAXN];Doublet,e;BOOLcmpintXinty) {returnX >y;} InlineBOOLGetposs () {T*=e; if(rand ()%10000< T)return false; Else return true;} InlinevoidClean () {T=9000, E =0.9; memset (s),0,sizeof(s)); Tim=0; return;}intMain () {Srand (Time (NULL)); scanf ("%d%d",&n,&W); W*=1.005;  for(inti =1; I <= n;i++) {scanf ("%d",&A[i]); } sort (A+1, A +1+n,cmp); BOOLFlag =false; intCNT =200000; Ans=0x3f3f3f3f;  while(cnt--) {clean ();  for(inti =1; I <= n;i++) {flag=false;  for(intj =1; J <= tim;j++) {                if(W-s[j] >= A[i] &&Getposs ()) {S[j]+=A[i]; Flag=true;  Break; }            }            if(!flag) S[++tim] + =A[i]; } ans=min (ans,tim); } printf ("%d\n", ans); return 0;}

[Usaco12mar] A cow in a skyscraper cows in a skyscraper

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.