A-HDU - 1008
Elevator
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 72929 Accepted Submission(s): 40148
For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.
1 2
3 2 3 1
0
17
41
[分析]
意思是电梯上一楼花6秒,下一楼花4秒,停下来花5秒。
计算具体行为所花费的时间。
分析在代码中
[代码]
#include<cstdio>
int main()
{
int a[105];
int n;
while(scanf("%d",&n)!=EOF)
{
if(!n)break;
int ans=0;
int dq=0;//当前楼层
int x;
for(int i=0;i<n;i++)
{
scanf("%d",&x);
if(x>dq)ans=ans+(x-dq)*6;//如果输入楼层大于当前楼层那么乘6
else ans=ans+(dq-x)*4;//小于乘4
ans=ans+5;//无论到哪,都停5秒;
dq=x;//更新当前楼层
}
printf("%d\n",ans);
}
}
B-HDU - 1003
Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 270669 Accepted Submission(s): 64346
Problem Description
Given a sequence a[1],a[2],a[3]……a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).
Output
For each test case, you should output two lines. The first line is “Case #:”, # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.
Sample Input
2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
Sample Output
Case 1:
14 1 4
Case 2:
7 1 6
[分析]
最大连续子序列
sum记录前缀和(之前数字的和)
将sum的数值画入坐标系后思路就很明朗了
找出一个min为当前最小的sum,
找出一个max为当前min之后的最大的sum(注意max一定要在min后面,为什么自己思考)
题目的含义要我们得到的最大数应该是max-(min的前面一个数)
所以我定义了一个premin,即max-premin,这个就是某个大序列的和,
我们只要比较每个可能的大序列的和就可以的到答案。
得到位置很简单,看代码就懂了。
[代码]
#include<cstdio>
#define MAX 0x3f3f3f3f
#define MIN -0x3f3f3f3f
int a[100005];
int main()
{
int t,kase=0;
a[0] = 0;
scanf("%d", &t);
while (t--)
{
int n,max=MIN,dif=MIN,sum=0,premin=MAX;
int AnsStarPos, AnsEndPos, NowStarPos, NowEndPos;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
if (sum < premin)
{
premin = sum;
max = MIN;
NowStarPos = i;
}
sum += a[i];
if (sum > max)
{
max = sum;
NowEndPos = i;
if (max - premin > dif)
{
dif = max - premin;
AnsStarPos = NowStarPos;
AnsEndPos = NowEndPos;
}
}
}
if (kase)printf("\n");
printf("Case %d:\n", ++kase);
printf("%d %d %d\n", dif,AnsStarPos,AnsEndPos);
}
}
D-HDU - 1998
奇数阶魔方
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4661 Accepted Submission(s): 2640
的方阵叫魔方。n为奇数时我们有1种构造方法,叫做“右上方” ,例如下面给出n=3,5,7时
的魔方.
3
8 1 6
3 5 7
4 9 2
5
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
7
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
第1行中间的数总是1,最后1行中间的数是n^2,他的右边是2,从这三个魔方,你可看出“右
上方”是何意。
2
3
5
8 1 6
3 5 7
4 9 2
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
[分析]
题目不难,就是按照题目的意思做就可以了。
分析在代码中。
[代码]
#include<cstdio>
#include<cstring>
int main()
{
int n;
int t;
int a[22][22];
//freopen("out.txt","w",stdout);
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(a,0,sizeof(a));
int x=0;
int y=n/2;//x,y初始定位在第零行的最中间
int t=n*n;//t是:有多少数字需要写;
int num=1;
while(t--)
{
a[x][y]=num++;
if(a[(x+n-1)%n][(y+n+1)%n]==0)//确定右上是否有数字(看不懂就多琢磨一下,有益处)
{
x=(x+n-1)%n;//确定x的位置
y=(y+n+1)%n;//确定y的位置
}
else x=(x+n+1)%n;//如果右上被占,那么移到下方
}
for(int i=0;i<n;i++)
{
int flag=1;
for(int j=0;j<n;j++)
{
printf("%4.d",a[i][j]);//题目中比较坑的,对其!!
}
printf("\n");
}
}
}
F-HDU - 1020
Encoding
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 46814 Accepted Submission(s): 20792
1. Each sub-string containing k same characters should be encoded to "kX" where "X" is the only character in this sub-string.
2. If the length of the sub-string is 1, ‘1’ should be ignored.
2
ABC
ABBCCC
ABC
A2B3C
[分析]
题目意思就是当一个字母连续出现时,就以连续出现的个数加这个字母替代原来在字符串中的位置。如ABBCCCA改为A2BCA。
[代码]
#include<cstdio>
#include<cstring>
int main()
{
int t;
char a[10005];
scanf("%d",&t);
while(t--)
{
scanf("%s",a);
int len=strlen(a);
int same=1;
for(int i=0;i<len;i++)
{
if(same==1&&a[i]!=a[i+1])printf("%c",a[i]);
else if(a[i]==a[i+1])
{
same++;
}
else if(same!=1&&a[i]!=a[i+1])
{
printf("%d%c",same,a[i]);
same=1;
}
}
printf("\n");
}
}