HDU 5328_Problem Killer
2015-07-30 19:53
357 查看
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 299 Accepted Submission(s): 116
Problem Description
You are a "Problem Killer", you want to solve many problems.
Now you have n problems,
the i-th
problem's difficulty is represented by an integer ai (1≤ai≤109).
For some strange reason, you must choose some integer l and r (1≤l≤r≤n),
and solve the problems between the l-th
and the r-th,
and these problems' difficulties must form an AP (Arithmetic Progression) or a GP (Geometric Progression).
So how many problems can you solve at most?
You can find the definitions of AP and GP by the following links:
https://en.wikipedia.org/wiki/Arithmetic_progression https://en.wikipedia.org/wiki/Geometric_progression
Input
The first line contains a single integer T,
indicating the number of cases.
For each test case, the first line contains a single integer n,
the second line contains n integers a1,a2,⋯,an.
T≤104,∑n≤106
Output
For each test case, output one line with a single integer, representing the answer.
Sample Input
2
5
1 2 3 4 6
10
1 1 1 1 1 1 2 3 4 5
Sample Output
4
6
意思是给出你一个数组,判断里面最长的一个区间,区间里的数要么为等比数列,要么为等差数列,也可以两个都是。
这道题因为在开头那里加了个memset,结果TLE了好几发,后来去了以后就A了,主要就是一边往后扫,分别用两个数来记录等差数列的差值和等比数列的比值,不断比较前面一个和后面一个的差值和比值,如果不相等,更新区间长度和差值或比值。
#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
#include<math.h>
#define LL long long
using namespace std;
const LL mod = 1e9 + 7;
const double PI = acos(-1.0);
const double E = exp(1.0);
const int M = 1e6 + 5;
double a[M];
int main()
{
int t;
int n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=0;i<n;i++) scanf("%lf",&a[i]);
if(n==1)
{
puts("1");
continue;
}
double ans=a[1]-a[0];
double nut=a[1]/a[0];
int sum1=2;
int sum2=2;
int count=2;
for(int i=2;i<n;i++)
{
if(ans==a[i]-a[i-1]) sum1++;
else
{
ans=a[i]-a[i-1];
count=count>sum1?count:sum1;
sum1=2;
}
if(nut==a[i]/a[i-1]) sum2++;
else
{
nut=a[i]/a[i-1];
count=count>sum2?count:sum2;
sum2=2;
}
}
count=count>sum1?count:sum1;
count=count>sum2?count:sum2;
printf("%d\n",count);
}
return 0;
}
Total Submission(s): 299 Accepted Submission(s): 116
Problem Description
You are a "Problem Killer", you want to solve many problems.
Now you have n problems,
the i-th
problem's difficulty is represented by an integer ai (1≤ai≤109).
For some strange reason, you must choose some integer l and r (1≤l≤r≤n),
and solve the problems between the l-th
and the r-th,
and these problems' difficulties must form an AP (Arithmetic Progression) or a GP (Geometric Progression).
So how many problems can you solve at most?
You can find the definitions of AP and GP by the following links:
https://en.wikipedia.org/wiki/Arithmetic_progression https://en.wikipedia.org/wiki/Geometric_progression
Input
The first line contains a single integer T,
indicating the number of cases.
For each test case, the first line contains a single integer n,
the second line contains n integers a1,a2,⋯,an.
T≤104,∑n≤106
Output
For each test case, output one line with a single integer, representing the answer.
Sample Input
2
5
1 2 3 4 6
10
1 1 1 1 1 1 2 3 4 5
Sample Output
4
6
意思是给出你一个数组,判断里面最长的一个区间,区间里的数要么为等比数列,要么为等差数列,也可以两个都是。
这道题因为在开头那里加了个memset,结果TLE了好几发,后来去了以后就A了,主要就是一边往后扫,分别用两个数来记录等差数列的差值和等比数列的比值,不断比较前面一个和后面一个的差值和比值,如果不相等,更新区间长度和差值或比值。
#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
#include<math.h>
#define LL long long
using namespace std;
const LL mod = 1e9 + 7;
const double PI = acos(-1.0);
const double E = exp(1.0);
const int M = 1e6 + 5;
double a[M];
int main()
{
int t;
int n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=0;i<n;i++) scanf("%lf",&a[i]);
if(n==1)
{
puts("1");
continue;
}
double ans=a[1]-a[0];
double nut=a[1]/a[0];
int sum1=2;
int sum2=2;
int count=2;
for(int i=2;i<n;i++)
{
if(ans==a[i]-a[i-1]) sum1++;
else
{
ans=a[i]-a[i-1];
count=count>sum1?count:sum1;
sum1=2;
}
if(nut==a[i]/a[i-1]) sum2++;
else
{
nut=a[i]/a[i-1];
count=count>sum2?count:sum2;
sum2=2;
}
}
count=count>sum1?count:sum1;
count=count>sum2?count:sum2;
printf("%d\n",count);
}
return 0;
}
相关文章推荐
- HDU 1568
- HDU1290
- HDU1568(Fobonacci公式)
- HDU ACM Step 2.2.2 Joseph(约瑟夫环问题)
- HDU 1405
- HDU 1297
- hdu 1205
- hdu 2087
- hdu 1016
- HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
- HDU 1000
- HDU 1001
- HDU 1622 Trees On The Level
- HDU 1063 Exponentiation
- Hdu5033
- HDU 1166 敌兵布阵
- HDU Rightmost Digit
- hdu 1002
- 贪心 hdu 1003
- hdu 1004