﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-I believe-随笔分类-贪心*二分</title><link>http://www.cppblog.com/luyulaile/category/10564.html</link><description>I  can</description><language>zh-cn</language><lastBuildDate>Thu, 09 Jul 2009 18:48:56 GMT</lastBuildDate><pubDate>Thu, 09 Jul 2009 18:48:56 GMT</pubDate><ttl>60</ttl><item><title>joj 2228 Crossed ladders 二分法求解方程</title><link>http://www.cppblog.com/luyulaile/archive/2009/07/08/89559.html</link><dc:creator>luis</dc:creator><author>luis</author><pubDate>Wed, 08 Jul 2009 11:30:00 GMT</pubDate><guid>http://www.cppblog.com/luyulaile/archive/2009/07/08/89559.html</guid><wfw:comment>http://www.cppblog.com/luyulaile/comments/89559.html</wfw:comment><comments>http://www.cppblog.com/luyulaile/archive/2009/07/08/89559.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luyulaile/comments/commentRss/89559.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luyulaile/services/trackbacks/89559.html</trackback:ping><description><![CDATA[<div class=prob_text>
<p>A narrow street is lined with tall buildings. An x foot long ladder is rested at the base of the building on the right side of the street and leans on the building on the left side. A y foot long ladder is rested at the base of the building on the left side of the street and leans on the building on the right side. The point where the two ladders cross is exactly c feet from the ground. How wide is the street? <br>
<center><img src="http://acm.jlu.edu.cn/joj/images/problems/2228_1.jpg"></center>
<p>&#160;</p>
<h3>Input Specification</h3>
<p>Each line of input contains three positive floating point numbers giving the values of x, y, and c. </p>
<h3>Output Specification</h3>
<br>
<p>For each line of input, output one line with a floating point number giving the width of the street in feet, with three decimal digits in the fraction. </p>
<h3>Sample Input</h3>
<pre>30 40 10
12.619429 8.163332 3
10 10 3
10 10 1
</pre>
<h3>Sample Output</h3>
<pre>26.033
7.000
8.000
9.798
</pre>
</div>
<br>joj测试数据很弱，可能在其他oj上过不了，根据三角形相似得出以下关于w的方程，解w，第一次用二分法，不错。<br>f(w) = c - sqrt((y*y-w*w)*(x*x-w*w))/(sqrt(y*y-w*w) + sqrt(x*x -w*w))<br><br>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">cstdlib</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iomanip</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">math.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">f(w)&nbsp;=&nbsp;c&nbsp;-&nbsp;sqrt((y*y-w*w)*(x*x-w*w))/(sqrt(y*y-w*w)&nbsp;+&nbsp;sqrt(x*x&nbsp;-w*w))</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">double</span><span style="COLOR: #000000">&nbsp;func(</span><span style="COLOR: #0000ff">double</span><span style="COLOR: #000000">&nbsp;x,</span><span style="COLOR: #0000ff">double</span><span style="COLOR: #000000">&nbsp;y,</span><span style="COLOR: #0000ff">double</span><span style="COLOR: #000000">&nbsp;c)<br>&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">double</span><span style="COLOR: #000000">&nbsp;wdown</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0.0</span><span style="COLOR: #000000">,wup,w,fw;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(x</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">y)<br>&nbsp;&nbsp;&nbsp;&nbsp;wup</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">y;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br>&nbsp;&nbsp;&nbsp;&nbsp;wup</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">x;<br>&nbsp;&nbsp;&nbsp;&nbsp;w</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">wup</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">2.0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fw</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">c</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">sqrt((y</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">y</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">w</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">w)</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(x</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">x</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">w</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">w))</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">(sqrt(y</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">y</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">w</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">w)&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;sqrt(x</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">x&nbsp;</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">w</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">w));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(fw</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">0.0001</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wup</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">w;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;w</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">(wup</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">wdown)</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(fw</span><span style="COLOR: #000000">&lt;-</span><span style="COLOR: #000000">0.0001</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wdown</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">w;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;w</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">(wup</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">wdown)</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;w;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;}<br>&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>{&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">freopen("s.txt","r",stdin);<br>&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">freopen("key.txt","w",stdout);</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">double</span><span style="COLOR: #000000">&nbsp;x,y,c;<br></span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(&nbsp;cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">x</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">y</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">c)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #0000ff">fixed</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">setprecision(</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">func(x,y,c)</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>}<br><br>&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">system("PAUSE");</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;}<br><br></span></div>
<img src ="http://www.cppblog.com/luyulaile/aggbug/89559.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luyulaile/" target="_blank">luis</a> 2009-07-08 19:30 <a href="http://www.cppblog.com/luyulaile/archive/2009/07/08/89559.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>joj 1995: Energy 求连续子串使和最大</title><link>http://www.cppblog.com/luyulaile/archive/2009/07/04/89209.html</link><dc:creator>luis</dc:creator><author>luis</author><pubDate>Sat, 04 Jul 2009 01:27:00 GMT</pubDate><guid>http://www.cppblog.com/luyulaile/archive/2009/07/04/89209.html</guid><wfw:comment>http://www.cppblog.com/luyulaile/comments/89209.html</wfw:comment><comments>http://www.cppblog.com/luyulaile/archive/2009/07/04/89209.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luyulaile/comments/commentRss/89209.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luyulaile/services/trackbacks/89209.html</trackback:ping><description><![CDATA[<p>Energy</p>
<hr>
<p>
<table cellSpacing=3 cellPadding=3 width="75%" border=1>
    <colgroup style="COLOR: red; TEXT-ALIGN: center" span=7>
    <tbody>
        <tr>
            <th height=40>Status</th>
            <th>In/Out</th>
            <th>TIME Limit</th>
            <th>MEMORY Limit</th>
            <th>Submit Times</th>
            <th>Solved Users</th>
            <th>JUDGE TYPE</th>
        </tr>
        <tr>
            <td align=middle height=40><span id=probinfo_placeholder><img height=20 src="http://acm.jlu.edu.cn/joj/images/ok1.gif" width=20></span></td>
            <td>stdin/stdout</td>
            <td>3s</td>
            <td>10240K</td>
            <td>717</td>
            <td>196</td>
            <td>Standard</td>
        </tr>
    </tbody>
</table>
</p>
<div class=prob_text>
<p>Mr. Jojer is a very famous chemist. He is doing a research about behavior of a group of atoms. Atoms may have different energy and energy can be positive or negative or zero, e.g. 18 or -9. Absolute value of energy can not be more than 100. Any number of continuous atoms can form an atom-group. Energy of an atom-group is defined by the sum of energy of all the atoms in the group. All the atoms form an atom-community which is a line formed by all the atoms one by one. Energy of an atom-community is defined by the greatest energy of an atom-group that can be formed by atoms in the atom-community. The problem is, given an atom-community, to calculate its energy.</p>
<h3>Input</h3>
<p>The input contains several test cases. Each test case consists of two lines describing an atom-community. The first line of each test case contains an integer N(N&lt;=1000000), the number of atoms in the atom-community. The second line of each test case contains N integers, separated by spaces, each representing energy of an atom, given in the order according to the atom-community. The last test case marks by N=-1, which you should not proceed.</p>
<h3>Output</h3>
<p>For each test case(atom-community description), print a single line containing the energy.</p>
<h3>Sample Input</h3>
<pre>5
8 0 6 4 -1
-1</pre>
<h3>Sample Output</h3>
<pre>18</pre>
</div>
<p><br>理解题意很重要，题目的意思是说 在m个中选&nbsp;&nbsp; 连续的n个atom能量值 使其最大。<br>程序中sumtemp，sum。sumtemp确定的是其左边界，sum确定其右边界。<br>sumtemp确定左边前n个数之和为负的最大的n，且第n个数显然为负，然后从n+1开始选数。<br>sum确定了右边界使其最大。<br>求和最大都可以用这种思路！！！！！！！！！！<br>举例<br>1，2，-4，4，2，-2，1，5，-6，5，-3，8，10<br>请看sumt1=1,sum=1<br>sumt2=2;sum=1+2=3;<br>sum3=sum2-4=-1;则sumtemp=0;sum不变。<br>sum4=sumtemp+4;sum&lt;sum4,sum=4;<br>sum5=sumtemp+2；sum&lt;sum5,sum=6<br>总之sum只有在sum&lt;sumtemp时才修改。sumtemp&lt;0则清0.<br>一维dp.<br>#include"stdio.h"<br>int main()<br>{<br>&nbsp;freopen("s.txt","r",stdin);<br>&nbsp; freopen("key.txt","w",stdout);<br>&nbsp;int n;<br>&nbsp;int a;<br>&nbsp;while(scanf("%ld",&amp;n),n!=-1)<br>&nbsp;{&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; long long&nbsp; sum=-0x7fffffff,sumtemp=-0x7fffffff;<br>&nbsp;&nbsp; for(long i=0;i&lt;n;i++)<br>&nbsp;&nbsp; {&nbsp;<br>&nbsp;&nbsp;&nbsp; scanf("%d",&amp;a);<br>&nbsp;&nbsp;&nbsp; if(sumtemp&gt;0)<br>&nbsp;&nbsp;&nbsp;sumtemp+=a;<br>&nbsp;&nbsp;&nbsp; else<br>&nbsp;&nbsp;&nbsp;sumtemp=a;<br>&nbsp;&nbsp;&nbsp; if(sumtemp&gt;sum)<br>&nbsp;&nbsp;&nbsp;sum=sumtemp;</p>
<p>&nbsp;&nbsp; }<br>&nbsp;&nbsp; printf("%lld\n",sum);</p>
<p>&nbsp;}<br>&nbsp;return 0;<br>}</p>
<img src ="http://www.cppblog.com/luyulaile/aggbug/89209.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luyulaile/" target="_blank">luis</a> 2009-07-04 09:27 <a href="http://www.cppblog.com/luyulaile/archive/2009/07/04/89209.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>joj 1966  Super Market III 带有期限的作业排序问题 贪心</title><link>http://www.cppblog.com/luyulaile/archive/2009/07/02/89066.html</link><dc:creator>luis</dc:creator><author>luis</author><pubDate>Thu, 02 Jul 2009 05:11:00 GMT</pubDate><guid>http://www.cppblog.com/luyulaile/archive/2009/07/02/89066.html</guid><wfw:comment>http://www.cppblog.com/luyulaile/comments/89066.html</wfw:comment><comments>http://www.cppblog.com/luyulaile/archive/2009/07/02/89066.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luyulaile/comments/commentRss/89066.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luyulaile/services/trackbacks/89066.html</trackback:ping><description><![CDATA[<div class=prob_text>
<p>A supermarket has a set Prod of products on sale. It earns a profit px for each product x&#8712;Prod sold by a deadline dx that is measured as an integral number of time units starting from the moment the sale begins. Each product takes precisely one unit of time for being sold. A selling schedule is an ordered subset of products Sell &#8804; Prod such that the selling of each product x&#8712;Sell, according to the ordering of Sell, completes before the deadline dx or just when dx expires. The profit of the selling schedule is Profit(Sell)=&#931;<sub>x&#8712;Sell</sub>px. An optimal selling schedule is a schedule with a maximum profit. <br>For example, consider the products Prod={a,b,c,d} with (pa,da)=(50,2), (pb,db)=(10,1), (pc,dc)=(20,2), and (pd,dd)=(30,1). The possible selling schedules are listed in table 1. For instance, the schedule Sell={d,a} shows that the selling of product d starts at time 0 and ends at time 1, while the selling of product a starts at time 1 and ends at time 2. Each of these products is sold by its deadline. Sell is the optimal schedule and its profit is 80. <br>
<center><img src="http://acm.jlu.edu.cn/joj/images/problems/1966_1.jpg"></center><br>Write a program that reads sets of products from an input text file and computes the profit of an optimal selling schedule for each set of products. <br>
<p>&nbsp;</p>
<h3>Input</h3>
<p>A set of products starts with an integer 0 &lt;= n &lt;= 10000, which is the number of products in the set, and continues with n pairs pi di of integers, 1 &lt;= pi &lt;= 10000 and 1 &lt;= di &lt;= 10000, that designate the profit and the selling deadline of the i-th product. White spaces can occur freely in input. Input data terminate with an end of file and are guaranteed correct. </p>
<h3>Output</h3>
<p>For each set of products, the program prints on the standard output the profit of an optimal selling schedule for the set. Each result is printed from the beginning of a separate line.</p>
<h3>Sample Input</h3>
<p>
<pre>4  50 2  10 1   20 2   30 1
7  20 1   2 1   10 3  100 2   8 2
5 20  50 10
</pre>
<p>&nbsp;</p>
<h3>Sample Output</h3>
<p>
<pre>80
185</pre>
<p>&nbsp;</p>
<h3>Hint</h3>
<p>The sample input contains two product sets. The first set encodes the products from table 1. The second set is for 7 products. The profit of an optimal schedule for these products is 185.</p>
</div>
<p><br>#include&lt;iostream&gt;<br>#include&lt;cstdlib&gt;<br>using namespace std;<br>#define MAX 10001<br>#define min(a,b) ((a)&lt;(b) ? (a) : (b))<br>int father[MAX];<br>int p[MAX];<br>int result[MAX];<br>struct job{<br>&nbsp;&nbsp;&nbsp;&nbsp; int value;<br>&nbsp;&nbsp;&nbsp;&nbsp; int T;<br>&nbsp;&nbsp;}JOB[MAX];<br>bool&nbsp;operator &lt;(job job1,job job2)<br>&nbsp;{<br>&nbsp;&nbsp;if(job1.value&gt;job2.value)<br>&nbsp;&nbsp;return true;<br>&nbsp;&nbsp;else<br>&nbsp;&nbsp;return false; <br>&nbsp;}&nbsp;<br>&nbsp;int find(int x)&nbsp; //&#183;&#181;&#187;&#216;&#181;&#218;&#163;&#216;&#189;&#218;&#181;&#227;&#203;&#249;&#202;&#244;&#188;&#175;&#186;&#207;&#181;&#196;&#184;&#249;&#189;&#225;&#181;&#227;<br>&nbsp; {<br>int px=x;<br>while(p[px]&gt;=0)<br>&nbsp;&nbsp; px=p[px];<br>int tmp;<br>while(p[x]&gt;=0)//&#211;&#166;&#206;&#170;&#179;&#245;&#214;&#181;&#206;&#170;&#184;&#186; <br>{<br>&nbsp;&nbsp; tmp=p[x];<br>&nbsp;&nbsp; p[x]=px;<br>&nbsp;&nbsp; x=tmp;<br>}<br>return px;<br>}</p>
<p>&nbsp;&nbsp; void UNION(int x,int y)<br>{<br>x=find(x);<br>y=find(y);<br>if(x==y)<br>&nbsp;&nbsp; return ;<br>int tmp=p[x]+p[y];<br>if(p[x]&gt;p[y])<br>{<br>&nbsp;&nbsp; p[y]=tmp;<br>&nbsp;&nbsp; p[x]=y;<br>}<br>else<br>{<br>&nbsp;&nbsp; p[x]=tmp;<br>&nbsp;&nbsp; p[y]=x;<br>}<br>}<br>&nbsp; <br>&nbsp; int main()<br>&nbsp; {<br>&nbsp; freopen("s.txt","r",stdin);<br>&nbsp; freopen("key.txt","w",stdout);<br>&nbsp; int num,temp=0;<br>&nbsp; while(cin&gt;&gt;num)<br>&nbsp; {<br>&nbsp;&nbsp;memset(result,0,num);<br>&nbsp;&nbsp;int i,l,j,k=0;<br>&nbsp;&nbsp;for( i=0;i&lt;num;i++)<br>&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;cin&gt;&gt;JOB[i].value&gt;&gt;JOB[i].T;<br>&nbsp;&nbsp;&nbsp;father[i]=i;<br>&nbsp;&nbsp;&nbsp;p[i]=-1;<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;sort(JOB,JOB+num);<br>&nbsp;&nbsp;for(i=0;i&lt;num;i++)<br>&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;j=find(min(JOB[i].T,num-1));//<br>&nbsp;&nbsp;&nbsp;if(father[j]!=0)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k++;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result[k]=i;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; l=find(father[j]-1);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; UNION(l,j);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; father[j]=father[l];<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;i=0;<br>&nbsp;&nbsp;for(j=1;j&lt;=k;j++)<br>&nbsp;&nbsp;{&nbsp; <br>&nbsp;&nbsp;&nbsp;i+=JOB[result[j]].value;}<br>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;i&lt;&lt;endl;<br>&nbsp;&nbsp; }</p>
<p>&nbsp; //system("PAUSE");<br>&nbsp; return&nbsp;&nbsp; 0;<br>&nbsp; }<br>对着课本写得，自己都看不怎么懂。</p>
<img src ="http://www.cppblog.com/luyulaile/aggbug/89066.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luyulaile/" target="_blank">luis</a> 2009-07-02 13:11 <a href="http://www.cppblog.com/luyulaile/archive/2009/07/02/89066.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>joj 1157 Station Balance 水</title><link>http://www.cppblog.com/luyulaile/archive/2009/07/01/88970.html</link><dc:creator>luis</dc:creator><author>luis</author><pubDate>Wed, 01 Jul 2009 02:54:00 GMT</pubDate><guid>http://www.cppblog.com/luyulaile/archive/2009/07/01/88970.html</guid><wfw:comment>http://www.cppblog.com/luyulaile/comments/88970.html</wfw:comment><comments>http://www.cppblog.com/luyulaile/archive/2009/07/01/88970.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luyulaile/comments/commentRss/88970.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luyulaile/services/trackbacks/88970.html</trackback:ping><description><![CDATA[#include&lt;iostream&gt;<br>#include&lt;cstdlib&gt;<br>#include&lt;iomanip&gt;<br>#include&lt;algorithm&gt; <br>#include&lt;math.h&gt;<br>using namespace std;<br><br>&nbsp; int main()<br>&nbsp; {<br>//freopen("s.txt","r",stdin);<br>// freopen("key.txt","w",stdout);<br>&nbsp; int n,m;<br>&nbsp; int a[12];<br>&nbsp; int time=0;<br>&nbsp; while(cin&gt;&gt;n&gt;&gt;m)<br>&nbsp; {<br>int i,j;<br>double k;<br>memset(a,0,sizeof(a));<br>time++;<br>double sum=0.0;<br>double temp=0.0;<br>for(i=0;i&lt;m;i++)<br>{<br>cin&gt;&gt;a[i];<br>sum+=a[i];<br>}<br>sum/=n;<br>sort(a,a+2*n);//????a[0]????a[2n-1] <br>for(j=0;j&lt;n;j++)<br>{<br>k=a[j]+a[2*n-j-1]-sum;<br>if(k&gt;0)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; temp+=k;<br>&nbsp;&nbsp;&nbsp; else<br>&nbsp;&nbsp;&nbsp;&nbsp; temp-=k;<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;"Set #"&lt;&lt;time&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout&lt;&lt;"IMBALANCE = "&lt;&lt;fixed&lt;&lt;setprecision(5)&lt;&lt;temp&lt;&lt;endl&lt;&lt;endl;<br>&nbsp; }<br><br>&nbsp; //system("PAUSE");<br>&nbsp; return&nbsp;&nbsp; 0;<br>&nbsp; }<br>1，设计到减法注意是否是double类型<br>2，要不要绝对值，abs(),include&lt;math.h&gt;<br>3,cout&lt;&lt;fixed&lt;&lt;setprecision(5)能保障小数点后有（5）几位小数 
<img src ="http://www.cppblog.com/luyulaile/aggbug/88970.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luyulaile/" target="_blank">luis</a> 2009-07-01 10:54 <a href="http://www.cppblog.com/luyulaile/archive/2009/07/01/88970.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>joj 2387 Fix the stick贪心算法，用到了排序优化</title><link>http://www.cppblog.com/luyulaile/archive/2009/05/16/83111.html</link><dc:creator>luis</dc:creator><author>luis</author><pubDate>Sat, 16 May 2009 03:41:00 GMT</pubDate><guid>http://www.cppblog.com/luyulaile/archive/2009/05/16/83111.html</guid><wfw:comment>http://www.cppblog.com/luyulaile/comments/83111.html</wfw:comment><comments>http://www.cppblog.com/luyulaile/archive/2009/05/16/83111.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luyulaile/comments/commentRss/83111.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luyulaile/services/trackbacks/83111.html</trackback:ping><description><![CDATA[<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">cstdlib</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;MIN;<br></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;P<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;&nbsp;b;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;e;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;visited;<br>}arr[</span><span style="COLOR: #000000">10000</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;comp(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">arg1,&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">arg2)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;P&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)arg1).e&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;P&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)arg2).e;<br>}<br><br><br>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;func(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;LEN,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;N,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sb)</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">LEN是最右边那个点，N是界限，i是开始支持的点，Nsb是次数&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;MIN</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">sb;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;start</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">LEN;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(LEN</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">N)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(arr[i].e</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">LEN)</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(arr[i].b</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">start)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;start</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">arr[i].b;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;func(start,N,i,sb</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br>&nbsp;&nbsp;{<br></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">freopen("s.txt","r",stdin);<br></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">freopen("key.txt","w",stdout);&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;LEN,N,j,temp1,temp2;<br>&nbsp;&nbsp;cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">LEN</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">N;<br>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(LEN</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MIN</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">10000</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;j</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">N;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">temp1</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">temp2;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr[j].b</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">temp1</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">temp2;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr[j].e</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">temp1</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">temp2;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;qsort(arr,&nbsp;N,&nbsp;</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(arr[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]),&nbsp;comp);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;func(LEN,N,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">MIN</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">LEN</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">N;<br>&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">system("PAUSE");</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;}<br></span></div>
<img src ="http://www.cppblog.com/luyulaile/aggbug/83111.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luyulaile/" target="_blank">luis</a> 2009-05-16 11:41 <a href="http://www.cppblog.com/luyulaile/archive/2009/05/16/83111.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>