来自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. Beta冲刺Day3

    项目进展 李明皇 今天解决的进度 完善了程序的运行逻辑(消息提示框等) 明天安排 前后端联动调试 林翔 今天解决的进度 向微信官方申请登录验证session以维护登录态 明天安排 继续完成维护登录态 ...

  2. 清华集训2015 V

    #164. [清华集训2015]V http://uoj.ac/problem/164 统计 描述 提交 自定义测试 Picks博士观察完金星凌日后,设计了一个复杂的电阻器.为了简化题目,题目中的常数 ...

  3. 动手写IL到Lua的翻译器——准备

    文章里的代码粘过来的时候格式有点问题,原因是一开始文章是在订阅号上写的(gamedev101,文末有二维码),不知道为啥贴过来就没了格式,还要手动删行号,就没搞了. 介绍下问题背景: 小说君正在参与的 ...

  4. jQuery 写的textarea输入字数限制

    //先判断浏览器是不是万恶的IE        var bind_name = 'input';//默认事件        if (navigator.userAgent.indexOf(" ...

  5. php_类的定义

    此文章为原创见解,例子各方面也是东拼西凑.如果有错请留言.谢谢 在面向对象的思维中提出了两个概念,类和对象. 类是对某一类实物的抽象描述,而对象用于表示现实中该类事物的个体, 例子:老虎是父类,东北虎 ...

  6. Python之旅_第一章Python入门

    一.编程语言分类 1.机器语言:即计算机能听懂的二进制语言,0000 0001,直接操控硬件: 2.汇编语言:简写的英文标识符代替二进制语言,本质同样是直接操控硬件: 3.高级语言:用更贴近人类的语言 ...

  7. ASP.NET CORE系列【三】使用Entity Framework Core进行增删改查

    身份验证 以前我们熟悉的web.config中配置的form验证,现在没有了.我们来看看在Core里面如何配置: 首先需要NuGet安装一个包:Microsoft.AspNetCore.Authent ...

  8. django关闭调试信息,打开内置错误视图

    1 内置错误视图 Django内置处理HTTP错误的视图,主要错误及视图包括: 404错误:page not found视图 500错误:server error视图 400错误:bad reques ...

  9. 谈谈自己的理解:python中闭包,闭包的实质

    闭包这个概念好难理解,身边朋友们好多都稀里糊涂的,稀里糊涂的林老冷希望写下这篇文章能够对稀里糊涂的伙伴们有一些帮助~ 请大家跟我理解一下,如果在一个函数的内部定义了另一个函数,外部的我们叫他外函数,内 ...

  10. uvalive 3602 DNA Consensus String

    https://vjudge.net/problem/UVALive-3602 题意: 给定m个长度均为n的DNA序列,求一个DNA序列,使得它到所有的DNA序列的汉明距离最短,若有多个解则输出字典序 ...