HDU5802-windows 10-dfs+贪心

音量减的时候,分两种,一种是减到大于目标M,另一种是减到小于M,停顿的时候可以减少最后往上加的次数,小于0的时候变成0

然后比一下这两种的最小值。

 1 /*--------------------------------------------------------------------------------------*/
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <cstring>
 5 #include <ctype.h>
 6 #include <cstdlib>
 7 #include <cstdio>
 8 #include <vector>
 9 #include <string>
10 #include <queue>
11 #include <stack>
12 #include <cmath>
13 #include <set>
14 #include <map>
15
16 //debug function for a N*M array
17 #define debug_map(N,M,G) printf("\n");for(int i=0;i<(N);i++)18 {for(int j=0;j<(M);j++){19 printf("%d",G[i][j]);}printf("\n");}
20 //debug function for int,float,double,etc.
21 #define debug_var(X) cout<<#X"="<<X<<endl;
22 #define LL long long
23 const int INF = 0x3f3f3f3f;
24 const int LLINF = 0x3f3f3f3f3f3f3f3f;
25 /*--------------------------------------------------------------------------------------*/
26 using namespace std;
27
28 LL N,M,ans;
29 int T;
30
31 LL sum(int x) {return (1LL<<x) - 1LL;}
32
33 LL dfs(LL cur,LL cnt,int stop)
34 {
35     //printf("cur:%d cnt:%d\n",cur,cnt);
36     if(cur == M) return cnt;
37     int i = 0;
38     while(cur-sum(i) > M) i++;
39
40     if(cur -sum(i) == M ) return cnt + i;
41     LL up =  M - max(0LL,cur-sum(i) );
42     LL res = i + max(0LL,up-stop);
43     return min(cnt+res , dfs(cur-sum(i-1) , cnt + i,stop+1));
44 }
45
46 int main()
47 {
48     scanf("%d",&T);
49     while(T--)
50     {
51         ans = LLINF;
52         scanf("%I64d%I64d",&N,&M);
53         if(N <= M)
54         {
55             printf("%I64d\n",M-N);
56             continue;
57         }
58
59         printf("%I64d\n",dfs(N,0,0));
60     }
61 }
时间: 2024-10-29 10:48:49

HDU5802-windows 10-dfs+贪心的相关文章

hdu5802 Windows 10 贪心

Windows 10 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2096    Accepted Submission(s): 630 Problem Description Long long ago, there was an old monk living on the top of a mountain. Recently,

hdu-5802 Windows 10(贪心)

题目链接: Windows 10 Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Problem Description Long long ago, there was an old monk living on the top of a mountain. Recently, our old monk found the operating system of his c

hdu 5802 Windows 10 (dfs)

Windows 10 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2191    Accepted Submission(s): 665 Problem Description Long long ago, there was an old monk living on the top of a mountain. Recently,

hdu 5802 Windows 10(2016 Multi-University Training Contest 6——贪心+dfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5802 Windows 10 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1005    Accepted Submission(s): 333 Problem Description Long long ago, there was a

hdu 5802 Windows 10

官方题解: Windows 10 _您可能是正版Windows 10的受害者_ 直接贪心就好 比较直观的看法是使劲往下降,然后升回来 或者使劲往下降然后停顿然后再使劲往下降... 于是就能将问题变成一个子问题,然后dfs就好 需要注意的是由于按up键也可以打断连续向下的功效 所以应该记录停顿了几次,以后向上的时候用停顿补回来 /*by*/ #include <iostream> #include <algorithm> #include <cstdio> #includ

2016暑假多校联合---Windows 10

2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on the top of a mountain. Recently, our old monk found the operating system of his computer was updating to windows 10 automatically and he even can't j

E - Game 树上dfs贪心

https://vjudge.net/contest/299785#problem/E 题意:给你n个点,n-1个边构成有向数,同时每个点都有一个权值,现在给你k次操作,每次操作你必须从根节点1出发,然后走到一个叶节点结束然后将它们点的权值累加,同时走过的点的权值不可以重复计算,问你k次之后最大得到多大的值. 做法:我们倒着存边,两次dfs第一次求每个点到根节点的权值,然后将这个权值从大到小排序,第二次dfs贪心遍历每个从大到小排好序的节点,最后将得到的遍历结果再从大到小排序,取前k个数求和.

Install Hyper-V on Windows 10

? Enable Hyper-V to create virtual machines on Windows 10.Hyper-V can be enabled in many ways including using the Windows 10 control panel, PowerShell (my favorite) or using the Deployment Imaging Servicing and Management tool (DISM). This documents

Windows 10 UWP程序标题栏设置

原文:Windows 10 UWP程序标题栏设置 在Windows 10程序中,以前只能用于全屏方式的Metro程序现在可以运行在窗口模式下了,并且改了个新名字,叫Windows 通用程序(Universal Windows app),简称UWP程序.新的UWP程序虽然大体上还是和以前的Metro程序差不多的,但还是引入了一点新东西的,本文这里就介绍一下它的标题栏设置的几个特性. 隐藏标题栏: 将应用界面扩展至 Titlebar 区域 CoreApplication.GetCurrentView

How to enable C development in a Windows 10 development environment VM

To enable C development in a Windows 10 development environment VM, follow these steps: Start VS in the Windows 10 development environment VM. Choose "File" -> "New" -> "Project", choose "Open Visual Studio Install