ACM第七次测验(图论)

 poj 1190 生日蛋糕
Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 17882
Accepted: 6363

Description

7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体。 
设从下往上数第i(1 <= i <= M)层蛋糕是半径为Ri, 高度为Hi的圆柱。当i < M时,要求Ri > Ri+1且Hi > Hi+1。 
由于要在蛋糕上抹奶油,为尽可能节约经费,我们希望蛋糕外表面(最下一层的下底面除外)的面积Q最小。 
令Q = Sπ 
请编程对给出的N和M,找出蛋糕的制作方案(适当的Ri和Hi的值),使S最小。 
(除Q外,以上所有数据皆为正整数) 

Input

有两行,第一行为N(N <= 10000),表示待制作的蛋糕的体积为Nπ;第二行为M(M <= 20),表示蛋糕的层数为M。

Output

仅一行,是一个正整数S(若无解则S = 0)。

Sample Input

100
2

Sample Output

68

Hint

圆柱公式 
体积V = πR2
侧面积A' = 2πRH 
底面积A = πR2 
#include <stdio.h>  
#define INF 0xFFFFFF  
#define POW(x)  ( (x) * (x) )  
//计算∑n^3 = n^2 * n^2 * (n+1)^2 * (n+1)^2 * 4  
#define SUM3(x) ( ( POW( (x) ) * POW( (x) + 1 ) ) >> 2 )  
int V, F;//输入的总体积和蛋糕的层数  
int ans;//最终的最小面积,也是搜索过程中的当前最小面积,搜索中不断更新  
  
int  
min( int a, int b ) {  
  return a < b ? a : b;  
}  
  
void  
dfs( int cur_f, int lft_v, int udr_s, int cur_r, int cur_h ) {  
    //cur_f,当前所处的层,从下往上进行搜索  
    //lft_v,当前还剩下多少体积没拼完  
    //udr_s,under_s,当前层以下(就是之前)已经拼出的表面积  
  
    int r, h;//当前层试探的半径和高度  
    int cir_s;//当前层的试探圆面积(由r决定)  
    if ( !cur_f ) {//已经到达顶层(顶层是1,超出顶层了)  
      //如果过剩余体积全部拼完,并且udr_s更优就跟新ans  
        if ( !lft_v && udr_s < ans ) ans = udr_s;  
        return ;  
    }  
    if ( udr_s >= ans ) return ;//剪枝1,如果当前拼出的面积以及大于前几次的最优解则不必再玩儿了  
     //剪枝2  
    //如果当前剩余体积连剩下可能的最小体积都达不到也退出  
    //因为题目要求下层一定要比上层半径和高度都大,因此一个m层的蛋糕,其最小体积为1^3 + 2^3...+ m^3  
    if ( lft_v < SUM3(cur_f) ) return;  
    //剪枝3,如果当前拼出的面积加上剩下可能拼出的最小面积比之前拼出的最小面积还要大也不必再玩儿了  
    //剩余可能拼出的最小面积( lft_v << 1 ) / cur_r其实就是剩余体积除以当前层的最大面积,cur_r就是当前层可能的最大半径  
    //其实就是将剩下的体积看成一个整个圆柱,这实际上不符合题意,但是这里使用的是不等式的放缩  
    if ( ( udr_s + ( lft_v << 1 ) / cur_r ) >= ans ) return ;  
     for ( r = cur_r; r >= cur_f; r-- ) {//从当前给定的半径开始搜索,半径最小不能炒股cur_f了  
      cir_s = r * r;  
        if ( cur_f == F ) udr_s = cir_s;//如果当前层是底层,需要算上俯瞰面积  
        //题中底面积不要求,但是要求俯瞰面积  
         //剪枝4  
        //可以通过( lft_v - SUM3( cur_f - 1 ) ) / cir_s得到一个该层高度的上界,避免多余的搜索  
        //因为上面的所有层最大可能体积就是SUM3( cur_f - 1 ),所以当前层可能的最大体积就是lft_v - SUM3( cur_f - 1 )  
        //除以一下cir_s就是最大可能的高度  
        h = min( cur_h, ( lft_v - SUM3( cur_f - 1 ) ) / cir_s );  
        for ( ; h >= cur_f; h-- )//高度最小不能超过cur_f  
            dfs( cur_f - 1,  
                 lft_v - cir_s * h,  
                 udr_s + ( ( r * h ) << 1 ),  
                 r - 1, h - 1 );  
    }  
}  
  int main() {  
     ans = INF;  
    scanf("%d%d", &V, &F);   
     //由于总体积最大为10000  
    //极端情况下就一层,如果高度为1,则半径达到最大100,如果半径为1则高度达到最大10000  
    dfs( F, V, 0, 100, 10000 );  
      if ( INF == ans ) puts("0");//无解  
    else printf("%d\n", ans);  
  return 0;  
}
poj 2369 Permutations
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 3015
Accepted: 1625

Description

We remind that the permutation of some final set is a one-to-one mapping of the set onto itself. Less formally, that is a way to reorder elements of the set. For example, one can define a permutation of the set {1,2,3,4,5} as follows: 
 
This record defines a permutation P as follows: P(1) = 4, P(2) = 1, P(3) = 5, etc. 
What is the value of the expression P(P(1))? It’s clear, that P(P(1)) = P(4) = 2. And P(P(3)) = P(5) = 3. One can easily see that if P(n) is a permutation then P(P(n)) is a permutation as well. In our example (believe us) 
 
It is natural to denote this permutation by P2(n) = P(P(n)). In a general form the defenition is as follows: P(n) = P1(n), Pk(n) = P(Pk-1(n)). Among the permutations there is a very important one — that moves nothing: 
 
It is clear that for every k the following relation is satisfied: (EN)k = EN. The following less trivial statement is correct (we won't prove it here, you may prove it yourself incidentally): Let P(n) be some permutation of an N elements set. Then there exists a natural number k, that Pk = EN. The least natural k such that Pk = EN is called an order of the permutation P. 
The problem that your program should solve is formulated now in a very simple manner: "Given a permutation find its order."

Input

In the first line of the standard input an only natural number N (1 <= N <= 1000) is contained, that is a number of elements in the set that is rearranged by this permutation. In the second line there are N natural numbers of the range from 1 up to N, separated by a space, that define a permutation — the numbers P(1), P(2),…, P(N).

Output

You should write an only natural number to the standard output, that is an order of the permutation. You may consider that an answer shouldn't exceed 109.

Sample Input

5
4 1 5 2 3

Sample Output

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值