B.
枚举a1, b1
并根据a1和b1值分别计算出满足 s>= 6*n的另一条边
记录最小的S
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
#define CLR(x,y) memset(x,y,sizeof(x))
#define mp(x,y) make_pair(x,y)
#define eps 1e-5
#define INF 0x3f3f3f3f
#define LLINF 1LL<<62
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef complex<ld> point;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
template<class T>
inline bool read(T &n)
{
T x = 0, tmp = 1; char c = getchar();
while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
if(c == EOF) return false;
if(c == '-') c = getchar(), tmp = -1;
while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
n = x*tmp;
return true;
}
template <class T>
inline void write(T n)
{
if(n < 0)
{
putchar('-');
n = -n;
}
int len = 0,data[20];
while(n)
{
data[len++] = n%10;
n /= 10;
}
if(!len) data[len++] = 0;
while(len--) putchar(data[len]+48);
}
//-----------------------------------
int main()
{
double s=LLINF,a1,b1;
double n,a,b;
scanf("%lf %lf %lf",&n,&a,&b);
if(a*b>=6LL*n)
{
cout<<(ll)(a*b)<<" "<<(ll)a<<" "<<(ll)b<<endl;
return 0;
}
for(double i=a;i*i<=8*n;i++)
{
double t=max(floor((6*n)/i-eps)+1,b);
if(t*i<s)
{
s=t*i;
a1=i,b1=t;
}
}
for(double i=b;i*i<=8*n;i++)
{
double t=max(floor((6*n)/i-eps)+1,a);
if(t*i<s)
{
s=t*i;
a1=t,b1=i;
}
}
cout<<(ll)s<<"\n"<<(ll)a1<<" "<<(ll)b1<<endl;
return 0;
}
维护前缀和
记录 sum 的三等分点
注意,一定要分成三个区间,每个区间至少有一个数
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
#define CLR(x,y) memset(x,y,sizeof(x))
#define mp(x,y) make_pair(x,y)
#define eps 1e-9
#define INF 0x3f3f3f3f
#define LLINF 1LL<<62
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef complex<ld> point;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
template<class T>
inline bool read(T &n)
{
T x = 0, tmp = 1; char c = getchar();
while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
if(c == EOF) return false;
if(c == '-') c = getchar(), tmp = -1;
while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
n = x*tmp;
return true;
}
template <class T>
inline void write(T n)
{
if(n < 0)
{
putchar('-');
n = -n;
}
int len = 0,data[20];
while(n)
{
data[len++] = n%10;
n /= 10;
}
if(!len) data[len++] = 0;
while(len--) putchar(data[len]+48);
}
//-----------------------------------
const int MAXN=500010;
ll sum[MAXN],cnt[MAXN];
int n,temp;
int main()
{
// freopen("data.txt","r",stdin);
read(n);
for(int i=1;i<=n;i++)
{
read(sum[i]);
sum[i]+=sum[i-1];
}
cout<<endl;
ll ans=0;
if(sum[n]%3!=0)
{
cout<<"0"<<endl;
return 0;
}
cnt[n+1]=0;
for(int i=n-1;i>=1;i--)
{
cnt[i]=cnt[i+1];
if(3*sum[i]==sum[n])
ans+=cnt[i];
if(3*sum[i]==2*sum[n])
cnt[i]++;
}
write(ans),putchar('\n');
return 0;
}
D.
Lets use dynamic programming to solve this problem. dp[i][opened] — the number of ways to cover prefix of array 1..i by segments and make it equal to h and remain after i-th element opened segments that are not closed.
Consider all possible variants opening/closing segments in each position:
- ]
closing one segment
- [
opening one new segment
- []
adding one segment with length 1
- ][
closing one opened segment and opening a new one
- -
do nothing
Lets understand how to build dynamic. It is obviously to understand that a[i] + opened can be equal h or h - 1. Otherwise, number of such ways equals 0.
Consider this two cases separately:
1) a[i] + opened = h
It means that number of opened segments after i-th as max as possible and we can’t open one more segment in this place. So there are two variants:
- [
Opening a new segment. If only opened > 0. dp[i][opened] += dp[i-1][opened + 1]
- -
Do nothing. dp[i][opened] += dp[i-1][opened]
Other variants are impossible because of summary value of a[i] will be greater than h(when segment is finishing in current position — it increase value, but do not influence on opened, by the dynamic definition.
2) a[i] + opened + 1 = h
Here we consider ways where i-th element has been increased by opened + 1 segments, but after i-th remain only opened not closed segments. Therefore, there are next variants:
- ]
— closing one of the opened segments(we can do it opened + 1 ways).dp[i][opened] += dp[i-1][opened + 1] * (opened + 1)
- []
— creating 1-length segment. dp[i][opened] += dp[i-1][opened]
- ][
— If only opened > 0. Amount of ways to choose segment which we will close equals opened.dp[i][opened] += dp[i-1][opened] * opened
Start values — dp[1][0] = (a[1] == h || a[1] + 1 == h?1:0); dp[1][1] = (a[1] + 1 == h?1:0)
Answer — dp[n][0]
.
Complexity: O(n)
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
#define CLR(x,y) memset(x,y,sizeof(x))
#define mp(x,y) make_pair(x,y)
#define eps 1e-9
#define INF 0x3f3f3f3f
#define LLINF 1LL<<62
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef complex<ld> point;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
template<class T>
inline bool read(T &n)
{
T x = 0, tmp = 1;
char c = getchar();
while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
if(c == EOF) return false;
if(c == '-') c = getchar(), tmp = -1;
while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
n = x*tmp;
return true;
}
template <class T>
inline void write(T n)
{
if(n < 0)
{
putchar('-');
n = -n;
}
int len = 0,data[20];
while(n)
{
data[len++] = n%10;
n /= 10;
}
if(!len) data[len++] = 0;
while(len--) putchar(data[len]+48);
}
//-----------------------------------
const int MAXN=2222;
const int MOD=1000000007;
int n, k, a[MAXN], dp[MAXN][MAXN];
int main()
{
// freopen("data.txt", "r", stdin);
read(n),read(k);
for (int i = 0; i < n; i++)
{
read(a[i]);
if (a[i] > k)
{
puts("0");
return 0;
}
}
dp[0][0] = 1;
for (int i = 0; i < n; i++)
for (int j = 0; j <= i; j++)
{
if (dp[i][j] != 0)
{
if (a[i] + j == k)
{
dp[i + 1][j] = (dp[i + 1][j]+dp[i][j]) % MOD;
if (j > 0)
dp[i + 1][j - 1] = (dp[i + 1][j - 1]+dp[i][j] * 1LL * j) % MOD;
}
else if(a[i] + j + 1 == k)
{
dp[i + 1][j + 1] = (dp[i + 1][j + 1] + dp[i][j])%MOD;
dp[i + 1][j] = (dp[i + 1][j] + dp[i][j] * 1LL * (j + 1)) % MOD;
}
}
}
write(dp[n][0]);
putchar('\n');
return 0;
}