来自FallDream的博客,未经允许,请勿转载,谢谢。

------------------------------------------------------

A.Anastasia and pebbles

你有两个口袋,每种口袋最多只能装k个物品,有n种物品,每种物品有ai个,两种不同的物品不能混在一起,问最少要装多少次.

题解:.............

#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<cstring>
#include<queue>
#include<algorithm>
#include<map>
#define ll long long
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-''; ch=getchar();}
return x*f;
} ll n,k;
ll ans=; int main()
{
n=read();k=read();
for(int i=;i<=n;i++)
{
ll x=read();
ans+=(x+k-)/k;
}
cout<<(ans+)/;
return ;
}

B.Masha and geometric depression
给定一个等比数列,第一项是b1,之后每一项是前一项乘以q,但是b1和q都可以是负数或0。你还有m个不吉利的数字。每当满足abs(b)<=l的时候,你会把b写出来,除非它是一个不吉利的数字。

问你会写出多少个数字,当然你可能写出无限多的数字。

题解:大判断。  一开始我以为不满足还能继续写....结果pretest WA了一排...

#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<cstring>
#include<queue>
#include<algorithm>
#include<map>
#define ll long long
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-''; ch=getchar();}
return x*f;
} int b1,q,l,m;
map<int,bool> mp; inline int abs(int x){return x<?-x:x;} int main()
{
b1=read();q=read();l=read();m=read();
if(abs(b1)>l) return *puts("");
for(int i=;i<=m;i++)mp[read()]=;
if(b1==)
{
if(mp[])return *puts("");
else return *puts("inf");
}
if(q==)
{
if(!mp[])return *puts("inf");
if(abs(b1)<=l&&!mp[b1]) return *puts("");
puts("");
return ;
}
if(q==)
{
if(abs(b1)<=l&&!mp[b1])return *puts("inf");
else return *puts("");
}
if(q==-)
{
int ans=;
if(abs(b1)<=l&&!mp[b1])ans++;
if(abs(b1)<=l&&!mp[-b1]) ans++;
if(ans) return *puts("inf");
else return *puts("");
}
else
{
int ans=(abs(b1)>l||mp[b1])?:;
while(1LL*abs(q)*abs(b1)<=(ll)l)
{
b1*=q;
if(abs(b1)<=l&&!mp[b1])ans++;
}
cout<<ans<<endl;
}
return ;
}

C.Functions again

给定一个数字序列ai和函数f,求f的最大值。  $n\leqslant 100000$

题解:发现每一种$|a[i]-a[i+1]|$只有两种不同的贡献,分开计算即可。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<cstring>
#include<queue>
#include<algorithm>
#define ll long long
#define INF 1000000000
#define MN 100000000
using namespace std;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-''; ch=getchar();}
return x*f;
} int n;
int a[];
ll f[],f2[];
ll ans=;
int abs(int x){return x<?-x:x;} int main()
{
n=read();
for(int i=;i<=n;i++)a[i]=read();
for(int i=;i<=n;i++) f[i]=f[i-]+1LL*(i&?:-)*abs(a[i]-a[i+]);
for(int i=;i<=n;i++) f2[i]=f2[i-]+1LL*(i&?-:)*abs(a[i]-a[i+]);
ll minn=;
for(int i=;i<n;i++)
{
ans=max(ans,f[i]-minn);
if(i%==)minn=min(minn,f[i]);
}
minn=f2[];
for(int i=;i<n;i++)
{
// cout<<i<<" "<<f2[i]<<" "<<ans<<" "<<minn<<endl;
ans=max(ans,f2[i]-minn);
if(i&)minn=min(minn,f2[i]);
}
cout<<ans<<endl;
return ;
}

D. Weird journey

给定一个无向图,可能有自环,无重边,你要求出满足恰好有两条边只走了一次,其它都走了两次的路径数量。两种方案不同当且仅当边不同。  $n,m\leqslant 10^{6}$

题解:先判联通。然后我们把自环和普通边分开考虑。

如果都选普通边,那么这两条边一定要有公共点,用度数计算一下即可。

如果一个选自环,那么显然任意普通边都能成为另一条边。

如果两个都选自环,那么显然没问题。

#include<iostream>
#include<cstdio>
#define ll long long
#define MN 1000000
using namespace std;
inline int read()
{
int x = , f = ; char ch = getchar();
while(ch < '' || ch > ''){ if(ch == '-') f = -; ch = getchar();}
while(ch >= '' && ch <= ''){x = x * + ch - '';ch = getchar();}
return x * f;
} int n,m,cnt=,head[MN+],num=,num2=,s[MN+];
struct edge{int to,next;}e[MN*+];
ll ans=;
bool mark[MN+],b[MN+];
void ins (int f,int t){e[++cnt]=(edge){t,head[f]};head[f]=cnt;} void dfs(int x)
{
mark[x]=;
for(int i=head[x];i;i=e[i].next)
if(!mark[e[i].to])
dfs(e[i].to);
} int main()
{
n=read();m=read();
for(int i=;i<=m;i++)
{
int u=read(),v=read();
if(u==v) num2++,b[u]=;
else ins(u,v),ins(v,u),num++,s[u]++,s[v]++;
}
for(int i=;i<=n;i++)
if(b[i]||head[i])
{
dfs(i);break;
}
for(int i=;i<=n;i++) if(!mark[i]&&(head[i]||b[i])) return *puts("");
for(int i=;i<=n;i++) ans+=1LL*s[i]*(s[i]-)/;
ans+=1LL*num2*(m-num2)+1LL*num2*(num2-)/;
printf("%lld",ans);
return ;
}

E. The Great Mixing

给定n个数ai,你要从中选出最少的数,每个数可以选任意次,使得平均值等于k   $n\leqslant 10^{6} , ai,k\leqslant 1000$

题解:显然n最多只有1000种,然后我们分成正的和负的考虑。我们发现我们选的数的总和不会超过$10^{6}$,所以直接bfs就可以了,复杂度不是满的,应该能比较快找出答案。

数据挺水的,直接dp好像也能过。

#include<iostream>
#include<cstdio>
#define MAXN 1000000
using namespace std;
inline int read()
{
int x = , f = ; char ch = getchar();
while(ch < '' || ch > ''){ if(ch == '-') f = -; ch = getchar();}
while(ch >= '' && ch <= ''){x = x * + ch - '';ch = getchar();}
return x * f;
} int n,k;
bool mark[];
int s[],q[MAXN+],top=,cnt=,d[MAXN+]; int main()
{
k=read();n=read();
for(int i=;i<=n;i++)
{
int x=read()-k+;
if(!mark[x]) mark[x]=,s[++cnt]=x-;
}
d[]=;
for(int i=;i<=top;i++)
for(int j=;j<=cnt;j++)
{
if(s[j]+q[i]==) return *printf("%d",d[q[i]]);
if(s[j]+q[i]<=MAXN&&s[j]+q[i]>=&&!d[s[j]+q[i]]) d[s[j]+q[i]]=d[q[i]]+,q[++top]=s[j]+q[i];
}
puts("-1");
return ;
}

Codeforces Round #407 (Div. 2)的更多相关文章

  1. Codeforces Round #407 (Div. 1) B. Weird journey —— dfs + 图

    题目链接:http://codeforces.com/problemset/problem/788/B B. Weird journey time limit per test 2 seconds m ...

  2. Codeforces Round #407 (Div. 1)

    人傻不会B 写了C正解结果因为数组开小最后RE了 疯狂掉分 AC:A Rank:392 Rating: 2191-92->2099 A. Functions again 题目大意:给定一个长度为 ...

  3. Codeforces Round #407 (Div. 2)A B C 水 暴力 最大子序列和

    A. Anastasia and pebbles time limit per test 1 second memory limit per test 256 megabytes input stan ...

  4. Codeforces Round #407 (Div. 2) D,E

    图论 D. Weird journey time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. Codeforces Round #407 (Div. 2) D. Weird journey(欧拉路)

    D. Weird journey time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  6. 【分类讨论】Codeforces Round #407 (Div. 2) D. Weird journey

    考虑这个二元组中有一者是自环,则必然合法. 考虑这两条边都不是自环,如果它们不相邻,则不合法,否则合法. 坑的情况是,如果它是一张完整的图+一些离散的点,则会有解,不要因为图不连通,就误判成无解. # ...

  7. 【预处理】Codeforces Round #407 (Div. 2) C. Functions again

    考虑枚举每个子串开头的位置,然后答案转化成询问这个位置之后 哪个位置的前缀和 - 这位置的前缀和 最大(当然是已经把绝对值和正负的情况处理好了,可以发现按奇偶,这个序列只有两种情况),只需要预处理这两 ...

  8. 【分类讨论】【set】Codeforces Round #407 (Div. 2) B. Masha and geometric depression

    模拟一下那个过程,直到绝对值超过l,或者出现循环为止. 如果结束之后,绝对值是超过l的,就输出当前写在黑板上的数量. 如果出现循环,则如果写在黑板上的数量非零,则输出inf(注意!如果陷入的循环是一个 ...

  9. 【贪心】Codeforces Round #407 (Div. 2) A. Anastasia and pebbles

    贪心地一个一个尽可能往口袋里放,容易发现和顺序无关. #include<cstdio> #include<iostream> using namespace std; type ...

随机推荐

  1. 从源码角度看LinkedList一些基本操作(jdk1.7)

    介绍 LinkedList是一个双向链表,就像下图展示那样,每个节点有个指向上个元素和一个指向下个元素的指针. 接下来我会对我们经常使用的方法进行介绍,代码如下 @Test public void t ...

  2. LeetCode & Q283-Move Zeroes-Easy

    Array Two Pointers Description: Given an array nums, write a function to move all 0's to the end of ...

  3. Python内置函数(46)——format

    英文文档: format(value[, format_spec]) Convert a value to a "formatted" representation, as con ...

  4. Python内置函数(5)——pow

    英文文档: pow(x, y[, z]) Return x to the power y; if z is present, return x to the power y, modulo z (co ...

  5. 源码解析flask的路由系统

    源码解析flask的路由系统 当我们新建一个flask项目时,pycharm通常已经为项目定义了一个基本路由 @app.route('/') def hello_world(): return 'He ...

  6. 如何将portfolio产品图片上的悬停去掉?

    在Avada主题里,文章和portfolio的分类界面的图片,鼠标移入后都会出现这个东西 那么如何把它去掉,改为直接点击产品图片后进入产品详情页呢? 在theme option里搜索image rol ...

  7. redis入门(06)各种类型的操作命令

    Redis 字符串命令下表列出了常用的 redis 字符串命令:序号 命令及描述1 SET key value 设置指定 key 的值2 GET key 获取指定 key 的值.3 GETRANGE ...

  8. gradle入门(1-5)创建并运行Web应用

    一.使用Gretty运行Web应用 Gretty支持Jetty和Tomcat,它不会被Gradle缺少SLF4J绑定所导致的问题所困扰. 1.配置文件build.gradle buildscript ...

  9. gradle入门(1-4)多项目构建实战

    一.多项目构建 1.多项目构建概念 尽管我们可以仅使用单个组件来创建可工作的应用程序,但有时候更广泛的做法是将应用程序划分为多个更小的模块. 因为这是一个非常普遍的需求,因此每个成熟的构建工具都必须支 ...

  10. jQuery serialize()方法获取不到数据,alert结果为空

    网上查找,问题可能是 id有重复 经排查,没有发现重复id 解决方案 form表单中每个input框都没有name属性,添加name属性即可 若name属性与jQuery的关键字有冲突,也可导致该问题 ...