답안 #68938

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
68938 2018-08-19T10:21:33 Z Navick Shortcut (IOI16_shortcut) C++17
0 / 100
3 ms 584 KB
#include <bits/stdc++.h>
#include "shortcut.h"

#define F first
#define S second
#define pii pair<int, int>
#define pb push_back

using namespace std;

const int maxN = 510;

typedef long long ll;
typedef long double ld;

const ll oo = 1e18;

ll dp[maxN][maxN], pf[maxN], sf[maxN], ps[maxN];
ll h1[maxN][maxN], h2[maxN][maxN];

ll f[maxN], g[maxN];

long long find_shortcut(int n, std::vector<int> l, std::vector<int> d, int c)
{

	if(n >= maxN) cout << 1/0;

	for (int i=1; i<n; i++) ps[i] = ps[i - 1] + l[i - 1];

	for (int i=0; i<n; i++)
	{
		if(i) pf[i] = pf[i - 1] + l[i - 1];
		pf[i] = max(pf[i], (ll)d[i]);
	}

	for (int i=n-1; i>=0; i--)
	{
		if(i < n - 1) sf[i] = sf[i + 1] + l[i];
		sf[i] = max(sf[i], (ll)d[i]);
	}

	for (int len=1; len<=n; len++)
		for (int i=0; i+len<=n; i++)
		{
			int j = i + len - 1;
			if(len == 1) h1[i][i] = (ll)d[i] + ps[i], h2[i][i] = (ll)d[i] - ps[i];
			else
			{
				h1[i][j] = max(h1[i][j - 1], h1[i + 1][j]);
				h2[i][j] = max(h2[i][j - 1], h2[i + 1][j]);
			}
		}

	for (int i=0; i<n; i++)
	{
		if(i) f[i] = f[i - 1];
		if(i)f[i] = max(f[i], d[i] + ps[i] + h2[0][i - 1]);
	
		//cout << i << " ... " << f[i] << endl;

	}

	for (int i=n-1; i>=0; i--)
	{
		if(i + 1 < n) g[i] = g[i + 1];
		if(i + 1 < n) g[i] = max(g[i], d[i] - ps[i] + h1[i + 1][n - 1]);
		
		//cout << i << " ... " << g[i] << endl;

	}


	ll ans = 1e18;

	for (int i=0; i<n; i++) ans = max(ans, (ll)d[i]);
	
	for (int i=0; i<n; i++)
		for (int j=i+1; j<n; j++)
		{

			//cout << " SOLVING " << i << ',' << j << endl;

			dp[i][j] = pf[i] + sf[j] + min((ll)c, ps[j] - ps[i]);

			dp[i][j] = max(dp[i][j], max(f[i], g[j]));
			
			ll len = ps[j] - ps[i] + c;
			
			vector <pii> vc;

			//if(i == 1 && j == 2) cout << pf[i] << ',' << sf[j] << " AND " << f[i] << endl;
			//if(i == 1 && j == 2) cout << ',' << g[j] << endl << " CURR " << dp[i][j] << endl;

			for (int k=i; k<=j; k++)
			{
				ll lf = min(ps[k] - ps[i], len - ps[k] + ps[i]);
				dp[i][j] = max(dp[i][j], d[k] + lf + pf[i]);
				
				ll rg = min(ps[j] - ps[k], len - ps[j] + ps[k]);
				dp[i][j] = max(dp[i][j], d[k] + rg + sf[j]);
					

				if(k < j) vc.pb({k, l[k]});
				else vc.pb({j, c});

			}

			int k = 0, ptr = 0;
			ll sum = 0, td = j - i + 1;
			
			//if(i == 1 && j == 2) cout << dp[i][j] << " NOW " << endl;

			while(k<td)
			{
				while(sum + vc[ptr].S <= len/2) sum += vc[ptr].S, ptr ++, ptr %= td;
				//dp[i][j] = max(dp[i][j], max(sum, len - sum - vc[ptr].S));

				//if(i == 1 && j == 2) cout << k << " --> " << ptr << endl;

				//k,a ptr

				if(k <= ptr)
				{
					ll mx;
					if(k + 1 <= ptr) mx = h1[i + k + 1][i + ptr];
					else mx = -oo;
					
					dp[i][j] = max(dp[i][j], mx + d[i + k] - ps[i + k]);

					if(k > 0)
					{
						ll mx = h2[i][i + k - 1];
						dp[i][j] = max(dp[i][j], mx + d[i + k] + ps[i + k]);
					}

					if(ptr < td-1)
					{
						ll mx = h2[i + ptr + 1][j];
						dp[i][j] = max(dp[i][j], mx + d[i + k] + ps[i + k] - ps[i] + c + ps[j]);
					}

				}else
				{
					if(k < td - 1)
					{
						ll mx = h1[i + k + 1][j];
						dp[i][j] = max(dp[i][j], mx - ps[i + k] + d[i + k]);
					}
					if(ptr < k-1)
					{
						ll mx = h2[i + ptr+1][i + k - 1];
						dp[i][j] = max(dp[i][j], mx + d[i+k] + ps[i+k]);
					}
					if(ptr > 0)
					{
						ll mx = h1[i][i + ptr - 1];
						dp[i][j] = max(dp[i][j], d[i+k] + mx - ps[i] + c + ps[j] - ps[i+k]);
					}
				}

		//		if(i == 1 && j == 2) cout << k << " LLL " << dp[i][j] << endl;

				if(ptr == k) ptr++, k++;
				else sum -= vc[k].S, k++;
					
				//cout << k << " : " << ptr << endl;
		
			}

		//	cout << i << ',' << j << " --> " << dp[i][j] << endl;

			ans = min(ans, dp[i][j]);

		}

    return ans;
}

Compilation message

shortcut.cpp: In function 'long long int find_shortcut(int, std::vector<int>, std::vector<int>, int)':
shortcut.cpp:26:25: warning: division by zero [-Wdiv-by-zero]
  if(n >= maxN) cout << 1/0;
                        ~^~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 376 KB n = 4, 80 is a correct answer
2 Correct 2 ms 488 KB n = 9, 110 is a correct answer
3 Correct 2 ms 488 KB n = 4, 21 is a correct answer
4 Correct 3 ms 584 KB n = 3, 4 is a correct answer
5 Incorrect 3 ms 584 KB n = 2, incorrect answer: jury 62 vs contestant 72
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 376 KB n = 4, 80 is a correct answer
2 Correct 2 ms 488 KB n = 9, 110 is a correct answer
3 Correct 2 ms 488 KB n = 4, 21 is a correct answer
4 Correct 3 ms 584 KB n = 3, 4 is a correct answer
5 Incorrect 3 ms 584 KB n = 2, incorrect answer: jury 62 vs contestant 72
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 376 KB n = 4, 80 is a correct answer
2 Correct 2 ms 488 KB n = 9, 110 is a correct answer
3 Correct 2 ms 488 KB n = 4, 21 is a correct answer
4 Correct 3 ms 584 KB n = 3, 4 is a correct answer
5 Incorrect 3 ms 584 KB n = 2, incorrect answer: jury 62 vs contestant 72
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 376 KB n = 4, 80 is a correct answer
2 Correct 2 ms 488 KB n = 9, 110 is a correct answer
3 Correct 2 ms 488 KB n = 4, 21 is a correct answer
4 Correct 3 ms 584 KB n = 3, 4 is a correct answer
5 Incorrect 3 ms 584 KB n = 2, incorrect answer: jury 62 vs contestant 72
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 376 KB n = 4, 80 is a correct answer
2 Correct 2 ms 488 KB n = 9, 110 is a correct answer
3 Correct 2 ms 488 KB n = 4, 21 is a correct answer
4 Correct 3 ms 584 KB n = 3, 4 is a correct answer
5 Incorrect 3 ms 584 KB n = 2, incorrect answer: jury 62 vs contestant 72
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 376 KB n = 4, 80 is a correct answer
2 Correct 2 ms 488 KB n = 9, 110 is a correct answer
3 Correct 2 ms 488 KB n = 4, 21 is a correct answer
4 Correct 3 ms 584 KB n = 3, 4 is a correct answer
5 Incorrect 3 ms 584 KB n = 2, incorrect answer: jury 62 vs contestant 72
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 376 KB n = 4, 80 is a correct answer
2 Correct 2 ms 488 KB n = 9, 110 is a correct answer
3 Correct 2 ms 488 KB n = 4, 21 is a correct answer
4 Correct 3 ms 584 KB n = 3, 4 is a correct answer
5 Incorrect 3 ms 584 KB n = 2, incorrect answer: jury 62 vs contestant 72
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 376 KB n = 4, 80 is a correct answer
2 Correct 2 ms 488 KB n = 9, 110 is a correct answer
3 Correct 2 ms 488 KB n = 4, 21 is a correct answer
4 Correct 3 ms 584 KB n = 3, 4 is a correct answer
5 Incorrect 3 ms 584 KB n = 2, incorrect answer: jury 62 vs contestant 72
6 Halted 0 ms 0 KB -