98102024-03-07 03:59:47SleepyOverlordSzitakötő (50 pont)cpp17Time limit exceeded 13/50601ms23748 KiB
#include <vector>
#include <string> 
#include <set> 
#include <map> 
#include <unordered_set>
#include <unordered_map>
#include <queue> 
#include <bitset> 
#include <stack>
#include <list>

#include <numeric> 
#include <algorithm> 
#include <random>
#include <chrono>

#include <cstdio>
#include <fstream>
#include <iostream> 
#include <sstream> 
#include <iomanip>
#include <climits>

#include <cctype>
#include <cmath> 
#include <ctime>
#include <cassert>

using namespace std;

#define ULL unsigned long long
#define LL long long
#define PII pair <int, int>
#define VB vector <bool>
#define VI vector <int>
#define VLL vector <LL>
#define VD vector <double>
#define VS vector <string>
#define VPII vector <pair <int, int> >
#define VVI vector < VI >
#define VVB vector < VB >
#define SI set < int >
#define USI unordered_set <int>
#define MII map <int, int>
#define UMII unordered_map <int, int>
#define MS multiset
#define US unordered_set
#define UM unordered_map
#define UMS unordered_multiset
#define UMM unordered_multimap

#define FORN(i, n) for(int i = 0; i < (n); ++i)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define MX(x, y) x = max(x, y);
#define MN(x, y) x = min(x, y);

#define SZ size()
#define BG begin() 
#define EN end() 
#define CL clear()
#define X first
#define Y second
#define RS resize
#define PB push_back
#define MP make_pair
#define ALL(x) x.begin(), x.end()
#define INS insert
#define ER erase
#define CNT count

template <class T> ostream& operator <<(ostream & os, const vector<T> &vec)
{
	for (int i = 0; i < vec.size() - 1; ++i) os << vec[i] << ' ';
	return os << vec[vec.size() - 1];
}

template <class T1, class T2> ostream& operator <<(ostream & os, const pair<T1, T2> &p)
{
	return os << p.X << " " << p.Y;
}

template <typename T>
void pr(T var1)
{
	cout << var1 << '\n';
}
template <typename T, typename... Types>
void pr(T var1, Types... var2)
{
	cout << var1;
	pr(var2...);
}

void in(int &n, VI &a) //array of ints
{
	cin >> n;
	a.CL, a.RS(n + 1);
	FOR(i, 1, n) cin >> a[i];
}

void in(int &n, VS &a) //array of strings
{
	cin >> n;
	a.CL, a.RS(n + 1);
	FOR(i, 1, n) cin >> a[i];
}

void in(int &n, VPII &a) //array of pairs
{
	cin >> n;
	a.CL, a.RS(n + 1);
	FOR(i, 1, n) cin >> a[i].X >> a[i].Y;
}

void in(int &n, int &m, VVI &g) //unweighted graph
{
	cin >> n >> m;
	g.CL, g.RS(n + 1);
	FOR(i, 1, n)
	{
		int x, y;
		cin >> x >> y;
		g[x].PB(y);
		g[y].PB(x);
	}
}

void in(int &n, VVI &g) //unweighted tree
{
	cin >> n;
	g.CL, g.RS(n + 1);
	FOR(i, 1, n - 1)
	{
		int x, y;
		cin >> x >> y;
		g[x].PB(y);
		g[y].PB(x);
	}
}

void in(int &n, int &m, vector <VPII> &g) //weighted graph
{
	cin >> n >> m;
	g.CL, g.RS(n + 1);
	FOR(i, 1, n)
	{
		int x, y, z;
		cin >> x >> y >> z;
		g[x].PB({y, z});
		g[y].PB({x, z});
	}
}

void in(int &n, vector <VPII> &g) //weighted tree
{
	cin >> n;
	g.CL, g.RS(n + 1);
	FOR(i, 1, n - 1)
	{
		int x, y, z;
		cin >> x >> y >> z;
		g[x].PB({y, z});
		g[y].PB({x, z});
	}
}

const int MOD = 1e9 + 7;

int n, k, sol = 0;
VI a, ss;

void back(int sp)
{
	if (sp == n + 1)
	{
		vector < vector <tuple <int, int, int> > > b(2, vector <tuple <int, int, int> >(n + 1));
		FOR(i, 1, n) b[0][i] = {a[i], ss[i], i};
		int row = 0, cnt = n;
		while (cnt > 1)
		{
			FOR(i, 1, n) b[row ^ 1][i] = {0, 0, 0};
			auto [val, dir, ind] = b[row][1];
			if (dir == -1) b[row ^ 1][1] = {val, 1, ind};
			auto [val2, dir2, ind2] = b[row][n];
			if (dir2 == 1) b[row ^ 1][n] = {val2, -1, ind2};
			VB checked(n + 1);
			FOR(i, 1, n)
			{
				if (checked[i]) continue;
				auto [val1, dir1, ind1] = b[row][i];
				if (i == n) 
				{
					if (dir1 == -1)
					{
						auto [v1, d1, i1] = b[row ^ 1][i + dir1];
						if (v1 == 0) b[row ^ 1][i + dir1] = {val1, dir1, ind1};
						else
						{
							--cnt;
							if (v1 > val1) b[row ^ 1][i + dir1] = {v1 + val1, d1, i1};
							else b[row ^ 1][i + dir1] = {v1 + val1, dir1, ind1};
						}
					}
					break;
				}
				auto [val2, dir2, ind2] = b[row][i + 1];
				if (dir1 == 1 and dir2 == -1)
				{
					--cnt;
					checked[i] = checked[i + 1] = true;
					if (val1 > val2) b[row ^ 1][i + 1] = {val1 + val2, 1, ind1};
					else 
					{
						auto [v1, d1, i1] = b[row ^ 1][i];
						if (v1 == 0) b[row ^ 1][i] = {val1 + val2, -1, ind2};
						else
						{
							--cnt;
							if (v1 > val1 + val2) b[row ^ 1][i] = {v1 + val1 + val2, d1, i1};
							else b[row ^ 1][i] = {v1 + val1 + val2, -1, ind2};
						}
					}
				}
				else 
				{
					if (dir1 == 0 or i + dir1 < 1 or i + dir1 > n) continue;
					auto [v1, d1, i1] = b[row ^ 1][i + dir1];
					if (v1 == 0) b[row ^ 1][i + dir1] = {val1, dir1, ind1};
					else
					{
						--cnt;
						if (v1 > val1) b[row ^ 1][i + dir1] = {v1 + val1, d1, i1};
						else b[row ^ 1][i + dir1] = {v1 + val1, dir1, ind1};
					}
				}
			}
			row ^= 1;
		}
		for (auto [val, dir, ind] : b[row])
			if (ind == k) 
			{
				++sol;
				break;
			}
		return;
	}
	ss[sp] = -1;
	back(sp + 1);
	ss[sp] = 1;
	back(sp + 1);
}

int main()
{
	#ifdef AT_HOME
	freopen("a.in", "r", stdin);
	freopen("a.out", "w", stdout);
	#endif

	cin >> n >> k;
	a.RS(n + 1);
	FOR(i, 1, n) cin >> a[i];

	ss.RS(n + 1);
	back(1);
	pr(sol);

	return 0;
}
SubtaskSumTestVerdictTimeMemory
base13/50
1Accepted0/03ms1880 KiB
2Time limit exceeded0/0601ms21240 KiB
3Accepted1/13ms2296 KiB
4Accepted1/13ms2508 KiB
5Accepted1/13ms2724 KiB
6Accepted1/13ms2940 KiB
7Accepted1/12ms3028 KiB
8Accepted1/175ms3000 KiB
9Accepted1/178ms3000 KiB
10Accepted2/275ms3004 KiB
11Accepted2/275ms3124 KiB
12Accepted2/275ms3380 KiB
13Time limit exceeded0/2574ms3720 KiB
14Time limit exceeded0/2561ms4016 KiB
15Time limit exceeded0/2550ms2968 KiB
16Time limit exceeded0/2550ms3336 KiB
17Time limit exceeded0/2554ms4532 KiB
18Time limit exceeded0/2561ms3648 KiB
19Time limit exceeded0/2564ms4636 KiB
20Time limit exceeded0/2569ms4720 KiB
21Time limit exceeded0/1578ms4696 KiB
22Time limit exceeded0/2540ms23440 KiB
23Time limit exceeded0/2555ms23492 KiB
24Time limit exceeded0/2559ms23584 KiB
25Time limit exceeded0/2542ms23472 KiB
26Time limit exceeded0/2564ms23444 KiB
27Time limit exceeded0/2538ms23684 KiB
28Time limit exceeded0/2564ms23660 KiB
29Time limit exceeded0/2523ms23616 KiB
30Time limit exceeded0/2570ms23696 KiB
31Time limit exceeded0/2572ms23748 KiB