409 总结
11.30 东塘 409 教室 - 第一次课【小田】
T854973 异或
题目传送门
时间
分数
😭😭😭
题目都没提交就不放了
正确思路
题目大意
对于每次询问
思路
询问的次数很多但是
正确代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=3e3+5;
int a[N],n,m,sum[N],maxn[N];
signed main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>a[i];
sum[i]=sum[i-1]^a[i];
}
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
maxn[j-i+1]=max(maxn[j-i+1],sum[j]^sum[i-1]);
}
}
while(m--)
{
int x;
cin>>x;
int f=0;
for(int i=1;i<=n;i++)
{
if(maxn[i]>=x)
{
cout<<i<<'\n';
f=1;
break;
}
}
if(!f)
{
cout<<-1<<'\n';
}
}
return 0;
}
T855429 报数
题目传送门
原题目传送门
时间
分数 = 30
🙂🙂🙂
暴力
暴力
预处理暴力
正确思路
题目大意
一个正整数
对于每次询问,给出一个
思路
我们可以用筛法来进行,一个
这样大约是能做到
正确代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e7+10;
int vis[N],sum[N],t;
vector<int>a;
bool check(int x)
{
while(x)
{
if(x%10==7)
{
return 1;
}
x/=10;
}
return 0;
}
void init(int n)
{
for(int i=1;i<=n;i++)
{
if(!check(i)){
continue;
}
if(vis[i])
{
continue;
}
for(int j=i;j<=n;j+=i)
{
vis[j]=1;
}
}
for(int i=1;i<=n;i++)
{
if(vis[i]==0)
{
a.push_back(i);
}
}
for(int i=0;i<a.size()-1;i++)
{
sum[a[i]]=a[i+1];
}
}
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
init((int)1e7+5);
cin>>t;
while(t--)
{
int x;
cin>>x;
if(vis[x]==1)
{
cout<<-1<<'\n';
}
else
{
cout<<sum[x]<<'\n';
}
}
return 0;
}
T687115 消息
题目传送门
时间
分数 = 30
🙂🙂🙂
暴力
也是我写的。很简单,有一个数组来存储他在哪个软件上,哪一个
正确思路
题目大意
空间复杂度:
正确代码
#include<bits/stdc++.h>
using namespace std;
const int N=3e5+10;
int n,m,idx=0,sum=0,p=0,cnt[N],bel[N],last[N];
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>m;
while(m--)
{
int op,x;
cin>>op>>x;
if(op==1)
{
cnt[x]++;
sum++;
idx++;
bel[idx]=x;
}
else if(op==2)
{
sum-=cnt[x];
cnt[x]=0;
last[x]=idx;
}
else
{
while(p<x)
{
p++;
int k=bel[p];
if(p>last[k])
{
sum--;
last[k]=p;
cnt[k]--;
}
}
}
cout<<sum<<'\n';
}
return 0;
}
T855429 报数
题目传送门
原题目传送门
时间
分数 = 0
正确思路
题目大意
每秒在所有蚯蚓里找到最长的一只,设长度为
思路
我们先想到优先队列来优化考虑不真的让每个蚯蚓长度增加,而是维护一个全局的偏移量 sum,蚯蚓实际的长度都需要加上 sum,那么只需要让 sum + q 就可以表示所有蚯蚓的长度都增加了
这样子就可以做到
#include<bits/stdc++.h>
#define int long long
using namespace std;
/*
1. 优先队列
2. 如何快速的让其他的蚯蚓都加 q
定义一个偏移量offset,把蚯蚓的长度视作全部都要 + offset
*/
int n, m, q, u, v, t, offset = 0;
priority_queue<int> a;
bool cmp(int a1, int a2) {
return a1 > a2;
}
signed main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin >> n >> m >> q >> u >> v >> t;
for (int i=1; i<=n; i++) {
int x;
cin >> x;
a.push(x);
}
for (int i=1; i<=m; i++) {
/*
1. 找出最大数字
2. 其他的数字都加q
3. 新的计算出来的数字加入数组中
*/
int x = a.top() + offset;
a.pop();
if (i % t == 0) cout << x << " ";
offset += q; // 所有蚯蚓都加q
int b = x * u / v;
a.push(b - offset);
a.push(x - b - offset);
}
cout << '\n';
for (int i=1; i<=n+m; i++) {
if (i % t == 0) cout << a.top() + offset << " ";
a.pop();
}
return 0;
}
我们可以把优先队列分解成三个队列
- 一开始就有的蚯蚓,从大到小排序,存到队列
a 里 - 每次切出来的前半段,按切出时间顺序存到队列
b 里 - 每次切出来的后半段,按切出时间顺序存到队列
c 里
这样就可以做到
正确代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5;
int n,m,q,u,v,t,offset=0;
int a[N];
queue<int>a1,a2,a3;
bool cmp(int a1,int a2)
{
return a1>a2;
}
int getmax()
{
int x=LLONG_MIN,y=LLONG_MIN,z=LLONG_MIN;
if(a1.size())x=a1.front();
if(a2.size())y=a2.front();
if(a3.size())z=a3.front();
if(x>=y and x>=z)
{
a1.pop();
return x;
}
if(y>=z)
{
a2.pop();
return y;
}
a3.pop();
return z;
}
signed main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>m>>q>>u>>v>>t;
for (int i=1;i<=n;i++)
{
cin>>a[i];
}
sort(a+1, a+1+n, cmp);
for(int i=1;i<=n;i++)
{
a1.push(a[i]);
}
for(int i=1;i<=m;i++)
{
int x=getmax()+offset;
if(i%t==0)
{
cout<<x<<' ';
}
offset+=q;
int b=x*u/v;
a2.push(b-offset);
a3.push(x-b-offset);
}
cout << '\n';
for (int i=1;i<=n+m;i++)
{
int x=getmax()+offset;
if(i%t==0)
{
cout<<x<<' ';
}
}
return 0;
}