7017 杭电多校(2021“MINIEYE杯”中国大学生算法设计超级联赛5) [记忆化搜索]

Posted 你可以当我没名字

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了7017 杭电多校(2021“MINIEYE杯”中国大学生算法设计超级联赛5) [记忆化搜索]相关的知识,希望对你有一定的参考价值。

https://acm.hdu.edu.cn/showproblem.php?pid=7017

Cute Tree

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 53 Accepted Submission(s): 40
*

Problem Description

Given the pseudo-code of a function Build−Tree(A,id,L,R):

where A is given in input, id is the number of node, L ,R is the left position and the right position of A

Require the number of nodes created by Build−Tree(A,root,1,n).

Input

The first line contains an integer T (1≤T≤5) representing the number of test cases.

For each test case, the first contain one integer n(1≤n≤2∗105).

The second line contain n integers Ai(1≤Ai≤109).

Output

For each test output one line, the number of nodes created by Build−Tree(A,root,1,n).

Sample Input

样例1:

1
7
4 3 5 2 6 7 1

样例2:

2
4
2 2 5 3
10
21 10 5 89 12 3 42 13 55 76

Sample Output

样例1:

11

样例2:

6
15

Source

2021“MINIEYE杯”中国大学生算法设计超级联赛(5)

Recommend

IceyWang | We have carefully selected several similar problems for you: 7024 7023 7022 7021 7020

代码与解释

和上次的签到题一样,都是记忆化搜索

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <iostream>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <bitset>
#include <vector>
#include <limits.h>
#include <assert.h>
#include <functional>
#include <numeric>
#include <ctime>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define pb          push_back
#define ppb         pop_back
#define lbnd        lower_bound
#define ubnd        upper_bound
#define endl        '\\n'
#define mll         map<ll,ll>
#define msl         map<string,ll>
#define mls         map<ll, string>
#define rep(i,a,b)  for(ll i=a;i<b;i++)
#define repr(i,a,b) for(ll i=b-1;i>=a;i--)
#define trav(a, x)  for(auto& a : x)
#define pll         pair<ll,ll>
#define vl          vector<ll>
#define vll         vector<pair<ll, ll>>
#define vs          vector<string>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll)x.size()
#define hell        1000000007
#define DEBUG       cerr<<"/n>>>I'm Here<<</n"<<endl;
#define display(x)  trav(a,x) cout<<a<<" ";cout<<endl;
#define what_is(x)  cerr << #x << " is " << x << endl;
#define ini(a)      memset(a,0,sizeof(a))
#define ini2(a,b)   memset(a,b,sizeof(a))
#define rep(i,a,b)  for(int i=a;i<=b;i++)
#define sc          ll T;cin>>T;for(ll Q=1;Q<=T;Q++)
#define lowbit(x)   x&(-x)
#define pr          printf
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define DBG(x) \\
    (void)(cout << "L" << __LINE__ \\
    << ": " << #x << " = " << (x) << '\\n')
#define TIE \\
    cin.tie(0);cout.tie(0);\\
    ios::sync_with_stdio(false);
//#define long long int

//using namespace __gnu_pbds;

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int maxn = 1009;
const ll N = 1000000007;
const ull N2 = 1000000007;

typedef struct LNode{
    int coef,index;
    struct LNode *next;
}LNode,*LinkList;

bool cmp(int a,int b){
	return a>b;
}

map<ll,ll>ma;

ll BuildTree(ll l,ll r) {
	ll sum = 1;
	if(ma.count(r-l))return ma[r-l];
    if (l == r){
    	return 1;
	}
	if(r-l==1){
		sum+=BuildTree(l,l);
		sum+=BuildTree(r,r);
	}else{
		ll b = r-l;
		if(b%3!=0){
			b = l+b/3;
		}else{
			b = l+b/3-1;
		}
		ll c = b+r;
		if(c%2!=0){
			c = c/2+1;
		}else{
			c = c/2;
		}
		sum+=BuildTree(l,b);
		sum+=BuildTree(b+1,c);
		sum+=BuildTree(c+1,r);
	}
	return ma[r-l] = sum;
}


void solve(){
	ma.clear();
	ll q,p;
	cin>>q;
	for(int i=0;i<q;i++){
		cin>>p;
	}
	cout<<BuildTree(1,q)<<endl;
}

int main()
{
	TIE;
//    #ifndef ONLINE_JUDGE
//    freopen ("input.txt","r",stdin);
//    #else
//    #endif
//	solve();
    sc{solve();}
//    sc{cout<<"Case "<<Q<<":"<<endl;solve();}
}





以上是关于7017 杭电多校(2021“MINIEYE杯”中国大学生算法设计超级联赛5) [记忆化搜索]的主要内容,如果未能解决你的问题,请参考以下文章

2021杭电多校赛2021“MINIEYE杯”中国大学生算法设计超级联赛签到题3题

6983 杭电多校(2021“MINIEYE杯”中国大学生算法设计超级联赛3) [记忆化搜索]

2021杭电多校赛2021“MINIEYE杯”中国大学生算法设计超级联赛签到题4题

2021杭电多校赛2021“MINIEYE杯”中国大学生算法设计超级联赛签到题5题

2021杭电多校赛2021“MINIEYE杯”中国大学生算法设计超级联赛签到题2题

2021杭电多校赛2021“MINIEYE杯”中国大学生算法设计超级联赛签到题5题