Educational Codeforces Round 18

时间:2022-12-20 18:17:55

A. New Bus Route

题目大意:给出n个不同的数,问差值最小的数有几对。(n<=200,000)

思路:排序一下,差值最小的一定是相邻的,直接统计即可。

#include<cstdio>
#include<algorithm>
using namespace std;
char B[<<],*S=B,C;int X,F;
inline int read()
{
for(F=;(C=*S++)<''||C>'';)if(C=='-')F=-;
for(X=C-'';(C=*S++)>=''&&C<='';)X=(X<<)+(X<<)+C-'';
return X*F;
}
#define MN 200000
int a[MN+];
int main()
{
fread(B,,<<,stdin);
int n=read(),i,mn=0x7FFFFFFF,cnt=;
for(i=;i<=n;++i)a[i]=read();
sort(a+,a+n+);
for(i=;i<n;++i)
{
a[i]=a[i+]-a[i];
if(a[i]<mn)mn=a[i],cnt=;
if(a[i]==mn)++cnt;
}
printf("%d %d",mn,cnt);
}

B. Counting-out Rhyme

题目大意:n个人站成一圈,一开始第1个人是首领,k次操作,每次首领向后数ai个人让那个人滚蛋并使这个人的后一个人变成首领,求每次谁滚蛋了。(1<=k<n<=100,ai<=10^9)

思路:ai对当前剩余人数取模后模拟。

#include<cstdio>
char B[<<],*S=B,C;int X;
inline int read()
{
while((C=*S++)<''||C>'');
for(X=C-'';(C=*S++)>=''&&C<='';)X=(X<<)+(X<<)+C-'';
return X;
}
#define MN 100
int u[MN+];
int main()
{
fread(B,,<<,stdin);
int n,k,i,x,s=;
n=read();k=read();
for(i=;i<k;++i)
{
for(x=read()%(n-i)+;x--;)while(u[++s>n?s=:s]);
u[s]=;printf("%d ",s);
}
}

C. Divide by Three

题目大意:给定一个长度为n的数字串,求一个最长的子序列满足没有前导0且是3的倍数。(n<=100,000)

思路:上过小学的都知道3的倍数各位数字和也是3的倍数。从后往前枚举哪个数字作为最后子序列的第一位数字,求出这个后缀各位之和模3的值,还有这一位后面有几个模3等1和模3等2的数,分情况判断即可。

#include<cstdio>
#include<cstring>
#define MN 100000
char s[MN+];
int main()
{
int n,i,f=,f1=,f2=,ans=-,p,pf1,pf2;
scanf("%s",s+);n=strlen(s+);
for(i=n;i;--i)
{
f=(f+(s[i]-=''))%;
if(s[i])
{
if(!f&&(ans<||i<=ans))ans=i-,p=i,pf1=pf2=;
if(f==&&f1&&(ans<||i<ans))ans=p=i,pf1=,pf2=;
if(f==&&f2>&&(ans<||i+<ans))ans=i+,p=i,pf1=,pf2=;
if(f==&&f2&&(ans<||i<ans))ans=p=i,pf1=,pf2=;
if(f==&&f1>&&(ans<||i+<ans))ans=i+,p=i,pf1=,pf2=;
}
else if(ans<)ans=-;
if(s[i]%==)++f1;
if(s[i]%==)++f2;
}
if(ans<-)return *puts("");
if(ans<)return *puts("-1");
for(printf("%d",s[i=p]);++i<=n;)
if(s[i]%==&&pf1)--pf1;
else if(s[i]%==&&pf2)--pf2;
else printf("%d",s[i]);
}

D. Paths in a Complete Binary Tree

题目大意:有一棵n个节点的满二叉树,节点标号为中序遍历,每次询问一个点,经过一串操作后在哪个点,操作有U,L,R,分别为走向父亲和左右儿子。(n<=10^18,操作总长<=10^5)

思路:我觉得转成先序遍历后再转回来比较直观,具体实现看下面的代码。

#include<cstdio>
#include<iostream>
using namespace std;
#define ll long long
#define MN 100000
char s[MN+];
ll find(ll x,ll n,ll k)
{
n>>=;
if(k<=n)return find(x<<,n,k);
if(k-=n+)return find(x<<|,n,k);
return x;
}
ll query(ll x,ll n)
{
int cnt=,a[];ll ans=;
while(x>)a[cnt++]=x&,x>>=;
while(cnt--){n>>=;if(a[cnt])ans+=n+;}
return ans++(n>>);
}
int main()
{
ll n,x;int q,i;
cin>>n>>q;
while(q--)
{
cin>>x;scanf("%s",s);
x=find(,n,x);
for(i=;s[i];++i)
{
if(s[i]=='U'&&x>)x>>=;
if(s[i]=='L'&&x<<<n)x<<=;
if(s[i]=='R'&&x<<<n)x=x<<|;
}
cout<<query(x,n)<<endl;
}
}

E. Colored Balls

题目大意:给出n个数ai,问把这些数字拆成若干个相差不超过1的数字最少拆几个。(n<=500,ai<=10^9)

思路:设最后全部拆成x和x-1,对于每个ai,符合条件的x满足x能整除ai或ai/x不小于x-1-ai%x,而对于每个ai,合法的x只有O(ai^0.5)种,枚举x<=ai^0.5,暴力check x,ai/x,ai/x-1,ai/x+1即可。复杂度O(n*ai^0.5)。

#include<cstdio>
#include<algorithm>
using namespace std;
#define MN 500
int n,a[MN+];
long long ans=1LL<<;
void cal(int x)
{
if(!x)return;
long long sum=;
for(int i=;i<=n;++i)
{
if(a[i]%x==||(a[i]/x>=x--a[i]%x))sum+=a[i]/x+bool(a[i]%x);
else return;
}
ans=min(ans,sum);
}
int main()
{
int i;
scanf("%d",&n);
for(i=;i<=n;++i)scanf("%d",&a[i]);
for(i=;i*i<=a[];++i)cal(i),cal(a[]/i),cal(a[]/i-),cal(a[]/i+);
printf("%I64d",ans);
}

Educational Codeforces Round 18的更多相关文章

  1. Educational Codeforces Round 18 D

    Description T is a complete binary tree consisting of n vertices. It means that exactly one vertex i ...

  2. Educational Codeforces Round 18 B

    Description n children are standing in a circle and playing the counting-out game. Children are numb ...

  3. Educational Codeforces Round 18 A

    Description There are n cities situated along the main road of Berland. Cities are represented by th ...

  4. Educational Codeforces Round 18 C&period; Divide by Three DP

    C. Divide by Three   A positive integer number n is written on a blackboard. It consists of not more ...

  5. Educational Codeforces Round 60 &lpar;Rated for Div&period; 2&rpar; - D&period; Magic Gems(动态规划&plus;矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  6. Educational Codeforces Round 40 F&period; Runner&&num;39&semi;s Problem

    Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...

  7. &lbrack;Educational Codeforces Round 16&rsqb;E&period; Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  8. &lbrack;Educational Codeforces Round 16&rsqb;D&period; Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

  9. &lbrack;Educational Codeforces Round 16&rsqb;C&period; Magic Odd Square

    [Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...

随机推荐

  1. Bootstrap 按钮和折叠插件

    ---恢复内容开始--- 一.按钮 可以通过按钮插件创建不同状态的按钮. //单个切换. <button class="btn btn-primary" data-toggl ...

  2. Tomcat8启动报there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache

    09-Dec-2016 10:57:49.150 WARNING [localhost-startStop-1] org.apache.catalina.webresources.Cache.getR ...

  3. linq to sql转载

    LINQ简介 LINQ:语言集成查询(Language INtegrated Query)是一组用于c#和Visual Basic语言的扩展.它允许编写C#或者Visual Basic代码以查询数据库 ...

  4. bzoj3798&colon; 特殊的质数

    分块打表.块内的暴力块外的打表.开始没有j>0所以WA了. #include<cstdio> #include<cmath> #include<cstring&gt ...

  5. 【转】C&plus;&plus;箴言:理解typename的两个含义

    [转载]http://dev.yesky.com/13/2221013.shtml 问题:在下面的 template declarations(模板声明)中 class 和 typename 有什么不 ...

  6. cocos2dX 它CCScene创建原则和切换模式

    今天, 让我们来看看现场CCScene创建原则和切换模式, 首先, 个什么样子: 我们先来看看效果: 啥也没有: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZX ...

  7. c&plus;&plus; STL 容器——联合容器

    STL提供了四种联合容器 set,multiset,map,multimap; set and multiset在<set>头文件 map and multimap在<map> ...

  8. HBase原理、设计与优化实践

    转自:http://www.open-open.com/lib/view/open1449891885004.html 1.HBase 简介 HBase —— Hadoop Database的简称,G ...

  9. 开始使用ARC

    Automatic Reference Counting (ARC)是编译器自动管理Objective-C对象的一个功能,相对于不得不考虑retain和release操作来说,ARC让我们有更多的精力 ...

  10. dubbo的一些默认变量

    dubbo默认变量表 变量名 描述 默认值 用途 DEFAULT_IO_THREADS 默认IO线程 Math.min(Runtime.getRuntime().availableProcessors ...