답안 #118757

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
118757 2019-06-19T16:26:56 Z silxikys Calvinball championship (CEOI15_teams) C++14
100 / 100
191 ms 1500 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
 
const int maxn = 1e4+4, M = 1e6+7;
int n, a[maxn];
int dp[2][maxn];
 
void madd(int& a, int b) {
	a = (a+b) % M;
}
int mult(int a, int b) {
	return (1LL*a*b) % M;
}
 
struct Node {
	int s, e, m;
	//covers s,e;
	int sum;
	int maxi;
	Node *l, *r;
	
	Node(int a, int b) {
		s = a;
		e = b;
		sum = 0;
		maxi = 0;
		if (s != e) {
			m = (s+e)/2;
			l = new Node(s,m);
			r = new Node(m+1,e);
		}
		else {
			l = NULL;
			r = NULL;
		}
	}
 
 
	void add(int i, int x) {
		if (s == e) {
			sum += x;
			maxi = sum;
			return;
		}
		if (i <= m) {
			l->add(i,x);
		}
		else if (i > m) {
			r->add(i,x);
		}
		else assert(false);
		sum = l->sum + r->sum;
		maxi = max(l->maxi,r->maxi);
	}
 
	int getmaxi(int st, int en) {
		if (st <= s && e <= en) {
			return maxi;
		}
		int ret = 0;
		if (st <= m) {
			ret = max(ret,l->getmaxi(st,en));
		}
		if (en > m) {
			ret = max(ret,r->getmaxi(st,en));
		}
		return ret;
	}	
 
	int getsum(int st, int en) {
		if (st <= s && e <= en) {
			return sum;
		}
		int ret = 0;
		if (st <= m) {
			ret += l->getsum(st,en);
		}
		if (en > m) {
			ret += r->getsum(st,en);
		}
		return ret;
	}
};
 
int br = 0;
void DFS(vector<int> v) {
    if (v.size() == n) {
        bool poss = true;
        int biggestSeen = 0;
        for (int i: v) {
            if (i > biggestSeen+1) {
                poss = false;
                break;
            }
            biggestSeen = max(biggestSeen,i);
        }
        if (poss) {
            br++;
            /*
            for (int i: v) {
                cout << i << ' ';
            }
            cout << '\n';
            */
        }
    }
    else {
        for (int j = 1; j <= n; j++) {
            v.push_back(j);
            DFS(v);
            v.pop_back();
        }
    }
}
 
int brute() {
    vector<int> v;
    DFS(v);
    return br;
}
 
int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0);
    cin >> n;
    Node *root = new Node(0,n);
    for (int i = 1; i <= n+1; i++) {
        cin >> a[i];
        root->add(i,a[i]);
    }
    for (int i = 1; i <= n+1; i++) {
    	dp[0][i] = 1;
    }
 
    int ans = 0;
    for (int i = n; i >= 1; i--) {
    	for (int j = 1; j <= i; j++) {
    		dp[1][j] = (mult(j,dp[0][j]) + dp[0][j+1]) % M;
    		//cout << dp[0][j] << ' ';
    	}
    	//cout << '\n';
    	int m = root->getmaxi(0,i-1);
    	madd(ans,mult(a[i]-1,dp[0][m]));
        //cout << i << ": " << mult(a[i]-1,dp[0][m]) << '\n';
 
    	for (int j = 1; j <= i; j++) {
    		dp[0][j] = dp[1][j];
    	}
    }
 
    cout << ((ans+1)%M) << '\n';
    //cout << "brute : " << brute() << '\n';
}

Compilation message

teams.cpp: In function 'void DFS(std::vector<int>)':
teams.cpp:88:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if (v.size() == n) {
         ~~~~~~~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 316 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 4 ms 556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 189 ms 1500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 896 KB Output is correct
2 Correct 50 ms 896 KB Output is correct
3 Correct 50 ms 916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 191 ms 1408 KB Output is correct
2 Correct 191 ms 1408 KB Output is correct
3 Correct 191 ms 1408 KB Output is correct