7.30
A
统计
询问
(lower_bound太好用了)
#include<bits/stdc++.h>
using namespace std;
int n , a[100005] , m;
int main(){
cin >> n;
for( int i = 1 ; i <= n ; i++ ) cin >> a[i] , a[i] += a[i - 1];
cin >> m;
while(m--){
int k;
cin >> k;
int pos = lower_bound(a + 1 , a + n + 1 , k) - a;
cout << pos << "\n";
}
return 0;
}
B
令未做修改前最长的满足
如果修改
1、最好的情况是修改后1 2 3 7 5 6把
由于只能改成整数,所以 这种情况要求
2、可以把
由于只能修改一次,直接枚举修改的数统计最大值
#include<bits/stdc++.h>
using namespace std;
int n , a[100005] , b[100005] , c[100005] , ans;
int main(){
cin >> n;
for( int i = 1 ; i <= n ; i++ ) cin >> a[i];
for( int i = 1 ; i <= n ; i++ ){
b[i] = 1;
if( a[i - 1] < a[i] ) b[i] += b[i - 1];
}
for( int i = n ; i >= 1 ; i-- ){
c[i] = 1;
if( a[i + 1] > a[i] ) c[i] += c[i + 1];
}
for( int i = 1 ; i <= n ; i++ ){
if( a[i - 1] + 1 < a[i + 1] ) ans = max( ans , b[i - 1] + c[i + 1] + 1 );
ans = max( ans , b[i - 1] + 1 );
ans = max( ans , c[i + 1] + 1 );
}
cout << ans;
return 0;
}
C
鉴于出题人写了这么多组数据,可以说一说
令值域为
20%
sort(a + 1 , a + n + 1);
for( int i = 1 ; i <= n ; i++ ){
for( int j = i + 1 ; j <= n ; j++ ){
ans = max( ans , a[j] % a[i] );
}
}
cout << ans;
20%(2、3)
for( int i = 1 ; i <= maxn ; i++ ){
for( int j = 1 ; j <= maxn ; j++ ){
if( !c[i] || !c[j] ) continue;
if( j >= i ) continue;
ans = max( ans , i % j );
}
}
cout << ans;
100%
能想到这一步纯靠数据范围
由于对于不同模数是不同的,考虑枚举模数
此时只要卡个范围让
不难发现在
枚举
什么?你说复杂度是
NONONO,
#include<bits/stdc++.h>
using namespace std;
int n , a[200005] , ans , maxn , c[1000005] , s[1000005];
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
cin >> n;
for( int i = 1 ; i <= n ; i++ ) cin >> a[i] , maxn = max( maxn , a[i] ) , c[a[i]]++;
for( int i = 1 ; i <= maxn ; i++ ) s[i] = s[i - 1] + c[i];
for( int i = 2 ; i <= maxn ; i++ ){
if( !c[i] ) continue;
for( int j = i ; j <= maxn ; j += i ){
int l = j , r = min(j + i - 1 , maxn);
if( s[r] - s[l - 1] == 0 ) continue;
int pos = lower_bound(s + l , s + r + 1 , s[r]) - s;
ans = max( ans , pos % i );
}
}
cout << ans;
return 0;
}
D
直接贪心,每次找能取出来的方格中序号最大/最小的,删除并统计答案
1、方格能取出来,只能是在没有其他方块仅仅以该方块为支撑的时候
2、取完要删除它,同时也可能产生一些影响,例如:
13 14 15 16
7 8 9 10 11
1 2 3 4 5 6
当取出方块10的时候,方块4本来不能取现在可以了,方块11本来可以拿现在拿不了了
经过枚举,本来可以拿但现在拿不了的情况有:
1 | 1 | 1 | 1
x y | y x | x y | y x
上述情况如果取走x那么y就取不了了
令x的坐标为
本来拿不了现在可以拿的情况有:
x | x | x
y | y | y
上述情况如果取走x那么y就可以取了
令x的坐标为
每次找最大/最小如果都遍历一次总的复杂度是set维护最大/最小
删除最大/最小后遍历被这次删除影响的范围,把符合要求的加进set,把不符要求的从set中删去
复杂度是
注意:一开始还要扫一遍把所有能取的先加进set
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int M = 1e9 + 9;
int n , ans;
struct node{
int x , y;
} a[100005];
map<pair<int , int> , int> mp;
set<int> s;
bool check2(node p){//检查p是否仅由一个方块支撑
int cnt = 0;
for( int i = -1 ; i <= 1 ; i++ ){
cnt += mp.count({p.x + i , p.y - 1});
}
return (cnt == 1);
}
bool check(node p){//检查p能否放入s中
for( int i = -1 ; i <= 1 ; i++ ){
if(!mp.count({p.x + i , p.y + 1})) continue;
if( check2({p.x + i , p.y + 1}) ) return 0;
}
return 1;
}
void del(int p){
//删除当前选中的方块和受影响不能选的方块并扩展可选的方块
s.erase(p);
mp.erase({a[p].x , a[p].y});
for( int i = -2 ; i <= 2 ; i++ ){
node nt = {a[p].x + i , a[p].y};
if(!mp.count({nt.x , nt.y})) continue;
int nnum = mp[{nt.x , nt.y}];
if(!s.count(nnum)) continue;
if( !check(nt) ) s.erase(nnum);
}
for ( int i = -1 ; i <= 1 ; i++ ){
pair<int, int> tmp = {a[p].x + i, a[p].y - 1};
if (!mp.count(tmp)) continue;
int id = mp[tmp];
if (s.count(id)) continue;
if(check({tmp.first, tmp.second})) s.insert(id);
}
}
signed main(){
cin >> n;
for( int i = 0 ; i < n ; i++ ) cin >> a[i].x >> a[i].y , mp[{a[i].x , a[i].y}] = i;
for( int i = 0 ; i < n ; i++ ){
if( check({a[i].x , a[i].y}) ) s.insert(i);
}
for( int i = 0 ; i < n ; i++ ){
int num = 0;
if( i % 2 == 0 ) num = *s.rbegin();
else num = *s.begin();
del(num);
ans = ( ans * n % M + num ) % M;
}
cout << ans;
return 0;
}
总结
1、T2一直在想dp差点没做出来,实际上枚举就行了
2、T3直接想正解花了很多时间,但是打一打暴力其实就能看出来了,不仅差点没写出来还没时间打后面的暴力了
4、T4有好多地方把i写成p了考后调了一个晚上才全部调出来
3、T5取模时做了减法没判负数痛失30 (我以为只有dp要这么干嘞qwq)