10.3下午测试-407
10.3下午测试-407
序列
失策,忘记看时间复杂度,只得了30分
30分暴力代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[100005],b[100005];
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while(t--)
{
int n,sum=0;
cin >> n;
for(int i=1;i<=n;++i)
{
cin >> a[i];
b[i]=65537;
}
b[1]=a[1];
sum=a[1];
for(int i=2;i<=n;++i)
{
for(int j=1;j<i;++j)
{
b[i]=min(b[i],abs(a[i]-a[j]));
}
sum+=b[i];
}
cout << sum << "\n";
}
return 0;
}
w(゚Д゚)wTLE了7个点
注意:
0\le a_{i}\le65536
既然
o( ̄▽ ̄)d good就是这样
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5;
int a[N],b[N],n;
bool vis[N];
signed main()
{
int T;
cin>>T;
while(T--)
{
memset(vis,0,sizeof vis);
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
int sum=a[1];
vis[a[1]]=1;
for(int i=2;i<=n;i++)
{
int tmp=a[i];
for(int j=0;;j++)
{
if(tmp+j<=65536 && vis[tmp+j]==1)
{
sum+=j;
break;
}
if(tmp-j>=0 && vis[tmp-j]==1)
{
sum+=j;
break;
}
}
vis[a[i]]=1;
}
cout<<sum<<'\n';
}
return 0;
}
火星上的加法运算
不得不说,我真是好样的,眼瞎了今天,小写字母都可以写成大写
此题不就是B进制星球吗
#include<bits/stdc++.h>
using namespace std;
string s1,s2;
int B;
char int2char(int x)
{
if(x<10)return x+'0';
else return x-10+'a';
}
int char2int(char c)
{
if(c>='a'&&c<='z')return c-'a'+10;
else return c-'0';
}
string add(string a,string b)
{
int len=max(a.size(),b.size());
while(a.size()<len)a="0"+a;
while(b.size()<len)b="0"+b;
string c;
int jw;
for(int i=len-1;i>=0;--i)
{
int ta=char2int(a[i]);
int tb=char2int(b[i]);
jw+=ta+tb;
char ch=int2char(jw%B);
jw/=B;
c=ch+c;
}
if(jw != 0){
c = '1' + c;
}
return c;
}
int main()
{
cin >> B >> s1 >> s2;
cout << add(s1,s2);
return 0;
}
上学路线
标准的dfs
#include<bits/stdc++.h>
using namespace std;
int a,b,k,x,y,sum;
bool s[20][20];
void dfs(int x,int y){
if(x==a&&y==b){
sum++;
return;
}
if(s[x][y+1]) dfs(x,y+1);
if(s[x+1][y]) dfs(x+1,y);
}
int main()
{
cin>>a>>b;
for(int i=1;i<=a;i++){
for(int j=1;j<=b;j++){
s[i][j]=true;
}
}
cin>>k;
while(k--){
cin>>x>>y;
s[x][y]=false;
}
dfs(1,1);
cout<<sum;
return 0;
}