URAL 1079. Maximum
2007-11-04 22:01
260 查看
problem url: http://acm.timus.ru/problem.aspx?space=1&num=1079
this should be a mathematics question. So far I got two answers. MY own is using static array to cache all the sequence elements while there is another mathematic way which dosn't require Array or cache(of each sequence element):
#simple approach(not good at scalability):
#include <cstdio>
#include <iostream>
#define MAXNUM 100000
using namespace std;
//GLOBAL VARIABLES
int NUMS[MAXNUM/2+2]; //each item of the sequence
int curmax = 0;
int inputs[10];
int maxs[10];
int inputcnt = 0;
int maxinput = 0;
void main()
...{
int i =0,j=0;
int curinput=0;
do
...{
scanf("%d", &curinput);
inputs[inputcnt++] = curinput;
if(curinput > maxinput) maxinput = curinput;
}while(curinput != 0);
curmax =0;
int curitem = 0;
for(i=0;i<= maxinput;++i)
...{
if(i==0) curitem = 0;
else if(i == 1) curitem = 1;
else
...{
if(i%2)
...{
curitem = NUMS[i/2] + NUMS[i/2 +1];
}else
...{
curitem = NUMS[i/2];
}
}
if(curmax < curitem) curmax = curitem;
if(i<(MAXNUM/2+2)) NUMS[i] = curitem;
for(j=0;j<inputcnt-1;++j)
...{
if(inputs[j] == i) maxs[j] = curmax;
}
}
for(i=0;i<inputcnt-1;++i) printf("%d ", maxs[i]);
}
#pure mathematics approach(can not understand yet :( ):
/*
Assume g(n,i,j)=i*f(n)+j*f(n+1).
Then
g(2n,i,j)=g(n,i+j,j)
g(2n+1,i,j)=g(n,i,i+j)
We must find f(n)=g(n,1,0), so...
*/
#include <iostream>
int n,x;
int a[10];m
int i,t;
int _max(int s1,int s2,int x)
{
if (x==n)
return s1+s2;
else {
int t1,t2;
if (x*2-1<=n)
t1 = _max(s1,s2+s1,x*2-1);
else
t1 = 0;
if (x*2+1<=n)
t2 = _max(s1+s2,s2,x*2+1);
else
t2 = 0;
if ((t1==t2)&&(t2==0))
return s1+s2;
else
return t1>t2?t1:t2;
}
}
int main()
{
std::cin >> n;
i = 0;
while (n){
if (n==2)
a[i] = 1;
else
if (n==1)
a[i] = 1;
else
if (n==0)
a[i] = 0;
else
a[i] = _max(1,1,3);
std::cin >> n;
i++;
}
for (n = 0;n<i;n++)
std::cout << a
<< " ";
return 0;
}
this should be a mathematics question. So far I got two answers. MY own is using static array to cache all the sequence elements while there is another mathematic way which dosn't require Array or cache(of each sequence element):
#simple approach(not good at scalability):
#include <cstdio>
#include <iostream>
#define MAXNUM 100000
using namespace std;
//GLOBAL VARIABLES
int NUMS[MAXNUM/2+2]; //each item of the sequence
int curmax = 0;
int inputs[10];
int maxs[10];
int inputcnt = 0;
int maxinput = 0;
void main()
...{
int i =0,j=0;
int curinput=0;
do
...{
scanf("%d", &curinput);
inputs[inputcnt++] = curinput;
if(curinput > maxinput) maxinput = curinput;
}while(curinput != 0);
curmax =0;
int curitem = 0;
for(i=0;i<= maxinput;++i)
...{
if(i==0) curitem = 0;
else if(i == 1) curitem = 1;
else
...{
if(i%2)
...{
curitem = NUMS[i/2] + NUMS[i/2 +1];
}else
...{
curitem = NUMS[i/2];
}
}
if(curmax < curitem) curmax = curitem;
if(i<(MAXNUM/2+2)) NUMS[i] = curitem;
for(j=0;j<inputcnt-1;++j)
...{
if(inputs[j] == i) maxs[j] = curmax;
}
}
for(i=0;i<inputcnt-1;++i) printf("%d ", maxs[i]);
}
#pure mathematics approach(can not understand yet :( ):
/*
Assume g(n,i,j)=i*f(n)+j*f(n+1).
Then
g(2n,i,j)=g(n,i+j,j)
g(2n+1,i,j)=g(n,i,i+j)
We must find f(n)=g(n,1,0), so...
*/
#include <iostream>
int n,x;
int a[10];m
int i,t;
int _max(int s1,int s2,int x)
{
if (x==n)
return s1+s2;
else {
int t1,t2;
if (x*2-1<=n)
t1 = _max(s1,s2+s1,x*2-1);
else
t1 = 0;
if (x*2+1<=n)
t2 = _max(s1+s2,s2,x*2+1);
else
t2 = 0;
if ((t1==t2)&&(t2==0))
return s1+s2;
else
return t1>t2?t1:t2;
}
}
int main()
{
std::cin >> n;
i = 0;
while (n){
if (n==2)
a[i] = 1;
else
if (n==1)
a[i] = 1;
else
if (n==0)
a[i] = 0;
else
a[i] = _max(1,1,3);
std::cin >> n;
i++;
}
for (n = 0;n<i;n++)
std::cout << a
<< " ";
return 0;
}
相关文章推荐
- ural 1079. Maximum
- URAL 1557 连通性
- URAL 1709 Penguin-Avia
- URAL 1221 Malevich Strikes Back! (DP动态规划 O(n^2) )
- ural 1217. Unlucky Tickets
- URAL 1018 Binary Apple Tree (树形DP)
- URAL 1018 Binary Apple Tree (树形DP)
- URAL 2040 Palindromes and Super Abilities 2
- URAL 1204 Idempotents
- URAL 1289 One Way Ticket
- ural 1244. Gentlemen
- ural 1431. Diplomas
- URAL 1684 Jack's Last Word
- ural 1338. Automobiles
- URAL 1128 Partition into Groups
- URAL 1145—— Rope in the Labyrinth——————【求树的直径】
- URAL 1252 ——Sorting the Tombstones——————【gcd的应用】
- ural 1075. Thread in a Space
- ural 1250. Sea Burial
- Ural 1073 Square Country (DP)