codeforces #301 div2
A:简单题
每次判断向上转快,还是向下转快即可
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#define N 10005
#define ll long long
char s1[N] , s2[N]; int main()
{
// freopen("a.in" , "r" , stdin);
int n;
while(~scanf("%d" , &n))
{
scanf("%s%s" , s1 , s2);
int ans=;
for(int i= ; i<n ; i++){
int a=s1[i]-'';
int b=s2[i]-'';
if(a>b){
int t=a;
a=b;
b=t;
}
ans += min(b-a , +a-b);
}
printf("%d\n" , ans);
}
return ;
}
B:贪心
先判断所给的数中能否已经保证中位数大于y,不能的话,添加尽可能少的y使其满足中位数为y,剩下的值全定为1,判断总和是否超过x
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#define N 10005
#define ll long long
int n,k,p,x,y;
int a[N];
int main()
{
// freopen("a.in" , "r" , stdin);
int n;
while(~scanf("%d%d%d%d%d" , &n,&k,&p,&x,&y))
{
int pos = (n+)/;
int cnt1 = , cnt2=;
int sum=;
for(int i= ; i<=k ; i++){
scanf("%d" , &a[i]);
if(a[i]>=y) cnt1++;
else cnt2++;
sum+=a[i];
} if(cnt1>n-pos){
for(int i=k+ ; i<=n ; i++) a[i]=,sum++;
if(sum<=x){
for(int i=k+ ; i<=n ; i++){
if(i<n) printf("%d " , a[i]);
else printf("%d\n" , a[i]);
}
}
else{
puts("-1");
}
}else{
bool flag=true;
if(pos-cnt1>n-k) flag=false;
else{
int i;
for(i=k+ ; i<=k+pos-cnt1 ; i++){
a[i] = y;
sum+=a[i];
}
for(;i<=n;i++){
a[i]=;
sum+=a[i];
}
if(sum>x) flag=false;
}
if(flag){
for(int i=k+ ; i<=n ; i++){
if(i<n) printf("%d " , a[i]);
else printf("%d\n" , a[i]);
}
}
else puts("-1");
}
}
return ;
}
C:bfs
从起点bfs,判断能否有两条路径到达终点即可
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#define N 1005
#define ll long long
int n , m;
int dir[][] = {{,},{-,},{,},{,-}}; struct Point{
int x,y;
Point(int x= , int y=):x(x),y(y){}
}p[N][N];
int mat[N][N];
char s[N][N];
Point st , en;
queue<Point> q; bool ok(int x , int y)
{
return x> && x<=n && y> && y<=m;
} bool bfs()
{
while(!q.empty()) q.pop();
q.push(st);
while(!q.empty())
{
Point u = q.front();
q.pop();
for(int i= ; i< ; i++){
int xx = u.x+dir[i][];
int yy = u.y+dir[i][];
if(!ok(xx,yy)) continue;
if(mat[xx][yy]==){
mat[xx][yy]=-;
// cout<<"in: "<<u.x<<" "<<u.y<<" to: "<<xx<<" "<<yy<<endl;
q.push(Point(xx,yy));
}
else if(mat[xx][yy]==- && xx==en.x && yy==en.y){
// cout<<"in: "<<u.x<<" "<<u.y<<endl;
return true;
}
}
}
return false;
} int main()
{
// freopen("a.in" , "r" , stdin); while(~scanf("%d%d" , &n,&m))
{
for(int i= ; i<=n ; i++)
scanf("%s" , s[i]+);
for(int i= ; i<=n ; i++){
for(int j= ; j<=m ; j++){
if(s[i][j] == '.'){
mat[i][j]=;
}
else mat[i][j]=-;
}
} scanf("%d%d" , &st.x , &st.y);
scanf("%d%d" , &en.x , &en.y); printf("%s\n" , bfs()?"YES":"NO");
}
return ;
}
D:概率DP
dp[i][j][k] 表示剩余i个rock,j个siccsors,k个paper时的概率
初始dp[r][q][s]=1
状态转移
dp[i][j][k]+=dp[i+1][j][k]*(1.0*(i+1)*k/((i+1)*j+(i+1)*k+k*j));
dp[i][j][k]+=dp[i][j+1][k]*(1.0*(j+1)*i/(i*(j+1)+i*k+(j+1)*k));
dp[i][j][k]+=dp[i][j][k+1]*(1.0*(k+1)*j/(i*j+i*(k+1)+j*(k+1)));
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#define N 210
#define ll long long double dp[N][N][N];
int r,s,q; void solve()
{
memset(dp,,sizeof(dp));
dp[r][s][q]=1.0;
for(int i=r ; i>= ; i--){
for(int j=s ; j>= ; j--){
for(int k=q ; k>= ; k--){
if(i==r && j==s && k==q) continue;
dp[i][j][k] = ;
if(k) dp[i][j][k]+=dp[i+][j][k]*(1.0*(i+)*k/((i+)*j+(i+)*k+k*j));
if(i) dp[i][j][k]+=dp[i][j+][k]*(1.0*(j+)*i/(i*(j+)+i*k+(j+)*k));
if(j) dp[i][j][k]+=dp[i][j][k+]*(1.0*(k+)*j/(i*j+i*(k+)+j*(k+)));
// cout<<i<<" "<<j<<" "<<k<<" "<<dp[i][j][k]<<endl;
}
}
}
} int main()
{
// freopen("a.in" , "r" , stdin); while(~scanf("%d%d%d" , &r,&s,&q))
{
solve();
double ans1= , ans2= , ans3=;
for(int i= ; i<=r ; i++) ans1+=dp[i][][];
for(int i= ; i<=s ; i++) ans2+=dp[][i][];
for(int i= ; i<=q ; i++) ans3+=dp[][][i];
printf("%.11f %.11f %.11f\n" , ans1 , ans2 , ans3);
}
return ;
}
E:线段树
将点离散化后保存到线段树上,那么最多有200000个点
逐个添加,判断离散化的点之间可以形成多少对
在计算每个离散化的点和其他非离散化的区间内的点形成了多少对
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 200010
#define ls o<<1
#define rs o<<1|1
#define define_m int m=(l+r)>>1
#define ll long long
int a[N*] , k , p[N] , q[N] , val[N*];
ll sum[N<<];
ll ans=; int getIndex(int key)
{
return lower_bound(a , a+k , key)-a;
} void push_up(int o)
{
sum[o] = sum[ls]+sum[rs];
} void update(int o , int l , int r , int pos)
{
if(l==r && l==pos){
sum[o]++;
return ;
}
define_m;
if(m>=pos) update(ls , l , m , pos);
else update(rs , m+ , r , pos);
push_up(o);
} int query(int o , int l , int r , int s , int t)
{
if(l>r) return ;
if(l>=s && r<=t){
return sum[o];
}
int ans=;
define_m;
if(m>=s) ans+=query(ls , l , m , s , t);
if(m<t) ans+=query(rs , m+ , r , s , t);
return ans;
} int main()
{
// freopen("a.in" , "r" , stdin);
int n;
while(~scanf("%d" , &n))
{
k=;
for(int i= ; i<n ; i++){
scanf("%d%d" , &p[i] , &q[i]);
a[k++]=p[i] , a[k++]=q[i];
}
sort(a , a+k);
k = unique(a , a+k)-a;
for(int i= ; i<n ; i++){
int index = getIndex(p[i]);
val[index] = p[i];
p[i] = index;
index = getIndex(q[i]);
val[index] = q[i];
q[i]=index;
}
for(int i= ; i<n ; i++) swap(val[p[i]] , val[q[i]]);
ans = ;
memset(sum , , sizeof(sum));
for(int i= ; i<k ; i++){
int index = getIndex(val[i]);
ans += (ll)query( , , k- , index+ , k-);
update( , , k- , index);
}
//判断当前第i个点和所有非离散化区间能形成的匹配
for(int i= ; i<k ; i++){
ans += (ll)abs(val[i]-val[getIndex(val[i])]) - (ll)abs(i-getIndex(val[i]));
}
cout<<ans<<endl;
}
return ;
}
codeforces #301 div2的更多相关文章
- Codeforces #180 div2 C Parity Game
// Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)
Problem Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)
Problem Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...
- 【Codeforces #312 div2 A】Lala Land and Apple Trees
# [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...
- Codeforces #263 div2 解题报告
比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注冊的时候非常想好好的做一下,可是网上喝了个小酒之后.也就迷迷糊糊地看了题目,做了几题.一觉醒来发现r ...
- codeforces #round363 div2.C-Vacations (DP)
题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...
- codeforces round367 div2.C (DP)
题目链接:http://codeforces.com/contest/706/problem/C #include<bits/stdc++.h> using namespace std; ...
随机推荐
- 用户名密码登录小程序及input与raw_input区别。
一.此次程序需要实现: 1.设定固定的用户名密码 2.用户名密码输入正确打印登录正确信息 3.仅仅运行三次登录 二.本次使用的python版本为: Windows下版本号: C:\Users\dais ...
- springboot项目启动问题
在调试项目的时候有遇到这样一个问题: 项目启动后访问不通,编译没有任何问题,启动也没有报错,日志在打,但是访问不通.而且之前一直可以正常访问,在没改任何代码的情况下不能访问了. 尝试很多次偶然发现,点 ...
- struct和union
struct的小秘密 C语言中的struct可以看做变量的集合,struct的问题: 空结构体占用多大内存? 例子1:空结构体的大小 #include<stdio.h> struct ST ...
- EventBus 报“Subscriber class already registered to event class”错误
这句子的话意思也很容易理解,“接收者类已经被注册为事件类了”. 之前我是这么写: 事件注册是写在onStart()里面的 @Override protected void onStart() { su ...
- Understanding Scroll Views 深入理解 scroll view 读书笔记
Understanding Scroll Views 深入理解 scroll view 读书笔记 It may be hard to believe, but a UIScrollView is ...
- IOS文件下载
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, ...
- IE8提速经验
给人写了个web程序,其中detail页要加载不少东西,所以耗时略长.因为bootstrap的原因,我要求用户使用chrome; 而chrome出了名的快,所以也基本没觉得什么. 后来用户因为别的原因 ...
- SqlServer 2008 创建测试数据
包含要点: 数据库的循环 . insert select 句式 . 随机数(rand()函数).绝对值(abs()函数) ) ) DECLARE @randomvalue float SET @s ...
- 洛谷 P1021 邮票面值设计
题目描述 给定一个信封,最多只允许粘贴N张邮票,计算在给定K(N+K≤15)种邮票的情况下(假定所有的邮票数量都足够),如何设计邮票的面值,能得到最大值MAX,使在1-MAX之间的每一个邮资值都能得到 ...
- 关于sigleton模式
单例模式的要点有三个:一是某个类只能有一个实例:二是它必须自行创建这个实例:三是它必须自行向整个系统提供这个实例. 从具体实现角度来说,就是以下三点:一是单例模式的类只提供私有的构造函数,二是类定义中 ...