답안 #1025904

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1025904 2024-07-17T11:06:53 Z vjudge1 San (COCI17_san) C++17
120 / 120
187 ms 20884 KB
#include <bits/stdc++.h>

using namespace std;

#define int long long

signed main()
{
	int n,k;
	cin>>n>>k;
	int h[n],a[n];
	for (int i=0;i<n;i++)
		cin>>h[i]>>a[i];
	int ans=0;
	if (n<=20)
	{
		for (int i=0;i<(1<<n);i++)
		{
			bool b=1;
			int x=0,pre=0;
			for (int p=0;p<n;p++)
				if ((1<<p)&i)
				{
					if (h[p]<pre)
						b=0;
					pre=h[p];
					x+=a[p];
				}
			if (x>=k && b)
				ans++;
		}
	}
	else
	{
		int op=20;
		vector<pair<int,int>> gl,hf;
		for (int i=1;i<(1<<op);i++)
		{
			bool b=1;
			int x=0,pre=0;
			for (int p=0;p<op;p++)
				if ((1<<p)&i)
				{
					if (h[p]<pre)
						b=0;
					pre=h[p];
					x+=a[p];
				}
			if (b)
				gl.push_back({x,pre}),ans+=(x>=k);
		}
		sort(gl.rbegin(),gl.rend());
		for (int i=1;i<(1<<n-op);i++)
		{
			bool b=1;
			int x=0,pre=0,fir=0;
			for (int p=0;p<n-op;p++)
				if ((1<<p)&i)
				{
					if (h[p+op]<pre)
						b=0;
					x+=a[p+op];
					pre=h[p+op];
					if (!fir)
						fir=h[p+op];
				}
			if (b)
				hf.push_back({x,fir}),ans+=(x>=k);
		}
		sort(hf.begin(),hf.end());
		int id=0;
		map<int,int> cnt;
		for (auto i:hf)
		{
			while (id<gl.size() && gl[id].first>=k-i.first)
				cnt[gl[id++].second]++;
			for (auto j:cnt)
				if (j.first<=i.second)
					ans+=j.second;
				else
					break;
		}
	}
	cout<<ans<<endl;
	
	return 0;
}

Compilation message

san.cpp: In function 'int main()':
san.cpp:53:23: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   53 |   for (int i=1;i<(1<<n-op);i++)
      |                      ~^~~
san.cpp:75:13: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |    while (id<gl.size() && gl[id].first>=k-i.first)
      |           ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 348 KB Output is correct
2 Correct 62 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 177 ms 16752 KB Output is correct
2 Correct 83 ms 996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 86 ms 4540 KB Output is correct
2 Correct 76 ms 980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 187 ms 20884 KB Output is correct
2 Correct 142 ms 3472 KB Output is correct