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; ...
随机推荐
- sed.exe 在bat中使用时,需要另外起一个文件
今天在windows使用sed.exe时,同一个文件死活不生效,然后换了一个bat,再来调用,就可以了,怀疑跟sed.exe的代码有关.有时间再研究
- 初次使用引用外部js心得
在外部引用自己编辑的js时建立链接写在头部中是会出错的,如下图 错误如下: 这是一个是我初学时遇到的一个算是低级错误吧,看到这个错误,我以为的是我引用的js中编辑的代码是不是哪里写错了,但是看了好多遍 ...
- ES6学习笔记(10)----Set和Map数据结构
参考书<ECMAScript 6入门>http://es6.ruanyifeng.com/ Set和Map数据结构 1.Set 基本用法 Set是一种新的数据结构,它的成员都是唯一 ...
- UVM挑战及概述
UVM的调度也具有其独特的挑战,尤其是在调试的领域.其中的一些挑战如下: 1. Phase的管理:objections and synchronization 2. 线程调试 3. Tracing i ...
- 【数据分析 R语言实战】学习笔记 第六章 参数估计与R实现(下)
6.3两正态总体的区间估计 (1)两个总体的方差已知 在R中编写计算置信区间的函数twosample.ci()如下,输入参数为样本x, y,置信度α和两个样本的标准差. > twosample. ...
- Git理论知识补充
转自: http://www.cnblogs.com/hnrainll/archive/2012/11/13/2768003.html 对于任何一个文件,在 Git 内都只有三种状态:已提交(comm ...
- 使用正则进行HTML页面属性的替换
使用正则表达式拼接富文本框 package com.goboosoft.common.utils; import org.apache.commons.lang3.StringUtils; impor ...
- shift Alt + up(down) copy current line ! ctrl + j show the control # vscode key
shift Alt + up(down) copy current line ! ctrl + j show the control # vscode key
- postman设置环境变量、全局变量
讲postman环境变量设置之前,先讲一个小插曲,环境变量.全局变量的区别在于Globals,只能用一组,而Environmen可以设置多组,所以我更喜欢设置环境变量 1.环境变量-Environme ...
- rfcn讲解博客
http://www.cnblogs.com/lillylin/p/6277094.html ROI pooling操作的输入(对于C+1个类)是k^2*(C+1)*W' *H'(W'和H'是ROI的 ...