Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. // 이상한 토너먼트
  2.  
  3. #include <iostream>
  4. #include <cstring>
  5. using namespace std;
  6. #include <fstream>
  7. #include <cstdio>
  8. #include <cmath>
  9. #include <ctime>
  10. #include <vector>
  11. #include <stack>
  12. #include <queue>
  13. #include <functional>
  14. #include <deque>
  15. #include <numeric>
  16. #include <set>
  17. #include <climits>
  18. #include <utility>
  19. #include <map>
  20. #include <algorithm>
  21. #define INF 987654321
  22. typedef long long ll;
  23. typedef unsigned long long ull;
  24. inline int max( int x, int y ){ return x > y ? x : y ; }
  25. inline int min( int x, int y ){ return x < y ? x : y ; }
  26. inline ll max( ll x, ll y ){ return x > y ? x : y ; }
  27. inline ll min( ll x, ll y ){ return x < y ? x : y ; }
  28. inline ull max( ull x, ull y ){ return x > y ? x : y ; }
  29. inline ull min( ull x, ull y ){ return x < y ? x : y ; }
  30.  
  31.  
  32. int main(){
  33.  
  34. int N, arr[500];
  35. int dp[500][500];
  36. int ans[500][500];
  37.  
  38. scanf("%d", &N);
  39. memset( arr, 0, sizeof(arr) );
  40. memset( dp, 0, sizeof(dp) );
  41. memset( ans, 0, sizeof(ans) );
  42.  
  43. for( int i=0; i<N; ++i ){
  44. scanf("%d", arr+i );
  45. }
  46.  
  47. for( int i=0; i<N; ++i ){
  48. for( int j=0; j<N; ++j ){
  49. if( i > j ) continue;
  50. if( i == j ){
  51. dp[i][j] = arr[i];
  52. }else{
  53. dp[i][j] = max( dp[i][j-1], arr[j] );
  54. ans[i][j] = INT_MAX;
  55. }
  56. }
  57. }
  58.  
  59. for( int i=N-1; i>=0; --i ){
  60. for( int j=0; j<N; ++j ){
  61. for( int k=i; k<j; ++k ){
  62. if( i>=j ) continue;
  63. ans[i][j] = min( ans[i][j], ans[i][k]+ans[k+1][j]+abs(dp[i][k]-dp[k+1][j]));
  64. }
  65. }
  66. }
  67.  
  68. printf("%d", ans[0][N-1]);
  69. return 0;
  70. }
  71.  
  72.  
  73. // arr[i] : 입력받을 배열
  74. // dp[i][j] : i to j까지 중에서 최댓값을 담을 배열, dp[i][j]는 arr[j]랑 dp[i][j-1]중에 최대값을 구하는 방식으로 갱신.
  75. // dp배열은 그냥 세그먼트트리 구현해서 풀어도 상관 X
  76. // ans[i][j] : i to j까지 경기를 진행했을 때 관중들의 지루함 값의 최소값. 메모이제이션과 재귀로 구현해도 되는데 위는 O(N^3)으로 구현함.
  77. // i to j까지의 최소값을 구하려면 i<=k<j 일때 i to k에서 올라온 선수와 k+1 to j에서 올라온 선수와 경기를 붙어서 나온 결과들을 계속 갱신해야함.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement