day 3: 这次模拟赛oi赛制,最难的题目是第3题,需要思考一下,其他题也还好,但是 第二题的模拟打太久,打复杂了,还是要优化一下思路。
A:没什么好说的,模拟翻书,记录左边的位置,如果左边位置和当前位置都没学过,就不查目录,步数+1,查目录步数+2。
赛时交了个70hhh,一个判断条件放错位置了,改了就过了。
B:纯模拟,可以枚举第一位处理,位数不同就不用判断,先比较有多少位不同,再分两种情况判断,l和r的全枚举一边就可以,但我的模拟写拉了。
赛时40,后面优化枚举就行。
C:给出若干区间,每个区间选一个数,要按位与最大。可以每一位从大到小枚举,合理就加到ans里,注意枚举时在区间内找与ans按位与后仍为ans的最小值,找枚举的数与左端点的按位与不一定是最小的,会导致这个值大于右端点时答案错误,优化枚举,此题就可以通过。看其他人的代码也有修改左右端点的,之后再学习下。
赛时20,考试暴力骗的分,后面优化后过了。
D:给出g个绿色方块,r个红色方块,在每层颜色相同的情况下摆个1,2,3数量递增的塔,问方案数。这题用二维dp可以过,但是要优化状态,题目中给出了层数h,且方案一定要有这么多层。我们可以利用h(h+1)/2 < r+g的思路将数字值域较大的那一位替换为h,便可以O(n根号n)的复杂度。后面发现空间不够,便滚动数组优化压维,最后就过了。到这里可以发现这题和01背包问题很像,容量为h(h+1)/2,一个颜色是选,一个颜色是不选(最好不选多的数,因为数组更大,合法答案统计的次数少)。
赛时暴力,后面优化后AC了。
模拟赛确实展现了很多问题,比如模拟题写太复杂之类的,后面要加强小题速刷的练习
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 1e5 + 20;
void work() {
int n;
cin >> n;
vector<ll> a(n, 0);
for(int i = 0; i < n; i++) {
cin >> a[i];
}
ll las = 0, ans = 0;
for(int i = 0; i < n; i++) {
if(a[i] == 1) {
if(i - las <= 1 || ans == 0) {
ans++;
}
else ans += 2;
las = i;
}
}
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int T = 1;
// cin >> T;
while(T--) {
work();
}
}
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 1e5 + 20;
int num[20];
void work() {
string l, r;
int w, ans = 0;
cin >> l >> r >> w;
for(int i = 1; i <= 18; i++) {
if(i <= 9) num[i] = i;
else num[i] = num[19 - i];
}
int dl = l.size(), dr = r.size();
vector<int> a(r.size()), b(l.size());
for(int i = 0; i < r.size(); i++) {
a[i] = r[i] - '0';
}
for(int i = 0; i < l.size(); i++) {
b[i] = l[i] - '0';
}
int dig = dr - dl;
if(dr - dl == 0) {//数位相同
int sdig = 0;
for(int i = 0; i < dl; i++) {
if(a[i] == b[i]) sdig++;
else break;
}
if(sdig == 0) {//第一位不同
if(a[0] <= w) {
int cnt = 1, now = a[0];
for(int i = 0; i < dr; i++) {//处理a
if(a[i] == now && cnt == 1) cnt = 1;
else if(a[i] > now && cnt == 1) cnt = 2;
else if(cnt != 2) {
cnt = 0;
}
now = w - now;//sum = w
}
if(cnt >= 1) ans++;
}
if(b[0] <= w) {
int cnt = 1, now = b[0];
for(int i = 0; i < dl; i++) {//处理b
if(b[i] == now && cnt == 1) cnt = 1;
else if(b[i] < now && cnt == 1) cnt = 2;
else if(cnt != 2) {
cnt = 0;
}
now = w - now;//sum = w
}
if(cnt >= 1) ans++;
}
if(min(w,a[0]-1) - b[0] > 0) ans += min(w,a[0]-1) - b[0];
}else {//数位相等但和不为w
int now = a[0], cnt = 1;
for(int i = 0; i < dl; i++) {
if(now == b[i] && now == a[i]) cnt = 1;
if(now > b[i] && cnt == 1) cnt = 2;
else if(now < a[i] && cnt == 1) cnt = 3;
else if(now < b[i] && cnt > 1) cnt = 4;
if(cnt != 4 && cnt != 2 && now < b[i]) cnt = -1;
if(cnt != 4 && cnt != 3 && now > a[i]) cnt = -1;
now = w - now;
}
if(cnt >= 1) ans++;
}
} else {//数位不同
if(a[0] <= w) {
int cnt = 1, now = a[0];
for(int i = 0; i < dr; i++) {//处理1000..-a
if(a[i] == now && cnt == 1) cnt = 1;
else if(a[i] > now && cnt == 1) cnt = 2;
else if(cnt != 2) cnt = 0;
now = w - now;//sum = w
}
if(cnt >= 1) {
ans++;
}
}
if(b[0] <= w) {
int cnt = 1, now = b[0];
for(int i = 0; i < dl; i++) {//处理b-999...
if(b[i] == now && cnt == 1) cnt = 1;
if(b[i] < now && cnt == 1) cnt = 2;
else if(cnt != 2) cnt = 0;
now = w - now;//sum = w
}
if(cnt >= 1) ans++;
}
ans += (dig-1) * num[w];
}
if(w == 0 || (dl == 1 && dr == 1)) cout << "0" << '\n';
else cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int T = 1;
cin >> T;
while(T--) {
work();
}
}
#include<bits/stdc++.h>
using namespace std;
#define ll long long
void work() {
int n;
cin >> n;
vector<ll> l(n), r(n);
for(int i = 0; i < n; i++) {
cin >> l[i] >> r[i];
}
ll ans = 0;
for(int i = 62; i >= 0; i--) {
ll now = ans | (1LL << i);
bool valid = true;
for(int j = 0; j < n; j++) {
ll mask = ~((1LL << i) - 1);
ll h = l[j] & mask;
ll digit = now | h;
if (digit > r[j]) {
valid = false;
break;
}
}
if (valid) {
ans = now;
for (int j = 0; j < n; j++) {
ll mask = ~((1LL << i) - 1);
ll h = l[j] & mask;
l[j] = max(l[j], ans | h);
}
}
}
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int T = 1;
while(T--) {
work();
}
}
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 2e5 + 20;
const int mod = 1e9 + 7;
vector<int> dp(N, 0);
//dp[i][j]: i层时使用j个绿色方块的方案数
void work() {
int r, g;
cin >> r >> g;
if(g > r) swap(g, r);//keep r > g
int h = sqrt((r + g) * 2);
if(h*(h + 1)/2 > r+g) h--;
dp[0] = 1;//1层dp[1]=1, 初始化
for(int i = 1; i <= h; i++) {
for(int j = g; j >= i; j--) {
dp[j] = (dp[j] + dp[j-i]) % mod;
}
}
ll ans = 0;
for(int i = g; i + r >= h*(h+1)/2; i--) {
ans = (ans + dp[i]) % mod;
}
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int T = 1;
// cin >> T;
while(T--) {
work();
}
}