Codeforces Round #407 (Div. 2)
来自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)的更多相关文章
- 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 ...
- Codeforces Round #407 (Div. 1)
人傻不会B 写了C正解结果因为数组开小最后RE了 疯狂掉分 AC:A Rank:392 Rating: 2191-92->2099 A. Functions again 题目大意:给定一个长度为 ...
- 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 ...
- Codeforces Round #407 (Div. 2) D,E
图论 D. Weird journey time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 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 ...
- 【分类讨论】Codeforces Round #407 (Div. 2) D. Weird journey
考虑这个二元组中有一者是自环,则必然合法. 考虑这两条边都不是自环,如果它们不相邻,则不合法,否则合法. 坑的情况是,如果它是一张完整的图+一些离散的点,则会有解,不要因为图不连通,就误判成无解. # ...
- 【预处理】Codeforces Round #407 (Div. 2) C. Functions again
考虑枚举每个子串开头的位置,然后答案转化成询问这个位置之后 哪个位置的前缀和 - 这位置的前缀和 最大(当然是已经把绝对值和正负的情况处理好了,可以发现按奇偶,这个序列只有两种情况),只需要预处理这两 ...
- 【分类讨论】【set】Codeforces Round #407 (Div. 2) B. Masha and geometric depression
模拟一下那个过程,直到绝对值超过l,或者出现循环为止. 如果结束之后,绝对值是超过l的,就输出当前写在黑板上的数量. 如果出现循环,则如果写在黑板上的数量非零,则输出inf(注意!如果陷入的循环是一个 ...
- 【贪心】Codeforces Round #407 (Div. 2) A. Anastasia and pebbles
贪心地一个一个尽可能往口袋里放,容易发现和顺序无关. #include<cstdio> #include<iostream> using namespace std; type ...
随机推荐
- 日志 --BUG记录
2014-12-15日 在做520wawa的免费推广 部署web应用时 错把path设置为"/*",导致启动tomcat时,导致错误 <Context path=&quo ...
- CNN中的padding
在使用TF搭建CNN的过程中,卷积的操作如下 convolution = tf.nn.conv2d(X, filters, strides=[1,2,2,1], padding="SAME& ...
- V7000数据恢复(存储文件系统损坏)案例_北亚数据恢复
我们今天介绍的数据恢复案例是一起 v7000存储文件系统损坏导致的数据丢失,首先简单介绍一下设备基本情况:发生文件系统损坏的是一台挂载在Windows2003服务器上的v7000存储,划分了一个容量为 ...
- es6+react.js组件入门初探
React是一个用于构建用户见面的javascript库. React主要用于构建UI,许多人认为React是MVC中的V(视图) React起源于Facebook的内部项目,用来架设Instagra ...
- c# 运算符:? ,??
参考微软帮助 1 ? 空值条件运算符,用于在执行成员访问 (?.) 或索引 (?[) 操作之前,测试是否存在 NULL. // ? 空值条件运算符 string str = null; Conso ...
- OO第一次总结
第一次作业: 第一次作业的指导书发下来之后我按着上面的步骤一步一步的做了之后发现项目拉下来了,怎么开始码代码呢...然后在舍友的帮助下才知道怎么建包建类,然后对Java的语法又不是很了解,于是就先把C ...
- 不允许用(a+b)/2这种方式求两个数的均值;如下程序在Linux和32位集成开发环境中运行
#define MAX(a,b) ((a)>(b)?(a):(b)) #include<stdio.h> int main() { int a = 10; int b = 20; i ...
- 【问题解决】jhipster-registry-master空白页
问题概述: 刚从github拉下来的jhipster-registry-master直接运行,访问http://localhost:8761会发现会空白页,但是网页的title显示正常,本文目的是解决 ...
- leetcode算法: Keyboard Row
Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...
- Python生成随机验证码
Python生成随机验证码,需要使用PIL模块. 安装: pip3 install pillow 基本使用 1.创建图片 from PIL import Image img = Image.new(m ...