CodeForces 570B,C

来源:互联网 发布:linux tcp连接日志 编辑:程序博客网 时间:2024/05/01 13:46

CodeForces 570B

题意:给定n和m,然后再(1-n)中随机取出c,求一个m使得  的概率最大,概率一样时输出最小的m。


思路:只需要看1到m-1和m+1和n的最大的那一边就可以了,坑是n=1的情况和n为奇数m为n/2+1的情况要特判。


code:

#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>#include <cstring>#include <sstream>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <bitset>using namespace std;typedef long long ll;typedef unsigned long long ull;typedef long double ld;const int INF=0x3fffffff;const int inf=-INF;const int N=1e5+5;const int M=2005;const int mod=1000000007;const double pi=acos(-1.0);#define cls(x,c) memset(x,c,sizeof(x))#define cpy(x,a) memcpy(x,a,sizeof(a))#define ft(i,s,n) for (int i=s;i<=n;i++)#define frt(i,s,n) for (int i=s;i>=n;i--)#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define lrt  rt<<1#define rrt  rt<<1|1#define middle int m=(r+l)>>1#define lowbit(x) (x&-x)#define pii pair<int,int>#define mk make_pair#define IN freopen("in.txt","r",stdin);#define OUT freopen("out.txt","w",stdout);int main(){    int n,m;    scanf("%d %d",&n,&m);    if (n==1) puts("1");    else {        if (m-1>n-m) printf("%d\n",m-1);        else if (n%2==1&&m==n/2+1) printf("%d\n",m-1);        else printf("%d\n",m+1);    }}



CodeForces 570C

题意:给定一个n的字符的字符串,然后有m个修改,对于每次修改,询问字符串中有连续。。的个数。


思路:一开始先统计一下个数,然后对于每次修改,最多可修改2个个数。然后分情况讨论就可以。


code:

#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>#include <cstring>#include <sstream>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <bitset>using namespace std;typedef long long ll;typedef unsigned long long ull;typedef long double ld;const int INF=0x3fffffff;const int inf=-INF;const int N=3e5+5;const int M=2005;const int mod=1000000007;const double pi=acos(-1.0);#define cls(x,c) memset(x,c,sizeof(x))#define cpy(x,a) memcpy(x,a,sizeof(a))#define ft(i,s,n) for (int i=s;i<=n;i++)#define frt(i,s,n) for (int i=s;i>=n;i--)#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define lrt  rt<<1#define rrt  rt<<1|1#define middle int m=(r+l)>>1#define lowbit(x) (x&-x)#define pii pair<int,int>#define mk make_pair#define IN freopen("in.txt","r",stdin);#define OUT freopen("out.txt","w",stdout);char s[N];int sol(int p,int c){    if (c=='.'&&s[p]!='.'){        s[p]=c;        if (s[p-1]=='.'&&s[p+1]=='.') return 2;        if (s[p-1]=='.'||s[p+1]=='.') return 1;        return 0;    }    if (c!='.'&&s[p]=='.'){        s[p]=c;        if (s[p-1]=='.'&&s[p+1]=='.') return -2;        if (s[p-1]=='.'||s[p+1]=='.') return -1;        return 0;    }    return 0;}int main(){    int n,k,p,t=0;    scanf("%d %d",&n,&k);    scanf("%s",s+1);s[0]='#';    ft(i,1,n) if(s[i]=='.'&&s[i-1]=='.') t++;    ft(i,1,k){        char ch[2];        scanf("%d %s",&p,&ch);        t+=sol(p,ch[0]);        printf("%d\n",t);    }}



0 0
原创粉丝点击