您的位置:首页 > 大数据 > 人工智能

personal training

2020-02-02 19:44 831 查看

Engines

题意:一个平面是,你在(0,0)上,然后给了n个坐标,你可以按照任何顺序,把某些坐标值加在原来的坐标值上面,要求到原点的距离最远!!!
题解:本来暴力枚举的,结果过了27个节点WA了,然后上网无意翻到学长的代码,真的是厉害…
将这n个二元组看做n个向量。
移动方式遵循平行四边形定则。
所以两个向量夹角越小,相加形成的和向量模长就越大。
所以将这些向量按照极角排序。
选择的向量肯定是一个区间。
枚举左右端点,求最大值即可。

using namespace std;
typedef long long ll;
const int N=2e5+10;
struct point
{
ll x,y;
}p[N];
bool cmp(point a,point b)
{
return atan2(a.y,a.x)<atan2(b.y,b.x);//atan2 -> 反正切
}
int main()
{
int n;	cin>>n;
for(int i=1;i<=n;i++)
scanf("%lld%lld",&p[i].x,&p[i].y);

int nx[N];   //用来循环
for(int i=1;i<=n;i++)
nx[i]=i+1;
nx[n]=1;

sort(p+1,p+1+n,cmp);
ll ans=0;
for(int i=1;i<=n;i++)
{
ll x=p[i].x;	ll y=p[i].y;
ans=max(ans,x*x+y*y);
for(int j=nx[i];j!=i;j=nx[j])
{
x += p[j].x;
y += p[j].y;
ans = max(ans, x * x + y * y);
}
}
printf("%.10lf\n",sqrt(ans));

return 0;
}

Consecutive Integers AtCoder - 5037

水题,读懂题意直接写就行

#include<iostream>
using namespace std;
typedef long long ll;
int main()
{
int n,k;
cin>>n>>k;
cout<<n-(k-1)<<endl;
return 0;
}

ModSum AtCoder - 4873

一定要认真读题…

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<stack>
#include<cstring>
using namespace std;
typedef long long ll;
ll a[1111111];
int main()
{
ios::sync_with_stdio(false);
ll n;	cin>>n;
ll sum=0;
for(ll i=0;i<n;i++)
sum=sum+i;     	//这里不应该写循环,应该用等差数列求和,以防TLE
cout<<sum<<endl;
return 0;
}

Shortest Path on a Line AtCoder - 5635

Counting of Trees AtCoder - 5633

看的挺难…其实做出来发现老水了!
题意:从节点1出发,给出到其他(n-1)个节点的边数,问你这样的走法(树)有多少

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
ll a[111111];
ll b[111111];
ll p(ll a,ll b)
{
ll ans=1;
while(b)
{
if(b&1)
ans=ans*a%mod;
b>>=1;
a=a*a%mod;
}
return ans;
}
int main()
{
ll n,x;
cin>>n>>a[0];
if(a[0])
{
for(ll i=1;i<n;i++)
scanf("%lld",&x);
cout<<0<<endl;
}
else
{

for(ll i=1;i<n;i++)
scanf("%lld",&a[i]);
sort(a,a+n);
if(a[1]==0)            //判断是否只有一个0,如果有>1个0.则输出0;
cout<<0<<endl;
else
{
ll flag=1;
int tt=1;
ll cnt=0;
ll sum=1;
for(ll i=0;i<n;i++)
{
if(a[i]==a[i+1])
{
flag++;
}
else if(a[i+1]>a[i]+1)  //判断数字是否连续,不连续则直接break;
{
tt=0;
cout<<0<<endl;
break;
}
else
{
b[cnt++]=flag;
flag=1;
}
}

if(tt)
{
for(ll i=1;i<cnt;i++)
{
ll f=p(b[i-1],b[i]);
sum=(sum%mod*(f%mod))%mod;
}
cout<<sum<<endl;
}
}
}
return 0;
}

自己写的比较low,再放一个大佬写的代码…

Monsters Battle Royale AtCoder - 4297

就是找他们的最小公约数…

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<stack>
using namespace std;
typedef long long ll;
ll a[1111111];
int main()
{
ll n;
cin>>n;
for(int i=0;i<n;i++)
scanf("%lld",&a[i]);
ll xx=a[0];
for(int i=1;i<n;i++)
xx=__gcd(xx,a[i]);
cout<<xx<<endl;

return 0;
}

Powerful Discount Tickets AtCoder - 4864

这个题意也很简单,但是当时直接暴力的,果然TLE了,后来才想到的优先队列,其实就是谁大就给谁打折

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<stack>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;
double a[1111111];
double cmp(double a,double b)
{
return a>b;
}
int main()
{
//ios::sync_with_stdio(false);
priority_queue<double> q;
double n,m,x;
cin>>n>>m;
for(int i=0;i<n;i++)
{
scanf("%lf",&x);
q.push(x);
}
for(int i=0;i<m;i++)
{
double y=q.top()/2.0;
q.pop();
q.push(y);
}
ll sum=0;
for(int i=0;i<n;i++)
{
sum=sum+floor(q.top());
q.pop();
}
cout<<sum<<endl;
return 0;
}

Attack Survival AtCoder - 4870

这个也很简单,因为总共有Q次,所以统计出每个人能加的分数,最后减去Q就行

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<stack>
#include<cstring>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
typedef long long ll;
ll a[1111111];
ll b[1111111];
ll c[1111111];

int main()
{
//ios::sync_with_stdio(false);
ll n,k,q,x;
cin>>n>>k>>q;
for(ll i=0;i<n;i++)
a[i]=k;
set<ll> st;
for(ll i=0;i<q;i++)
{
scanf("%lld",&x);
a[x-1]++;
}

for(ll j=0;j<n;j++)
if(a[j]-q>0)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
return 0;
}

Lower AtCoder - 4871

暴力就行,记得用LL…否则会WA掉的…

//#include<bits/stdc++.h>
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<stack>
using namespace std;
typedef long long ll;
ll a[1111111];
int main()
{
int n;cin>>n;
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]);
ll tt=0;
ll flag=0;

for(int i=1;i<n;i++)
{
if(a[i]>=a[i+1])
tt++;
else
tt=0;
flag=max(flag,tt);
}
cout<<flag<<endl;
return 0;
}

Kleene Inversion AtCoder - 5165

思路附在代码中了…

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
ll a[2222];
ll b[2222];
int main()
{
//ios::sync_with_stdio(flase);
ll n,k;
cin>>n>>k;
for(int i=0;i<n;i++)     //先读入
scanf("%lld",&a[i]);

for(int i=0;i<n;i++)     //统计读入的那组数据 统计每个数字比该组数字大的次数
{
for(int j=0;j<n;j++)
{
if(a[i]>a[j])
b[i]++;
}
}
ll sum1=0;
for(int i=0;i<n;i++)   //统计读入的这组数字按顺序比其他数字大的和
{
for(int j=i+1;j<n;j++)
{
if(a[i]>a[j])
sum1++;
}
}
ll sum=0;
ll cnt=0;
cnt=k%mod*((k-1)%mod)/2%mod;
for(int i=0;i<n;i++)     //统计剩余的(k-1)次
sum=sum+(b[i]%mod)*((cnt)%mod)%mod;

sum1=k%mod*(sum1%mod)%mod;
sum=(sum+sum1)%mod;
cout<<sum%mod<<endl;
return 0;
}

Two Contests AtCoder - 5660

Get Everything AtCoder - 5217

AB Substrings AtCoder - 5039

暴力枚举…详情附在代码中

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<stack>
#include<cstring>
using namespace std;
typedef long long ll;
ll a[1111111];
int main()
{
ios::sync_with_stdio(false);
ll n;	cin>>n;
ll xx=0;
ll yy=0;
ll zz=0;
ll sum=0;
string ss[111111];

for(ll i=0;i<n;i++)
{
string s;	cin>>s;
if(s[0]=='B'&&s[s.size()-1]=='A')   //枚举首位是'B',末尾是'A'的个数
zz++;
if(s[0]=='B'&&s[s.size()-1]!='A')    //同理
xx++;
if(s[0]!='B'&&s[s.size()-1]=='A')     //同理
yy++;
for(ll j=0;j<s.size()-1;j++)       //统计同一个字符串里面含有的"AB"的数量
if(s[j]=='A'&&s[j+1]=='B')
sum++;

}
int xx1=0;
int yy1=0;
if(zz)                //考虑一个字符串是否刚好首是B尾是A的情况
{
sum=sum+zz-1;
if(xx)
yy1=1;
if(yy)
xx1=1;
}
xx=xx+xx1;
yy=yy+yy1;
sum=sum+min(xx,yy);
cout<<sum<<endl;
return 0;
}
  • 点赞
  • 收藏
  • 分享
  • 文章举报
〆℡小短腿走快点ゝ 发布了30 篇原创文章 · 获赞 1 · 访问量 1408 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: