2023河南萌新联赛第(六)场
A. 简单的异或(前缀和)

输入
5
1 2 3 4 5
3
1 2
2 4
3 5
输出
2147483644
2147483645
2147483642
说明
第一个样例中,第一次访问区间[1,2]区间内的值为1,2当x取2147483644即1111111111111111111111111111100(31位二进制)时
x1+x2的值最大
点击查看代码
#include<bits/stdc++.h>
#define int long long
//#define mod 998244353
#define fi first
#define se second
using namespace std;
const int N = 2e5 + 10, Inf = 2147483647;
int st[N], prime[N], cnt;
typedef long long i64;
int a[N], b[N][35], s[N];
int n, m, ans, x;
int ff[N];
void init(){
ff[0] = 1;
for(int i = 1; i <= 40; i ++)
ff[i] = ff[i - 1] * 2;
}
void solve()
{
cin >> n;
string s;
for(int i = 1; i <= n; i ++) cin >> a[i];
for(int i = 1; i <= n; i ++)
{
for(int j = 0; j < 31; j ++)
{
b[i][j] = a[i] % 2;
a[i] /= 2;
}
}
// for(int i = 1; i <= n; i ++)
// {
// for(int j = 0; j < 31; j ++)
// cout << b[i][j] << ' ';
// cout << '\n';
// }
for(int j = 0; j < 31; j ++)
for(int i = 1; i <= n; i ++)
b[i][j] += b[i - 1][j];
// for(int i = 1; i <= n; i ++)
// {
// for(int j = 0; j < 31; j ++)
// cout << b[i][j] << ' ';
// cout << '\n';
// }
cin >> m;
int l, r, t, tt, k;
while(m --)
{
cin >> l >> r;
ans = 0, k = (r - l + 1);
string s;
t = 0;
for(int i = 0; i < 31; i ++)
{
// cout << t << ' ';
t = b[r][i] - b[l - 1][i];
s += (t < k - t) + '0';
}
// cout << s << "\n";
for(int i = 30; i >= 0; i --)
{
if(s[i] == '0')
ans += ff[i];
}
ans = Inf ^ ans;
cout << ans << "\n";
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
init();
int T = 1;
// T = Read();
while(T --)
solve();
return T ^ T;
}
H. 左右横跳(线性dp)

输入
5 3 2
1 4 3
2 2 5
4 3 4
7 2 7
1 2 1
输出
11
说明
从第2列开始,初始分数为4,向上移动一个单位长度(不获取分数)到达第2行第2列,再向上跳k=2个单位长度到达第4行第1列,或第4行第3列,总分数都是11
点击查看代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+5;
int t,n,m,k,a[N],f[N],x;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m>>k;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>x;
a[i]=max(a[i],x);
}
}
for(int i=1;i<=k;i++) f[i]=a[1];
for(int i=k+1;i<=n;i++)
f[i]=max(f[i-1],f[i-k]+a[i]);
cout<<f[n]<<endl;
return 0;
}
I. 简单的组合(模拟)

输入
8
输出
134217728
点击查看代码
#include<bits/stdc++.h>
#define int long long
//#define mod 998244353
#define fi first
#define se second
using namespace std;
const int N = 2e5 + 10;
int st[N], prime[N], cnt;
typedef long long i64;
int n, ans;
int s[110], t;
void solve()
{
int n;
cin >> n;
while(n)
{
s[cnt ++] = n % 2;
n >>= 1;
}
string ss;
reverse(s, s + 32);
for(int i = 0; i < 32; i ++)
// if(s[i] == 0)
ss += s[i] + '0';
// cout << ss << "\n";
string a[10];
for(int i = 0; i < 4; i ++)
a[i] = ss.substr(i * 8, 8);
sort(a, a + 4);
// for(int i = 0; i < 4; i ++)
// cout << a[i] << "\n";
int ans = 0, t = 1;
for(int i = 0; i < 4; i ++)
{
for(int j = 7; j >= 0; j --)
{
if(a[i][j] == '1')
ans += t;
t *= 2;
}
}
cout << ans << '\n';
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
// cout << (1 << 27) << "\n";
int T = 1;
// T = Read();
while(T --)
solve();
return T ^ T;
}
J. 现代高手(二位前缀和)

输入
3 3
1 2 4
1 2 4
9
输出
4
说明
C矩阵为:
1 2 4
2 4 8
4 8 16
元素和小于等于9的面积最大的子矩阵就是左上角2*2的矩阵,所以答案是4。
点击查看代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1010;
int a[N], b[N], c[N][N], s[N][N];
int n, m, ans, x;
void solve()
{
cin >> n >> m;
for(int i = 1; i <= n; i ++) cin >> a[i];
for(int i = 1; i <= m; i ++) cin >> b[i];
cin >> x;
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= m; j ++)
{
c[i][j] = a[i] * b[j];
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + c[i][j];
}
// for(int i = 1; i <= n; i ++)
// {
// for(int j = 1; j <= m; j ++)
// cout << c[i][j] << ' ';
// cout << "\n";
// }
// for(int i = 1; i <= n; i ++)
// {
// for(int j = 1; j <= m; j ++)
// cout << s[i][j] << ' ';
// cout << "\n";
// }
int tt;
for(int i = 1; i <= n; i ++)
for(int j = i; j <= n; j ++)
for(int l = 1; l <= m; l ++)
for(int r = l; r <= m; r ++)
{
tt = s[j][r] - s[i - 1][r] - s[j][l - 1] + s[i - 1][l - 1];
// tt = s[r][j] - s[l - 1][j] - s[r][i - 1] + s[l - 1][i - 1];
if(tt <= x)
{
ans = max(ans, (r - l + 1) * (j - i + 1));
}
}
cout << ans << "\n";
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int T = 1;
// T = Read();
while(T --)
solve();
return T ^ T;
}

浙公网安备 33010602011771号