UOJ136 开学前的作文

描述

红包是一个萌萌的男孩子。

红包由于 NOI 惨挂,直到前不久依然无心写作业。如今快开学了,他决定好好完成作业。

对于可以交电子稿的作文,红包有特殊的完成技巧,大致流程是依次选中一段内容→按下 Ctrl+C→按下 Ctrl+V

由于红包的键盘过于奇特,只有 Fn55 个按键能够移动光标,导致每次选中一段内容总要费上一番功夫。

按键功能
将光标向上移动一格
将光标向下移动一格
将光标向左移动一格
将光标向右移动一格
Fn 依次按下最近被物理按下的两个
例子:若依次按下 FnFn,则第一次按下 Fn 后等价于按下 ,第二次按下 Fn 后等价于依次按下

现在红包想要在按键次数尽可能少的情况下移动光标到第 nn 行第 mm 列。为了简化问题我们默认光标起始位置为第一行第一列。

输入格式

第一行一个正整数 TT,表示数据组数。

接下来 TT 行,每行两个正整数 n,mn,m,如题所述,表示光标的目标位置。

每组数据之间相互独立。

输出格式

对于每一个询问,输出一行一个整数 ansans,表示最小按键次数。

样例一

input

2
1 5
3 3

output

3
3

explanation

对于第一组输入,我们要将光标移至第一行第五列,依次按 Fn,即可。

对于第二组输入,依次按下 Fn,即可。

 

 

正解:模拟

解题报告:

  一道UER的题目居然搞了我这么久,醉了。。。  

  显然n==1 或者 m==1特判一下水过去。考虑二者均不为1的情况,首先肯定尽可能地每次走一个往右下的路线,所以可以算出能往右下走多远,然后就转换成了n==1 或者 m==1的情况了。

 

 1 //It is made by jump~
 2 #include <iostream>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <algorithm>
 8 #include <ctime>
 9 #include <vector>
10 #include <queue>
11 #include <map>
12 #include <set>
13 #ifdef WIN32   
14 #define OT "%I64d"
15 #else
16 #define OT "%lld"
17 #endif
18 using namespace std;
19 typedef long long LL;
20 int ans;
21 
22 inline int getint()
23 {
24        int w=0,q=0;
25        char c=getchar();
26        while((c<'0' || c>'9') && c!='-') c=getchar();
27        if (c=='-')  q=1, c=getchar();
28        while (c>='0' && c<='9') w=w*10+c-'0', c=getchar();
29        return q ? -w : w;
30 }
31 /*
32 inline int solve(int x){    
33     int ans;
34     if(x<=1) return 0;
35     else if(x<=2) ans=1;
36     else if(x<=3) ans=2;
37     else ans=2+(x-2)/2;
38     return ans;
39 }*/
40 
41 inline void work(){
42     int T=getint(); int x,y; 
43     while(T--) {
44     x=getint(); y=getint(); ans=0;
45     if(x>y) swap(x,y); x--; y--;
46     if(x==0) {
47         if(y<=1) ans=y;//特判只能走一次
48         else ans=1+(y+1)/2;
49     }
50     else {
51         ans=x;//共x次
52         y=y-x+1;
53         if(y<=1) ans+=y;
54         else ans+=(y+1)/2+1;
55     }
56     /*
57     if(x==1 && y==1) ans=0; 
58     else if(x==1) ans=solve(y);                                                                                                    
59     else{
60         now=min(x-1,y-1);
61         ans=2; ans+=now-1; x-=now; y-=now;
62         ans+=solve(y);
63         }*/
64     printf("%d\n",ans);
65     }
66 }
67 
68 int main()
69 {
70   work();
71   return 0;
72 }

 

posted @ 2016-08-28 22:49  ljh_2000  阅读(392)  评论(0编辑  收藏  举报