CodeForces:54

前言

整场比赛的数据范围都很良心
A是水题.
B是恶心的模拟,有一点锻炼代码能力
C是很水的数数套贪心题
D是被数据范围祸害了的构造题
E是一个很适合(像我一样)的不会计算几何的人做的计算几何题,只需要基本的向量知识

CF54A Presents

Description \text{Description} Description

你有 n n n 天时间,在这 n n n 天内,在给出的 c c c 个特定日期一定会得到礼物,此外,如果连续 k k k 天没有收到礼物,就会得到礼物.
1 ≤ k ≤ n ≤ 365 , 1 ≤ c ≤ n 1\le k\le n\le365,1\le c\le n 1kn365,1cn

Solution \text{Solution} Solution

如果 n ≤ 1 0 18 n\le10^{18} n1018,那可能还会有一些细节需要处理.
但是这个垃圾数据范围,直接模拟就行了…

#include<bits/stdc++.h>
using namespace std;
#define ll long long
//#define double long double
#define debug(...) fprintf(stderr,__VA_ARGS__)
const int N=2e5+100;
const int mod=1e9+7;
inline ll read(){
  ll x(0),f(1);char c=getchar();
  while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
  while(isdigit(c)){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
  return x*f;
}

int n,m,k;
int a[400];
signed main(){
  #ifndef ONLINE_JUDGE
  freopen("a.in","r",stdin);
  freopen("a.out","w",stdout);
  #endif
  n=read();k=read();
  m=read();
  for(int i=1;i<=m;i++){
    int x=read();a[x]=1;
  }
  int now=0,tot(0);
  for(int i=1;i<=n;i++){
    ++now;
    //printf("i=%d now=%d\n")
    if(now==k||a[i]){
      ++tot;now=0;
    }
  }
  printf("%d\n",tot);
  return 0;
}
/*

*/

CF54B Cutting Jigsaw Puzzle

Description \text{Description} Description

有一个 n × m n\times m n×m 的字符串矩形,你可以指定一个大小 ( x , y ) (x,y) (x,y),然后把矩形分割成 n x × m y \dfrac{n}{x}\times \dfrac{m}{y} xn×ym 个小矩形( x ∣ n , y ∣ m x|n,y|m xn,ym),若所有的小矩形(可以旋转,但不能翻转)后均互不相同,则称这种分割是合法的.
求合法 分割的方案总数以及子矩形最小的分割方案.
n , m ≤ 20 n,m\le20 n,m20.

Solution \text{Solution} Solution

一道复杂度怎么做应该都太不可能 TLE 的模拟题.
手玩出旋转对坐标的映射关系.
分别枚举因数暴力哈希 check 即可.
时间复杂度大概是 O ( n 3 ) O(n^3) O(n3).

Code \text{Code} Code

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
//#define double long double
#define debug(...) fprintf(stderr,__VA_ARGS__)
const int N=2e5+100;
const int mod=1e9+7;
inline ll read(){
  ll x(0),f(1);char c=getchar();
  while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
  while(isdigit(c)){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
  return x*f;
}

int n,m,k;
char s[25][25];
int key=31;
struct node{
  char a[25][25];
  inline ull calc(){
    ull res(0);
    for(int i=1;i<=n;i++){
      for(int j=1;j<=m;j++) res=res*key+a[i][j]-'A'+1;
    }
    return res;
  }
}now,pre,w[6];
map<ull,int>mp;
int mn,x,y,res,num;
bool work(int a,int b,int x,int y){
  //memset(now.a,0,sizeof(a));
  num=0;
  for(int i=1;i<=max(n,m);i++){
    for(int j=1;j<=max(n,m);j++) now.a[i][j]=0;
  }
  for(int i=1;i<=x;i++){
    for(int j=1;j<=y;j++){
      //printf("  %d %d c=%c\n",a+i-1,b+j-1,s[a+i-1][b+j-1]);
      now.a[i][j]=s[a+i-1][b+j-1];
    }
  }
  /*printf("(%d %d)\n",a,b);
  for(int i=1;i<=n;i++){
    for(int j=1;j<=m;j++) printf("%c",now.a[i][j]?now.a[i][j]:'0');
    putchar('\n');
  }
  printf("calc=%llu\n",now.calc());*/
  if(mp[now.calc()]) return false;
  w[++num]=now;
  for(int o=1;o<=3;o++){
    memcpy(pre.a,now.a,sizeof(now.a));
    for(int i=1;i<=max(n,m);i++){
      for(int j=1;j<=max(n,m);j++) now.a[i][j]=0;
    }
    for(int i=1;i<=x;i++){
      for(int j=1;j<=y;j++){
	//printf("  (%d %d)-> (%d %d)\n",i,j,j,x-i+1);
	now.a[j][x-i+1]=pre.a[i][j];
      }
    }
    swap(x,y);    
    if(o==2||x==y){
      /*for(int i=1;i<=n;i++){
	for(int j=1;j<=m;j++) printf("%c",now.a[i][j]?now.a[i][j]:'0');
	putchar('\n');
      }
      printf("calc=%llu\n",now.calc());
      */
      if(mp[now.calc()]) return false;
      w[++num]=now;
    }
  }
  for(int i=1;i<=num;i++) mp[w[i].calc()]=1;
  return true;
}
bool check(int x,int y){
  //printf("\nx=%d y=%d\n",x,y);
  mp.clear();
  for(int i=1;i<=n;i+=x){
    for(int j=1;j<=m;j+=y){
      if(!work(i,j,x,y)) return false;
    }
  }
  return true;
}
signed main(){
  #ifndef ONLINE_JUDGE
  freopen("a.in","r",stdin);
  freopen("a.out","w",stdout);
  #endif
  n=read();m=read();
  mn=2e9;
  for(int i=1;i<=n;i++){
    scanf(" %s",s[i]+1);
  }
  for(int i=1;i<=n;i++){
    if(n%i) continue;
    for(int j=1;j<=m;j++){
      if(m%j) continue;
      if(check(i,j)){
	++res;
	//printf("------------ok! (%d %d)\n",i,j);
	if(mn>i*j){
	  mn=i*j;x=i;y=j;
	}
      }
    }
  }
  printf("%d\n%d %d\n",res,x,y);
  return 0;
}
/*

*/

CF54C First Digit Law

Description \text{Description} Description

给出 n n n 个数,然后给出 n n n 行,每行 l i , r i l_i,r_i li,ri,代表第 i i i 个数在区间 [ l i , r i ] [l_i,r_i] [li,ri] 中,求一个概率使得这 n n n 个数中有 k % k\% k% 的数是 1 1 1 开头的.
n ≤ 1000 , 1 ≤ l i ≤ r i ≤ 1 0 18 n\le1000,1\le l_i\le r_i\le10^{18} n1000,1liri1018.

Solution \text{Solution} Solution

f x f_x fx [ 1 , x ] [1,x] [1,x] 符合条件的数的个数, s u m x = ∑ x = 0 x 1 0 i sum_x=\sum_{x=0}^x 10^i sumx=x=0x10i w x w_x wx 为满足 1 0 i ≤ x 10^i\le x 10ix 的最大的 i i i.
就有:
f x = min ⁡ ( 2 × 1 0 w x − 1 , x ) − 1 0 w x + 1 + s u m w x − 1 f_x=\min(2\times10^{w_x}-1,x)-10^{w_x}+1+sum_{w_x-1} fx=min(2×10wx1,x)10wx+1+sumwx1

那么第 i i i 个数符合条件的概率就是 f r i − f l i − 1 r i − l i + 1 \dfrac{f_{r_i}-f_{l_i-1}}{r_i-l_i+1} rili+1frifli1.
求出每个变量的概率之后,简单 n 2 n^2 n2 dp即可.
细节上,注意 1 0 18 10^{18} 1018 10 10 10 会爆 longlong,所以求 w x w_x wx 判断时要乘转除.

Code \text{Code} Code

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
//#define double long double
#define debug(...) fprintf(stderr,__VA_ARGS__)
const int N=1050;
const int mod=1e9+7;
inline ll read(){
  ll x(0),f(1);char c=getchar();
  while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
  while(isdigit(c)){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
  return x*f;
}

int n,m,k;
ll sum[20],mi[20];
double dp[N][N],p[N];
ll calc(ll x){
  if(x==0) return 0;
  int o=0;
  while(x/mi[o]>=10) ++o;
  return (o?sum[o-1]:0)+min(x,2*mi[o]-1)-mi[o]+1;
}
signed main(){
  #ifndef ONLINE_JUDGE
  freopen("a.in","r",stdin);
  freopen("a.out","w",stdout);
  #endif
  mi[0]=sum[0]=1;
  for(int i=1;i<=18;i++){
    mi[i]=mi[i-1]*10;sum[i]=sum[i-1]+mi[i];
  }
  n=read();
  for(int i=1;i<=n;i++){
    ll l=read(),r=read();
    p[i]=1.0*(calc(r)-calc(l-1))/(r-l+1);
    //printf("i=%d p=%lf\n",i,p[i]);
  }
  dp[0][0]=1;
  for(int i=0;i<n;i++){
    for(int j=0;j<=i;j++){
      dp[i+1][j+1]+=dp[i][j]*p[i+1];
      dp[i+1][j]+=dp[i][j]*(1-p[i+1]);
    }
  }
  int k=read();
  double res=0.0;
  for(int i=(n*k+99)/100;i<=n;i++) res+=dp[n][i];
  printf("%.10lf",res);
  return 0;
}
/*

*/

CF54D Writing a Song

Description \text{Description} Description

要求构造一个字符串 s s s,满足该串长度为 n n n,只出现字母表中前 k k k 个字母,并且在指定位置必须出现指定字符串 p p p,或者报告无解.
n ≤ 100 n\le100 n100.

Solution \text{Solution} Solution

第一眼:毒瘤计数题…
看完题意:恶心贪心题…
看到数据范围:就这?
或许贪心构造还是可做的,但是对于这个数据范围,我们不必冒着 WA 的风险动脑.
首先 KMP 预处理一下,然后设计 d p i , j dp_{i,j} dpi,j 表示填到第 i i i 位,匹配到 p p p 的第 j j j 位是否合法.
转移暴力在 kmp 上跳即可,这数据范围也不需要预处理加速了.
对于输出方案,dp 过程中记录一下转移路径即可.

Code \text{Code} Code

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
//#define double long double
#define debug(...) fprintf(stderr,__VA_ARGS__)
const int N=105;
const int mod=1e9+7;
inline ll read(){
  ll x(0),f(1);char c=getchar();
  while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
  while(isdigit(c)){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
  return x*f;
}

int n,m,k;
int dp[N][N],frm[N][N],id[N][N];
int p[N],jd[N];
char s[N];
void kmp(){
  p[1]=0;
  for(int i=1,j=0;i<m;i++){
    while(j&&s[i+1]!=s[j+1]) j=p[j];
    if(s[i+1]==s[j+1]) ++j;
    p[i+1]=j;
  }
  //for(int i=1;i<=m;i++) printf("i=%d p=%d\n",i,p[i]);
  return;
}
int find(int j,int id){
  char c='a'+id-1;
  while(j&&s[j+1]!=c) j=p[j];
  if(s[j+1]==c) ++j;
  return j;
}
void print(int k,int pl){
  if(!k) return;
  print(k-1,frm[k][pl]);
  putchar('a'+id[k][pl]-1);
}
signed main(){
  #ifndef ONLINE_JUDGE
  freopen("a.in","r",stdin);
  freopen("a.out","w",stdout);
  #endif
  n=read();k=read();
  scanf(" %s",s+1);m=strlen(s+1);
  for(int i=1;i<=n-m+1;i++) scanf("%1d",&jd[i+m-1]);
  kmp();
  dp[0][0]=1;
  for(int i=0;i<=n;i++){    
    if(jd[i]){
      if(!dp[i][m]){
	printf("No solution\n");return 0;
      }
      for(int j=0;j<m;j++) dp[i][j]=0;
      //dp[i][p[m]]=1;frm[i][p[m]]=dp[i][m];id[i][p[m]]=id[i][m];
    }
    else{
      dp[i][m]=0;
    }
    if(i==n) break;
    for(int j=0;j<=m;j++){
      if(!dp[i][j]) continue;
      for(int p=1;p<=k;p++){
	int to=find(j,p);
	//printf("k=%d (%d %d) -> (%d %d)\n",p,i,j,i+1,to);
	dp[i+1][to]=1;frm[i+1][to]=j;id[i+1][to]=p;
      }
    }
  }
  for(int i=0;i<=m;i++){
    if(dp[n][i]){
      print(n,i);return 0;
    }
  }
  printf("No Solution\n");
  return 0;
}
/*

*/

CF54E Vacuum Сleaner

Description \text{Description} Description

对于一个 n n n 个结点的凸包形状的吸尘器,问你这个吸尘器在清理矩形的角落时,遗留下的最小面积,可以旋转.
n ≤ 4 × 1 0 4 n\le 4\times10^4 n4×104.

Solution \text{Solution} Solution

可能是入门难度的计算几何了.
不难发现最优的时候墙一定和一条边重合(可以通过画圆证明).
那么我们就暴力枚举这条边,双指针维护墙另一条边过的顶点即可.
最后把所有点翻转过来再做一次.
实现上的技巧:

  1. 寻找另一条墙边过的点可以通过判断向量在当前边上投影的大小来实现.
  2. 计算面积时用墙的三角形面积减去多边形两个顶点截出的多边形面积,这个多边形面积可以处理一个面积关于顶点叉积的前缀和再减去这两个顶点叉积求出(建议画图理解).(算出的面积 可能为负,所以要取绝对值)
  3. 墙角三角形的边长一条可以通过投影来求,另一条勾股定理即可.

具体实现建议参考代码理解.

Code \text{Code} Code

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
//#define double long double
#define debug(...) fprintf(stderr,__VA_ARGS__)
const int N=1e5+100;
const int mod=1e9+7;
inline ll read(){
  ll x(0),f(1);char c=getchar();
  while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
  while(isdigit(c)){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
  return x*f;
}

int n,m,k;
struct P{
  double x,y;
}p[N];
inline P operator + (P a,P b){return (P){a.x+b.x,a.y+b.y};}
inline P operator - (P a,P b){return (P){a.x-b.x,a.y-b.y};}
inline double operator * (P a,P b){return a.x*b.x+a.y*b.y;}
inline double operator ^ (P a,P b){return a.x*b.y-a.y*b.x;}
inline double dis(P a,P b){return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}
inline double calc(double x,double y){return sqrt(x*x-y*y);}
double sum[N];
double solve(){
  double res=2e18;
  for(int i=n+1;i<=n+n;i++) p[i]=p[i-n];
  for(int i=2;i<=n+n;i++) sum[i]=sum[i-1]+(p[i]^p[i-1])/2;
  int j=1;
  for(int i=1;i<=n;i++){
    j=max(j,i+1);
    while(j+1<=n*2&&(p[i+1]-p[i])*(p[j+1]-p[i])>=(p[i+1]-p[i])*(p[j]-p[i])) ++j;
    if(j==i+1) res=0;
    if(j>2*n) break;
    double s=sum[j]-sum[i]+(p[i]^p[j])/2;
    double x=(p[j]-p[i])*(p[i+1]-p[i])/dis(p[i+1],p[i]);
    double y=calc(dis(p[i],p[j]),x);
    res=min(res,x*y/2-abs(s));
    //printf("i=%d j=%d s=%lf x=%lf y=%lf res=%lf\n",i,j,s,x,y,x*y/2-s);
  }
  return res;
}
signed main(){
  #ifndef ONLINE_JUDGE
  freopen("a.in","r",stdin);
  freopen("a.out","w",stdout);
  #endif
  n=read();
  for(int i=1;i<=n;i++) p[i].x=read(),p[i].y=read();
  double ans=solve();
  reverse(p+1,p+1+n);
  ans=min(ans,solve());
  printf("%.10lf\n",ans);
  return 0;
}
/*

*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值