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

131 篇文章 0 订阅

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):

img

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)

代码与解释

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

#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();}
}





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值