답안 #173498

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
173498 2020-01-04T09:39:12 Z juggernaut Bigger segments (IZhO19_segments) C++14
13 / 100
3 ms 376 KB
#include <bits/stdc++.h>

#define ll long long
#define int long long int
using namespace std;

const int N = (int)100001;

int n;
int a[N], dp[N], ind[N];
ll pref[N];
 int t[4*N];
 void build(int v,int l,int r){
      if(l==r){
            t[v]=1e9;
            return;
      }
      int mid=(l+r)>>1;
      build(2*v,l,mid);
      build(2*v+1,mid+1,r);
      t[v]=1e9;
}
 void update(int v,int l,int r,int pos,int val){
      if(l==pos&&r==pos){
            t[v]=val;
            return;
      }
      int mid=(l+r)>>1;
      if(pos<=mid)update(2*v,l,mid,pos,val);
      else update(2*v+1,mid+1,r,pos,val);
      t[v]=min(t[v*2],t[v*2+1]);
}
int get(int v,int l,int r,int ql,int qr){
      if(ql<=l&&r<=qr)return t[v];
      if(r<ql||qr<l)return 1e9;
      int mid=(l+r)>>1;
      return min(get(v*2,l,mid,ql,qr),get(v*2+1,mid+1,r,ql,qr));
}
main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		pref[i] = pref[i - 1] + a[i];
	}
	build(1,1,n);
	for (int i = 1; i <= n; i++) {
		dp[i] = dp[i - 1];
		ind[i] = ind[i - 1];
		int l = 1,r=i-1,mid;
		while(l<r){
            mid=(l+r+1)>>1;
            if(pref[i]>=get(1,1,n,mid,i-1))l=mid;
            else r=mid-1;
		}
		if(pref[i]<2*pref[l]-pref[ind[l]])l=0;
		/*for (int j = 0; j < i; j++) {
			if (pref[i] >= 2 * pref[j] - pref[ind[j]]) {
				l = j;
			}
		}*/

		update(1,1,n,i,2*pref[i]-pref[l]);
		dp[i] = dp[l] + 1;
		ind[i] = l;
	}
	// cerr << ind[4] << endl;
	cout << dp[n];
}

Compilation message

segments.cpp:39:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main() {
      ^
segments.cpp: In function 'int main()':
segments.cpp:40:16: warning: format '%d' expects argument of type 'int*', but argument 2 has type 'long long int*' [-Wformat=]
  scanf("%d", &n);
              ~~^
segments.cpp:42:20: warning: format '%d' expects argument of type 'int*', but argument 2 has type 'long long int*' [-Wformat=]
   scanf("%d", &a[i]);
               ~~~~~^
segments.cpp:40:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
segments.cpp:42:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &a[i]);
   ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Incorrect 3 ms 376 KB Output isn't correct
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Incorrect 3 ms 376 KB Output isn't correct
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Incorrect 3 ms 376 KB Output isn't correct
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Incorrect 3 ms 376 KB Output isn't correct
18 Halted 0 ms 0 KB -