这里用dfs写,带剪枝
#include <bits/stdc++.h>
using namespace std;
const int N = 1e2 + 1;
int a[N];
set<int> s;
bool check(int date)
{
int mm = date / 100 % 100;
int dd = date % 100;
if (mm < 1 || mm > 12)
return false;
if (mm == 1 || mm == 3 || mm == 5 || mm == 7 || mm == 8 || mm == 10 || mm == 12)
{
if (dd >= 1 && dd <= 31)
return true;
}
else if (mm == 2)
{
if (dd >= 1 && dd <= 28)
return true;
}
else if (dd >= 1 && dd <= 30)
return true;
return false;
}
void dfs(int x, int cnt, int date)
{
if (x == 100)
return;
if (cnt == 8)
{
if (check(date))
s.insert(date);
return;
}
if (cnt == 0)
date = 0;
if ((cnt == 0 && a[x] == 2) || (cnt == 1 && a[x] == 0) || (cnt == 2 && a[x] == 2) || (cnt == 3 && a[x] == 3) || (cnt == 4 && a[x] >= 0 && a[x] <= 1) || (cnt == 5 && a[x] >= 0 && a[x] <= 9) || (cnt == 6 && a[x] >= 0 && a[x] <= 3) || (cnt == 7 & a[x] >= 0 && a[x] <= 9))
dfs(x + 1, cnt + 1, date * 10 + a[x]); // 合法可以选
dfs(x + 1, cnt, date); // 不选
}
int main()
{
for (int i = 0; i < 100; i++)
cin >> a[i];
dfs(0, 0, 0);
cout << s.size() << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 23333333,M = N/2;
long double eps = 11625907.5798;
int cal(int i)
{
long double p;
long double n0 = i;
long double n1 = N-i;
p = -((n0*n0/N)*log2l(n0/N)+(n1*n1/N)*log2l(n1/N));
//cout<<p<<endl;
if(abs(p-eps)<0.0001)
cout<<i<<endl;
return p;
}
int main()
{
//cout<<cal(114514)<<endl;
for(int i = 1;i<=N/2;i++)
cal(i);
return 0;
}
对答案使用二分,复杂度o(nlog(n))
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1;
int a[N], b[N], n;
bool checkmin(int mid)
{
for (int i = 0; i < n; i++)
if (a[i] / mid > b[i])
return false;
return true;
}
bool checkmax(int mid)
{
for (int i = 0; i < n; i++)
if (a[i] / mid < b[i])
return false;
return true;
}
int main()
{
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i] >> b[i];
int l = 0, r = 1e9;
while (l <= r)
{
int mid = (l + r) >> 1;
if (checkmin(mid))
r = mid - 1;
else
l = mid + 1;
}
cout << l << " ";
l = 0, r = 1e9;
while (l <= r)
{
int mid = (l + r) >> 1;
if (checkmax(mid))
l = mid + 1;
else
r = mid - 1;
}
cout << r << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 1001;
int T, n;
int t[N], d[N], l[N];
bool st[N];
bool flag = false;
void dfs(int k, int time)
{
if (k == n - 1)
{
flag = true;
return;
}
for (int i = 0; i < n; i++)
{
if (!st[i])
{
if (t[i] + d[i] >= time)
{
st[i] = true;
dfs(k + 1, max(time + l[i], t[i] + l[i]));
st[i] = false; // 找完了放回去}
}
}
}
}
void solve()
{
flag = false;
memset(st, 0, sizeof(st));
cin >> n;
for (int i = 0; i < n; i++)
cin >> t[i] >> d[i] >> l[i];
for (int i = 0; i < n; i++)
{
if (!st[i])
{
st[i] = true;
dfs(0, t[i] + l[i]);
st[i] = false;
}
}
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
int main()
{
cin >> T;
while (T--)
solve();
return 0;
}
/*
2
3
0 100 10
10 10 10
0 2 20
3
0 10 20
10 10 20
20 10 20
*/
dp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1;
int f[N];
int a[N], last[N], st[N];
int n;
int get_the_first_number(int n)
{
while (n)
{
int t = n % 10;
n /= 10;
if (n == 0)
return t;
}
return 0;
}
int get_the_last_number(int n)
{
return n % 10;
}
bool check(int a, int b)
{
int a_last = get_the_last_number(a);
int b_first = get_the_first_number(b);
if(a_last==b_first)
return true;
return false;
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++)
{
f[i] = f[i - 1];
st[i] = st[i - 1];
if (i == 1 || check(st[i], a[i]))
{
f[i]++;
st[i] = a[i];
}
}
cout<<n-f[n]<<endl;
return 0;
}