当前位置:网站首页>NewOJ Week 6
NewOJ Week 6
2022-06-21 19:33:00 【WAWA源】
B 特殊数字—预处理+二分
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define int long long
const int N = 100010;
vector<int>nums;
signed main()
{
for(int i=0;i<=30;i++)
for(int j=i+1;j<=30;j++)
nums.push_back((1<<i)+(1<<j));
sort(nums.begin(),nums.end());
int T;cin>>T;
while(T--)
{
int x;cin>>x;
int res=1e9;
int pos=lower_bound(nums.begin(),nums.end(),x)-nums.begin();
if(nums[pos]==x)res=0;
else
{
res=nums[pos]-x;
if(pos>0)res=min(res,x-nums[pos-1]);
}
cout<<res<<'\n';
}
}
C 质数拼图游戏—BFS
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
using namespace std;
#define int long long
int dx[4]={
0,1};
int dy[4]={
1,0};
int vis[21]={
0,
0,1,1,0,1,0,1,0,0,0,
1,0,1,0,0,0,1,0,1,0,
};
queue<string>q;
map<string,int>mp;
void bfs(string s)
{
q.push(s);
mp[s]=0;
while(q.size())
{
string t=q.front();
q.pop();
for(int i=0;i<3;i++)for(int j=0;j<3;j++)
{
int x1=i*3+j;
for(int k=0;k<2;k++)
{
int x=i+dx[k];
int y=j+dy[k];
if(x>=3||y>=3)continue;
int x2=x*3+y;
if(vis[t[x1]-'0'+t[x2]-'0'])
{
string ss=t;
swap(ss[x1],ss[x2]);
if(!mp.count(ss))
{
mp[ss]=mp[t]+1;
q.push(ss);
}
}
}
}
}
}
signed main()
{
string s="123456789";
bfs(s);
int T;cin>>T;
while(T--)
{
string ss="";
for(int i=0;i<9;i++)
{
char op;cin>>op;
ss+=op;
}
if(mp.count(ss))cout<<mp[ss]<<'\n';
else cout<<-1<<'\n';
}
}
D 推箱子—差分
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define int long long
const int N=1000010;
int s[N]={
0};
signed main()
{
int n,h;cin>>n>>h;
for(int i=1,l,r;i<=n;i++)
{
cin>>l>>r;
s[l]++;
s[r+1]--;
}
for(int i=1;i<=n;i++)s[i]+=s[i-1];
for(int i=1;i<=n;i++)s[i]+=s[i-1];
int res=1e18;
for(int i=1;i+h-1<=n;i++)
{
int t=s[i+h-1]-s[i-1];
res=min(res,n*h-t);
}
cout<<res<<'\n';
}
E 最小公倍数—数学推导
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<cmath>
using namespace std;
#define int long long
const int N=1000010 , INF=1e18;
map<int,pair<int,int>>mp;
void init()
{
for(int l=1;l<=2000000;l++)
{
int sum=l*(l+1);
for(int r=l+2;;r++)
{
int g=__gcd(sum,r);
if(sum/g>INF/r)break;
sum=sum/g*r;
if(!mp.count(sum))
{
mp[sum]={
l,r};
}
}
}
}
void solve()
{
pair<int,int>res;
int n;cin>>n;
int t=(sqrt(4*n+1)-1)/2;
if(t*(t+1)==n)
{
res={
t,t+1};
if(mp.count(n))
{
if(mp[n].first<t)
res=mp[n];
}
}
else if(mp.count(n))res=mp[n];
else
{
cout<<-1<<'\n';
return ;
}
cout<<res.first<<" "<<res.second<<'\n';
}
signed main()
{
init();
int T;cin>>T;
while(T--)solve();
}
边栏推荐
- What noteworthy technologies of gold: the importance of fund management
- volatile
- Is it true and safe for qiniu to open a securities account? Do you charge for opening an account
- 机器学习之数据处理与可视化【鸢尾花数据分类|特征属性比较】
- MySQL learning (from getting started to mastering 1.2)
- LeeCode70 爬楼梯
- 获取OpenHarmony源码:从DevEco Marketplace获取(1)
- Data types and usage of mediacodec
- Decision tree learning notes
- How functions are declared
猜你喜欢

向量與平面交點

产品创新 | 物极必反,回归真实生活的创新社交APP

高考后网上查询信息,注意防范没有 SSL证书的网站

高性能内网DNS系统介绍

异步方法 理解(demo附代码)

Take off, annual salary: 400000+

Jingdong 39 year old "graduate" found a new job within a week after being laid off, with a salary increase of 20%!

Get the openharmony source code: get it from the deveco marketplace (1)

MySQl学习(从入门到精通 1.2)

集群二---LVS负载均衡群集DR模式
随机推荐
String类型转换成List<Integer>
What are the applications of 4.3-inch touch screen intelligent network central control host
全局负载均衡实现原理
Simulate the input and get the contents of print output
How functions are declared
UIButton实现左文字右图片
Server body 17: simple understanding of memory mapping and shared memory
Golang learning notes - pointer
Henkel database custom operator '! ~~'
向量与平面交点
AB打包有的Shader没有触发IPreprocessShaders的回调
ASP. Net core creates razor page and uploads multiple files (buffer mode)
延长 洁鲜生密实袋,给食材上一把“安全锁”
Ns32f103vbt6 hardware and software replace stm32f103vbt6
evaluating expression ‘ew.sqlSegment != null and ew.sqlSegment != ‘‘ and ew. mybaties plus问题
Extend the clean, fresh and dense bag, and put a "safety lock" on the ingredients
向量與平面交點
文件编译过程
Show you how to distinguish several kinds of parallelism
Is it true and safe for qiniu to open a securities account? Do you charge for opening an account