9.3 simulated match

2118: 卡片

时间限制: 1 Sec  内存限制: 128 MB

题目描述

       lrb喜欢玩卡牌。他手上现在有张牌,每张牌的颜色为红绿蓝中的一种。现在他有两种操作。一是可以将两张任意位置的不同色的牌换成一张第三种颜色的牌;二是可以将任意位置的两张相同颜色的牌换成一张该颜色的牌。两个操作后都可以将生成的牌放到任意位置。现在他想知道,最后一张牌可能是什么颜色的。

输入

第一行输入一个n,表示卡牌数量。

       第二行输入一个由’B’,’G’,’R’组成的长度为n的字符串,分别表示卡牌的颜色为蓝色、绿色、红色中的一种。

输出

输出’B’,’G’,’R’中的若干个字母,按字典序输出。代表可能的最后一张牌的颜色。

样例输入

3
GRG

样例输出

BR

well,this problem was easy,but I made the order of the letters wrong.
so...you know...i fell to pass it.
the thinking doesn't need any explication.
here is the code
#include<iostream>
using namespace std;
string w;
char a[3]={'B','G','R'};int a1[3]={0};
int main(){
    int n;cin>>n;cin>>w;
    for(int i=0;i<n;i++)for(int j=0;j<3;j++)if(w[i]==a[j])a1[j]++;
    for(int i=0;i<3;i++)if(a1[i]==n){cout<<a[i];return 0;}
    if(n==2)for(int i=0;i<3;i++)if(!a1[i]){cout<<a[i];return 0;}
    for(int i=0;i<3;i++)
        if(a1[i]==n-1){for(int j=0;j<3;j++)if(j!=i)cout<<a[j];return 0;}
    for(int i=0;i<3;i++)cout<<a[i];return 0;
}

the only problem is that when I used getchar optimization,it went wrong.

maybe this web never allows it...

2119: 取数

时间限制: 1 Sec 内存限制: 128 MB

题目描述

lrb目前有n个数字,他想知道这n个数中选出若干个数,平均数减中位数的最大值是多少。

输入

第一行为n,为数字个数。

第二行有n个数,分别代表n个数字。

输出

输出一个数,为平均数减中位数的最大值,保留两位小数。

样例输入

4
1 2 3 4

样例输出

0.33

提示

对于100%的数据,n<=10^5,0<=a[i]<=10^6

well the code is the best language

#include <cstdio> 
#include <algorithm> 
#define cint    register int  
#define fr(i,a,b)  for(cint i=a;i<=b;i++)
#define INF 1e9 
#define ll long long 
//---------------read optimization----------------------------------- 
#define getchar() (SS==TT&&(TT=(SS=BB)+fread(BB,1,1<<15,stdin),SS==TT)?EOF:*SS++) 
char BB[1<<15],*SS=BB,*TT=BB; 
int read(){cint x=0,f=1;char ch=getchar(); 
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 
    while('0'<=ch&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}return x*f;} 
//---------------optimization above-----------------------------------
using namespace std; 
int n,a[200010];  
ll maxw,maxl,left,right,w1,w2,p[200010];  
double v1,v2,v,maxh=-1; 
double solute(ll w,ll l){return (double)(p[w]-p[w-l-1]+p[n]-p[n-l])/(double)(2*l+1)-(double)a[w];}
//calculate out the average_number  subtract median
int TPM(int i){left=0;right=min(i-1,n-i); 
    while(left+1<=right){//if has not run out the section 
        w1=(right-left)/3+left;w2=right-(right-left)/3; 
        v1=solute(i,w1);v2=solute(i,w2); 
        if(v1>v2)right=w2-1;else left=w1+1;} 
}//Three_Point_Method
void pans(double key){printf("%.2lf
",key);}//out the data
int main(){n=read(); // read the total_sum of the array
    fr(i,1,n)a[i]=read();//read the array
    sort(a+1,a+n+1);//set the array in order
    fr(i,1,n)p[i]=p[i-1]+a[i];//mark each prefix sum
    if(n<=2){pans(0);return 0;} //the special case
    fr(i,1,n){TPM(i);v=solute(i,left);//mark the solution of the_average_number_subtract_median
        if(v>maxh)maxh=v,maxw=i,maxl=left;}//if the solution is the best ,renew it.
    pans(maxh);return 0;//out the ans
} 

2120: 密码

时间限制: 1 Sec 内存限制: 128 MB

题目描述

lrb去柜员机取钱,并输入了他的四位密码。但是这一切都被一个黑帮拍摄了下来。幸好,这一次他的银行卡里并没有剩余任何钱。lrb知道了他的账户被异常登录后十分害怕,然后修改了他的密码。从此以后他为了不被看出密码,他开始“徐晃”。但这一切还是被拍摄了下来,拍摄多次以后,这个黑帮找到了你,希望你在一秒内帮他算出lrb的可能使用的密码数量。

输入

第一行输入一个n,为lrb被拍摄的动作次数。

接下来n行,每行第一个数为t,表示lrb接下来的手指移动次数,接下来为一个长度为t的数字串,表示lrb手指经过的轨迹。lrb手指经过的位置,可能没有按,也有可能按了多次。

输出

输出一行,为可能的密码数量。

样例输入

2
3 123
3 234

样例输出

5

提示

lrb可能用的是2222,2223,2233,2333,3333五种密码

设所有轨迹串的总长度为L。


对于100%的数据,1<=n<=1000,1<=t<=10^4,L<=10^6

the code is the best language...

#include <bits/stdc++.h> 
#define ll long long 
using namespace std; 
#define cint    register int
#define cchar   register char
#define fr(cint,i,a,b)  for(cint i=a;i<=b;i++)
#define fd(i,a,b)  for(cint i=a;i>=b;i--)
//---------------read optimization-----------------------------------
char BB[1<<15],*SS=BB,*TT=BB; 
short inline read(){short x=0;char ch=getchar(); 
    while(ch<'0'||ch>'9')ch=getchar(); 
    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}return x;} 
//---------------optimization above-----------------------------------
int n,cnt; 
//n:just the total_num
//cnt:mark the final solution(just the possible number of password)
short k[1005],ans[5]={0,1,3,3,1};
//k:use to read the move frequency
char *ch[1005],a[5]; 
//a:use to mark the password
//ch:use to read the given trail
struct sth{short las[10];void init(){memset(las,0,sizeof(las));} }*suf[1004],tmp; 
//las's 1 to 9 individually mark the last_appear_site of each number
bool check(int x){int w,fr;//set two variable 
    fr(cint,i,1,n){//make a loop from 1 to n 
        for(w=1,fr=0;w<=x;w++)//initializate fr as 0
        //and give w a loop from 1 to x
            if(suf[i][fr].las[a[w]])fr=suf[i][fr].las[a[w]];
            //if the last_appear_site is real,just renew the fr
            else break; //or just means that the site isn't real,
            //just means that this suppose is wrong just this case can't be real
            //just break
        if(w<=x)return false;//NO SOLUTION case,just return false 
    }return true;//or,this case is real ,just return true
} 
void dfs(int x){ 
    fr(cchar,i,0,9)//make a loop from 0 to 9(type:char)
    if(i!=a[x-1]){//if the number has not been marked
        a[x]=i;//just mark it and then go on
        if(check(x))cnt+=ans[x];//if after check this case is true ,just add the ans
        if(x<4)dfs(x+1);//because the array:ans's section is from 0 to 4
        //so we can only operate when it was inside the section,
        //if x<4,just means that x+1<=4,means satisfy the requirement
        //if satisfy this,just go on to the next operatation
    }} 
//deep search
int main(){n=read();//read the total_num of the trail
    fr(cint,i,1,n)//make a loop from 1 to n
    {
        k[i]=read();//read the move frequency
        ch[i]=new char[k[i]+2];suf[i]=new sth[k[i]+2];//set the memory dynamically
        //one point is very important,the  new_type[size]_operation only support pointer
        scanf("%s",ch[i]+1);tmp.init();//read the trail and initialilzate the letters' array
        fd(j,k[i],0){//make a loop from k[i] down to 0
           suf[i][j]=tmp;//well,set the pointer:suf against tmp
           //this operation just means that send the tmp's address to suf[i][j]
           //afer this operation,you can use suf[i][j].lase[] to replace the tmp.las[]
           if(j)//if j is ont zero just means that it is inside the section
           //just go on
           tmp.las[ch[i][j]-'0']=j;//renew the last_appear_site
        }
    }
    a[0]=-1;dfs(1);//initializate the a[0] as a boundary,and then go to the operate_function
    //!-_-!,the highlight part!!!!
    printf("%d",cnt);return 0;//out the ans
} 
原文地址:https://www.cnblogs.com/muzu/p/7499571.html