ch1 : Number Triangles


/ Published in: C++
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /*
  2.   NAME : Nursoltan
  3.   PROB : numtri
  4.   LANG : C++
  5.   DATE : 01/07/11 14:08
  6. */
  7. #include <algorithm>
  8. #include <bitset>
  9. #include <cctype>
  10. #include <cmath>
  11. #include <cstdio>
  12. #include <cstdlib>
  13. #include <cstring>
  14. #include <ctime>
  15. #include <sstream>
  16. #include <iostream>
  17. #include <map>
  18. #include <set>
  19. #include <stack>
  20. #include <utility>
  21. #include <queue>
  22.  
  23. using namespace std;
  24.  
  25. #define MAX 100
  26. #define INF INT_MAX
  27. #define eps (1e-9)
  28.  
  29. #define FOR(_i,_k,_n) for(int (_i)=(_k); (_i)<(_n); (_i)++)
  30. #define FORR(_i,_k,_n) for(int (_i)=(_k); (_i)>=(_n); (_i)--)
  31. #define CLR(_x) memset((_x),0,sizeof(_x))
  32. #define SQR(_x) ((_x)*(_x))
  33. #define all(_x) _x.begin(),_x.end()
  34. #define sz(_x) sizeof(_x)
  35.  
  36. #define vc vector<int>
  37. #define pb push_back
  38. #define mp make_pair
  39. #define iss istringstream
  40. #define oss ostringstream
  41. #define px first
  42. #define py second
  43.  
  44. typedef long long ll;
  45. typedef pair <int,int> point;
  46. int ABS(int _x){ return _x>0?_x:-_x; }
  47.  
  48. int main()
  49. {
  50. freopen("numtri.in","r",stdin);
  51. freopen("numtri.out","w",stdout);
  52.  
  53. int n,a[1001][1001];
  54. cin>>n;
  55.  
  56. FOR(i,0,n) FOR(j,0,i+1) cin>>a[i][j];
  57. FOR(i,1,n) a[i][0]+=a[i-1][0],a[i][i]+=a[i-1][i-1];
  58. FOR(i,2,n) FOR(j,1,i) a[i][j]+=max(a[i-1][j],a[i-1][j-1]);
  59.  
  60. cout<<(*max_element(a[n-1],a[n-1]+n))<<endl;
  61.  
  62. //system("pause");
  63. return 0;
  64. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.