Codeforces Round #340 (Div. 2)
来源:互联网 发布:win7不允许安装软件 编辑:程序博客网 时间:2024/05/16 15:52
题目链接:http://codeforces.com/contest/617
A.Elephant
题意:大象每次能移动1,2,3,4,5 步,问移动x步至少要多少次,贪心思想答案即(x+4)/5
#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>using namespace std;typedef long long ll;int main(){ int x; cin>>x; cout<<(x+4)/5<<endl;}
B.Chocolate
题意:给你一个长为n只含0 1的序列,将这个序列分开,使每一小组恰好只含有一个1,问你有多少种分法
只要考虑每两个1之间的0的个数即可,即前缀0和后缀0不必考虑,将这些数加一相乘即可,注意如果不含1需输出0
#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>using namespace std;typedef long long ll;int main(){ int n; int a[105]; cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; } int j=0,k; for(int i=1;i<=n;i++) if(a[i]==1){ j=i; break; } for(int i=n;i>=1;i--){ if(a[i]==1){ k=i; break; } } if(j==0){ cout<<0<<endl; return 0; } ll ans=1; int sum=0; for(int i=j;i<=k;i++){ if(a[i]==0){ sum++; } else { ans*=(sum+1); sum=0; } } cout<<ans<<endl;}
题意:已知n(n<=2000)朵花的坐标,和两个喷头的坐标,每个喷头的喷洒范围为以喷头为圆心的一个圆,两个喷头的坐标可自由设定分别为r1,r2,现在要使两个喷头能够浇到所有的花,问你r1^2+r2^2的最小值,答案必须为整数
刚拿到这道题是往二分的方向想,其实n只有2000,可以n^2复杂度,首先很容易理解每个喷头至少有一朵花在边界上,我们先将所有的花到第一个喷头的距离平方算出来并排个序,则对于每一个不同的距离,将比该距离大的花都与另一个喷头匹配,不断更新答案即可,注意会爆int
#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>using namespace std;typedef long long ll;const int maxn=2005;struct P{ll d;ll x,y;}p[maxn];ll x[maxn],y[maxn];bool cmp(P a,P b){ return a.d<b.d;}int main(){ int n; scanf("%d",&n); ll x1,y1,x2,y2; cin>>x1>>y1>>x2>>y2; for(int i=1;i<=n;i++) cin>>x[i]>>y[i]; for(int i=1;i<=n;i++){ p[i].d=(x[i]-x1)*(x[i]-x1)+(y[i]-y1)*(y[i]-y1); p[i].x=x[i];p[i].y=y[i]; } sort(p+1,p+1+n,cmp); ll ans=1e18; for(int i=0;i<=n;i++){ ll sum=0; while(i!=n&&p[i].d==p[i+1].d) i++; for(int j=i+1;j<=n;j++){ sum=max(sum,(p[j].x-x2)*(p[j].x-x2)+(p[j].y-y2)*(p[j].y-y2)); //cout<<j<<(p[j].x-x2)*(p[j].x-x2)+(p[j].y-y2)*(p[j].y-y2)<<endl; } ans=min(ans,sum+p[i].d); } cout<<ans<<endl;}
D. Polyine
题意:已知三个点的坐标,现在需要用若干条线段将其串起来,要求线段不能有交点且平行于坐标轴问你至少需要多少条线段,如图所示
这题wa了好几发,需要分清情况
首先只需要一条线段的情况:三个点横(纵)坐标都相等
两条线段的情况:有两个点的横(纵)坐标相等,且另外一个点的纵(横)坐标在其他两个点外面
其他的情况都是三个点
#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>using namespace std;typedef long long ll;int main(){ ll x1,y1,x2,y2,x3,y3; cin>>x1>>y1>>x2>>y2>>x3>>y3; if((x3-x1)*(y2-y1)==(y3-y1)*(x2-x1)){ if(y2==y1||x2==x1) cout<<1<<endl; else cout<<3<<endl; } else if(x1==x2&&(y3>=max(y2,y1)||y3<=min(y2,y1))) cout<<2<<endl; else if(x2==x3&&(y1>=max(y2,y3)||y1<=min(y2,y3))) cout<<2<<endl; else if(x1==x3&&(y2>=max(y3,y1)||y2<=min(y3,y1))) cout<<2<<endl; else if(y1==y2&&(x3>=max(x2,x1)||x3<=min(x2,x1))) cout<<2<<endl; else if(y2==y3&&(x1>=max(x2,x3)||x1<=min(x2,x3))) cout<<2<<endl; else if(y1==y3&&(x2>=max(x3,x1)||x2<=min(x3,x1))) cout<<2<<endl; else cout<<3<<endl;}
题意:给你一个长度为n(n<=1e5)的序列a,(0<=a[i]<=1e6),已知q个询问和一个整数k,每个询问给你两个整数l,r(1<=l<=r<=n),问你有多少对i,j满足l<=i<=j<=r,且a[i]^a[i+1]^......^a[j]=k.
该问题满足莫队算法的条件,先处理出s[i]=a[1]^a[2]^......^a[i],则a[i]^a[i+1]^......^a[j]=s[j]^s[i-1];
存储询问,分块排序,离线查询
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>using namespace std;const int maxn=3000005;int n,m,k;int L=1,R=0;long long sum=0;struct NODE{ int l,r,i;}q[maxn];int a[maxn],xo[maxn],kuai[maxn];long long p[maxn],ans[maxn];bool cmp(NODE a,NODE b){ if(kuai[a.l]==kuai[b.l]) return a.r<b.r; return kuai[a.l]<kuai[b.l];}void add(int x){ sum+=p[xo[x]^k]; p[xo[x]]++;}void de(int x){ p[xo[x]]--; sum-=p[xo[x]^k];}int main(){ p[0]=1; cin>>n>>m>>k; int cnt=sqrt(n); for(int i=1;i<=n;i++){ cin>>a[i]; xo[i]=xo[i-1]^a[i]; kuai[i]=i/cnt; } for(int i=1;i<=m;i++){ cin>>q[i].l>>q[i].r; q[i].i=i; } sort(q+1,q+1+m,cmp); for(int i=1;i<=m;i++){ while(q[i].l>L){ de(L-1); L++; } while(q[i].l<L){ L--; add(L-1); } while(q[i].r>R){ R++; add(R); } while(q[i].r<R){ de(R); R--; } ans[q[i].i]=sum; } for(int i=1;i<=m;i++) cout<<ans[i]<<endl;}
- Codeforces Round #340 (Div. 2)
- Codeforces Round #340 (Div. 2)
- Codeforces Round #340 (Div. 2)
- Codeforces Round #340 (Div. 2)
- Codeforces Round #198 (Div. 2) 340C
- Codeforces Round #340 (Div. 2) 总结
- Codeforces Round #340 (Div. 2):A. Elephant
- Codeforces Round #340 (Div. 2):B. Chocolate
- Codeforces Round #340 (Div. 2)题解
- Codeforces Round #340 (Div. 2)[Codeforces617]
- Codeforces Round #340 (Div. 2)题解
- Codeforces Round #340 (Div. 2) C
- Codeforces Round #340 (Div. 2)(A) 模拟
- Codeforces Round #340 (Div. 2) by komendart
- Codeforces Round #340 (Div. 2)--补题
- Codeforces Round #340 (Div. 2) D. Polyline
- Codeforces Round #340 (Div. 2)D. Polyline
- Codeforces Round #340 (Div. 2)-D. Polyline
- 面试(其他)
- 常见排序算法
- Linux下修改Mysql的用户(root)的密码
- PAT 1135. Is It A Red-Black Tree (30) 二叉搜索树建立 + 红黑树判断
- ACM周末总结—9月17日
- Codeforces Round #340 (Div. 2)
- 第四周 建设“单链表”算法库
- python学习2
- PAT-1132 Cut Integer (整数分割)
- [线段树] HDU 1754
- HDU1081 最大字段和 压缩数组
- angular js 日程安排表
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 F.Trig Function(论文+组合数)
- LeetCode 203. Remove Linked List Elements