CSU 1408: 種植樹苗 1409: 集合的並 1411: Longest Consecutive Ones 1412: Line and Circles
阿新 • • 發佈:2019-01-27
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define LL long long
using namespace std;
const int MAX = 1e6+10;
const int INF = 0x3fffffff;
int a[MAX];
bool check[MAX];
int main(){
int t;
cin>>t;
while(t--){
memset(a,0 ,sizeof(a));
memset(check,true,sizeof(check));
int n,d;
scanf("%d%d",&n,&d);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
sort(a,a+n);
int l = 0;
for(int i=1;i<n;i++){
if(a[i]-a[l]>=d){
l = i;
}
else {
check[i] = false;
}
}
int num = 0;
for(int i=0;i<n;i++){
if(check[i]) num++;
}
cout<<num<<endl;
}
return 0;
}
/**********************************************************************
Problem: 1408
User: 3901140225
Language: C++
Result: AC
Time:896 ms
Memory:6904 kb
**********************************************************************/
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=200005;
int n,m;
struct point
{
int x,y;
}f[maxn];
bool mycomp(const point &a,const point &b)
{
if(a.x!=b.x) return a.x<b.x;
else return a.y>b.y;
}
void solve()
{
int i,ans = 0;
int start=0;
for(i=0;i<n+m;i++)
{
if(f[i].x>start) start=f[i].x;
if (f[i].y>=start)
{
ans+=f[i].y-start+1;
start = f[i].y+1;
}
}
printf("%d\n",ans);
}
int main()
{
int t,i;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=0;i<n;i++) scanf("%d %d",&f[i].x,&f[i].y);
scanf("%d",&m);
for(i=n;i<m+n;i++) scanf("%d %d",&f[i].x,&f[i].y);
sort(f,f+n+m,mycomp);
solve();
}
return 0;
}
/**********************************************************************
Problem: 1409
User: 3901140225
Language: C++
Result: AC
Time:464 ms
Memory:3584 kb
**********************************************************************/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long int LL;
const int maxn=100100;
LL pos[maxn],sum[maxn],K;
int cnt;
char str[maxn];
bool ck(int l,int r)
{
int mid=(l+r)/2;
LL temp=0;
int num=mid-l;
temp+=pos[mid]*num-num*(num+1)/2-sum[mid-1]+sum[l-1];
num=r-mid;
temp+=sum[r]-sum[mid]-num*(num+1)/2-pos[mid]*num;
if(temp>K)
return false;
return true;
}
int main()
{
int T_T;
scanf("%d",&T_T);
while(T_T--)
{
scanf("%s",str+1);
scanf("%I64d",&K);
int len=strlen(str+1);
cnt=1;
for(int i=1;i<=len;i++)
{
if(str[i]=='1')
{
pos[cnt]=i;
sum[cnt]=pos[cnt]+sum[cnt-1];
cnt++;
}
}
cnt--;
int i=1,ans=0,maxn=1;
while(true)
{
int j=i+maxn-1;
if(j>cnt) break;
if(ck(i,j))
{
ans=maxn; maxn++;
}
else i++;
}
printf("%d\n",ans);
}
return 0;
}
/**********************************************************************
Problem: 1411
User: 3901140225
Language: C++
Result: AC
Time:136 ms
Memory:3684 kb
**********************************************************************/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define inf 1e20
#define eps 1e-8
const int N = 100005 ;
const double PI = 2.0*asin(1.0); //高精度求PI
struct Lpoint
{
double x,y;
}a[N], b[N]; //點
struct Llineseg
{
Lpoint a,b;
}; //線段
struct Ldir
{
double dx,dy;
}; //方向向量
struct Lline
{
Lpoint p;
Ldir dir;
}; //直線
bool mult(Lpoint sp, Lpoint ep, Lpoint op)
{
return (sp.x - op.x) * (ep.y - op.y)
>= (ep.x - op.x) * (sp.y - op.y);
}
bool operator < (const Lpoint &l, const Lpoint &r)
{
return l.y < r.y || (l.y == r.y && l.x < r.x);
}
int graham(Lpoint pnt[], int n, Lpoint res[])
{
int i, len, top = 1;
sort(pnt, pnt + n);
if (n == 0) return 0;
res[0] = pnt[0];
if (n == 1) return 1;
res[1] = pnt[1];
if (n == 2) return 2;
res[2] = pnt[2];
for (i = 2; i < n; i++)
{
while (top && mult(pnt[i], res[top], res[top-1]))
top--;
res[++top] = pnt[i];
}
len = top;
res[++top] = pnt[n - 2];
for (i = n - 3; i >= 0; i--)
{
while (top!=len && mult(pnt[i], res[top], res[top-1])) top--;
res[++top] = pnt[i];
}
return top; // 返回凸包中點的個數
}
void format(Lline ln, double& A, double& B, double& C)
{
A=ln.dir.dy;
B=-ln.dir.dx;
C=ln.p.y*ln.dir.dx-ln.p.x*ln.dir.dy;
}
double p2ldis(Lpoint a, Lline ln)
{
double A,B,C;
format(ln,A,B,C);
return(fabs(A*a.x+B*a.y+C)/sqrt(A*A+B*B));
}
double CPMD(Lpoint p[], int n)//ConvexPolygonMinimumDiameter
{
int i, j;
double ans = inf, tmp;
p[n] = p[0];
Lline ln;
Ldir dir;
for(i = 0, j = 1; i < n; ++i)
{
if((i+1)%n == j) j = (j + 1) % n;
dir.dx = p[i].x - p[i+1].x;
dir.dy = p[i].y - p[i+1].y;
ln.dir = dir;
ln.p = p[i];
while((tmp = p2ldis(p[j], ln)) < (p2ldis(p[(j+1)%n], ln)))
j = (j + 1) % n;
ans = min(ans, tmp);
}
return ans;
}
double dis(Lpoint u, Lpoint v)
{
return sqrt((u.x-v.x) * (u.x-v.x) + (u.y - v.y)*(u.y - v.y));
}
int main()
{
int n, t;
double r;
scanf("%d", &t);
while(t--)
{
scanf("%d%lf", &n, &r);
for(int i = 0; i < n; i++)
scanf("%lf%lf", &a[i].x, &a[i].y);
int m = graham(a, n, b);
if(m <= 2)
{
printf("Yes\n");
continue;
}
double k = CPMD(b, m);
if(k - 2*r < eps)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
/**********************************************************************
Problem: 1412
User: 3901140225
Language: C++
Result: AC
Time:608 ms
Memory:4260 kb
**********************************************************************/