Dirt030 code title record


title: title Dirt030 code recording
DATE: 2019-10-06 16:52:45
Updated:
Tags: ACM
---

In order to monitor up his own title, by the way keep the code, the code of the question threw it on the Blog.

Codeforces Round #601 (Div. 2)

A Changing Volume

Portal

answer

First volume of not less than five grid is certainly a priority by +/- 5

Then manually handle it, twice to fourfold volume +/- 2; three grid once the volume +/- 1, +/- 2 once; two and a lattice grid is a

Because forget grammar case, so using a nested if

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
    /*freopen("sample.in", "r", stdin);
    freopen("sample.out", "w", stdout);*/

    int T;
    cin>>T;
    while(T--){
        int a,b;
        cin>>a>>b;
        int ans=0;
        int d=abs(a-b)/5,m=abs(a-b)%5;
        if(m==0) ans=d;
        else if(m==1) ans=d+1;
        else if(m==2) ans=d+1;
        else if(m==3) ans=d+2;
        else if(m==4) ans=d+2;
        cout<<ans<<endl;
    }

    /*fclose(stdin);
    fclose(stdout);*/

    return 0;
}

B Fridge Lockers

Portal

answer

This question was like pan, VP after the game open more inside the limit m≤n. Obviously a refrigerator and at least two other refrigerator connected to ensure that others will not be opened, with m≤n restrictions, only the refrigerator into the ring. Special judge what n == 2 on the line.

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
    /*freopen("sample.in", "r", stdin);
    freopen("sample.out", "w", stdout);*/

    int T;
    cin>>T;
    while(T--){
        int n,m;
        cin>>n>>m;
        int ans=0,tmp;
        for(int i=0;i<n;i++){
            cin>>tmp;
            ans+=tmp;
        }
        ans*=2;
        if(n==2||m<n) cout<<-1<<endl;
        else
        {
            cout<<ans<<endl;
            for(int i=1;i<n;i++)
                cout<<i<<' '<<i+1<<endl;
            cout<<n<<' '<<1<<endl;
        }
    }

    /*fclose(stdin);
    fclose(stdout);*/

    return 0;
}

C League of Leesins

Portal

answer

Pure analog, since the beginning appeared only once, so find out one by one and then push past on the line.

Too lazy to want to write the variable name so the code is very ugly.

#include<bits/stdc++.h>
#define ll long long
using namespace std;
//tga记录每个三元组的三个数字,agt记录每个数字被包含于哪几个三元组
struct tgadata{
    int num,q[3];
}tga[100005],agt[100005];
//枚举寻找同时包含b和c的三元组,然后如果其不含有a,那它就是下一个三元组
int findnxt(int a,int b, int c){
    for(int i=0;i<agt[b].num;i++){
        for(int j=0;j<agt[c].num;j++){
            if(agt[b].q[i]==agt[c].q[j]){
                for(int k=0;k<3;k++){
                    if(tga[agt[b].q[i]].q[k]!=b&&tga[agt[b].q[i]].q[k]!=c){
                        if(tga[agt[b].q[i]].q[k]!=a) return tga[agt[b].q[i]].q[k];
                        break;
                    }
                }
            }
        }
    }
    return 0;
}
int main(){
    /*freopen("sample.in", "r", stdin);
    freopen("sample.out", "w", stdout);*/

    int n;
    cin>>n;
    for(int i=0;i<n-2;i++){
        for(int j=0;j<3;j++){
            cin>>tga[i].q[j];
            agt[tga[i].q[j]].q[agt[tga[i].q[j]].num++]=i;
        }
        tga[i].num=i;
    }
    //这里因为懒得压代码所以直接分开来写的,实际上如果考虑到n>=5的限制,第二和第三个数有更简单的办法找出来
    //我这里针对了不存在的n==3和n==4进行了处理
    int p1;
    for(int i=0;i<n-2;i++){
        if(agt[i].num==1){
            p1=i;
            break;
        }
    }
    int p2;
    for(int i=0;i<3;i++)
    {
        if(tga[agt[p1].q[0]].q[i]!=p1&&agt[tga[agt[p1].q[0]].q[i]].num<=2){
            p2=tga[agt[p1].q[0]].q[i];
            break;
        }
    }
    int p3;
    for(int i=0;i<3;i++)
    {
        if(tga[agt[p1].q[0]].q[i]!=p1&&tga[agt[p1].q[0]].q[i]!=p2){
            p3=tga[agt[p1].q[0]].q[i];
            break;
        }
    }
    cout<<p1;
    for(int i=0;i<n-3;i++){
        int tmp=findnxt(p1,p2,p3);
        p1=p2;
        p2=p3;
        p3=tmp;
        cout<<' '<<p1;
    }
    cout<<' '<<p2<<' '<<p3<<endl;

    /*fclose(stdin);
    fclose(stdout);*/

    return 0;
}

Hello 2020

A New Year and Naming

Portal

answer

Attribution method similar to the Chinese calendar, the y and m are respectively modulo n then corresponds to the name.

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
    /*freopen("sample.in", "r", stdin);
    freopen("sample.out", "w", stdout);*/
    
    int n,m;
    cin>>n>>m;
    string a[23],b[23];
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    for(int i=0;i<m;i++){
        cin>>b[i];
    }
    int q;
    cin>>q;
    int y;
    while(q--){
        cin>>y;
        string ans=a[(y-1)%n]+b[(y-1)%m]+"\n";
        cout<<ans;
    }

    /*fclose(stdin);
    fclose(stdout);*/

    return 0;
}

B New Year and Ascent Sequence

Portal

answer

The sequence is divided into two types, one is the inherent increase in the number of pairs, there is a one in mind; one is the increase in the number of itself does not exist, denoted a 'b.

For the former, and the former arrangement, any arrangement can therefore have a * a viable solution; for the former and the latter arrangement, any arrangement may be, it is a feasible solution a * b; for the and after one kind of an arrangement, a maximum value of the minimum value equal to or less than required to meet before a sequence enumerated in this calculation.

For the calculation of the third type arrangement, considering that we do not care about in the end is who and who arranged only care about how many species can be arranged, so you can keep only the minimum and maximum information for each sequence. Next, the maximum and minimum number of first columns in ascending order, respectively, may be noted the number of columns of the monotone decrease the complexity of the enumeration (violence O (n ^ 2 ^)). For a certain minimum value, we find a just larger than its maximum value, the value is greater than this can be a viable solution; and greater than the minimum of the minimum value of the currently selected, can form a feasible solution to its the maximum value of the maximum value of the inevitable after previously selected. By monotonic, we only need to enumerate the sequence again, reduced complexity of O (n).

Because the game did not take into account the first category feasible solution, leading to mend in time debug the code is very ugly, variable names are recklessly.

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int smin[100005],smax[100005];
int main(){
    /*freopen("sample.in", "r", stdin);
    freopen("sample.out", "w", stdout);*/

    ll n;
    cin>>n;
    //nn为第一种序列,nnn为第二种序列
    ll nn=0,nnn=0;
    for(int j=0;j<n;j++){
        int l;
        cin>>l;
        bool flg=1;
        int minn=1000006,maxx=-1;
        for(int i=0;i<l;i++){
            int tmp;
            cin>>tmp;
            //判断是否自身存在上升数对
            if(flg&&tmp>minn){
                nnn++;
                flg=0;
            }
            minn=min(tmp,minn);
            maxx=max(tmp,maxx); 
        }
        if(flg){
            smin[nn]=minn;
            smax[nn]=maxx;
            nn++;
        }
    };
    //cout<<nn<<' '<<nnn<<endl;
    sort(smin,smin+nn);
    sort(smax,smax+nn);
    //for(int i=0;i<nn;i++) cout<<smax[i]<<' ';cout<<endl;
    //for(int i=0;i<nn;i++) cout<<smin[i]<<' ';cout<<endl;
    
    ll ans=0;   
    //计算第三类排列
    ll p1=0,p2=0;
    while(p1!=nn){
        while(p1!=nn&&p2!=nn&&smin[p1]>=smax[p2]){
            p2++;
        }
        ans+=nn-p2;
        p1++;
    }
    //计算前两类排列
    ans+=nnn*nn*2+nnn*nnn;
    
    cout<<ans<<"\n";

    /*fclose(stdin);
    fclose(stdout);*/

    return 0;
}

C New Year and Permutation

Portal

answer

First of all we can not put ideas on the number of sequences for a particular number of columns how it meets the requirements of the meaning of the questions, but think about how to construct sequences directly meet the meaning of the questions.

Rl is just as max-min, and each number is not the same, it is clear that l and r in the interval must be exactly the number of continuous.

We consider this is the number of consecutive numbers i, then this number has ni + 1 kinds of borrowing, which arrangement there i! Species, and for the remaining ni numbers, which are arranged in a selected this digital section end sections, first, arrayed with (Ni)! species, and then both sides of the card method will be divided into two sections in a selected number, thus also multiplied by (ni + 1 ).

So the final answer is [Sigma n- I. 1 = (I! * (N-I-+. 1) * (n-I-+. 1)!).

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll jc[250004];
int main(){
    /*freopen("sample.in", "r", stdin);
    freopen("sample.out", "w", stdout);*/

    ll n,m;
    cin>>n>>m;
    //预处理阶乘的值
    jc[0]=1;
    for(ll i=1;i<=n;i++){
        jc[i]=jc[i-1]*i%m;
    }
    //计算公式
    ll ans=0;
    for(ll i=1;i<=n;i++){
        //ans=(ans+(((jc[i]*jc[n-i])%m)*(((n-i+1)*(n-i+1)))%m)%m)%m;
        ans=(ans+(jc[i]*jc[n-i+1]%m)*(n-i+1)%m)%m;
    }
    cout<<ans<<endl;
    

    /*fclose(stdin);
    fclose(stdout);*/

    return 0;
}

D New Year and Conference

Portal

answer

There are two kinds of the case of NO, namely site A conflict in the two speeches, no conflict in the B sites; B sites in conflict, no conflict in the A site. So it is necessary to determine the two, but in fact the process is the same judge.

That former case of NO as an example, we first of each presentation start time and end nodes A site in chronological order, while also recording the number into speech. A bit like bracket matching, every time we order enumerate node, with a current record Set what speech talking about, then whenever encountered a time node has a number speech does not appear, it added, it has appeared on hit Set in the deletion of the corresponding speech. This means that, while there Set in a speech in the number of the corresponding lecture in the A site is a conflict.

So how do you deal with these speeches whether the conflict in B site, let's use a segment tree maintenance time for each node how many speeches that whenever Set added a speech, we put it in the B site contains lecture period all nodes plus one time, whenever you delete all minus one. In a long speech because all nodes conflict, then that is all the conflict, so we just ask the current maximum segment tree of all nodes, if a maximum value equal to the number of speech Set, then they must have at least one node time all conflicts in B otherwise, there is not a conflict of all time node. This can solve Set in a speech at the same time is not all conflicts in B site. Modify the interval, the interval tree line inquiry template is the best value, not repeat them.

Since the range of values, the need for discrete data, which is why the words used in the earlier time nodes.

#include<bits/stdc++.h>
using namespace std;
//每场演讲的属性
struct cfdata{
    int sa,ea,sb,eb;
}cf[100005];
//演讲的开始和结束的时间节点和对应的演讲序号
pair<int,int> cfina[200005],cfinb[200005];
//按照时间顺序排序
bool cmp(pair<int,int> a,pair<int,int> b){
    return a.second<b.second;
}
//离散化
vector<int> trans;
int gettrans(int x){
    return lower_bound(trans.begin(),trans.end(),x)-trans.begin()+1;
}
//线段树,因为询问全局最大值就是询问f[1],所以没有query()
//而初始就是空树,所以也没有build()
int f[2001005],tag[2001005];
void downtag(int x){
    if(tag[x]==0) return;
    tag[x*2]+=tag[x];
    f[x*2]+=tag[x];
    tag[x*2+1]+=tag[x];
    f[x*2+1]+=tag[x];
    tag[x]=0;
}
void modify(int x,int l,int r,int ll,int rr,int d){
    if(ll<=l&&r<=rr){
        f[x]+=d;
        tag[x]+=d;
    }
    else{
        downtag(x);
        int mid=(l+r)/2;
        if(ll<=mid) modify(x*2,l,mid,ll,rr,d);
        if(rr>=mid+1) modify(x*2+1,mid+1,r,ll,rr,d);
        f[x]=max(f[x*2],f[x*2+1]);
    }
}
int main(){
    /*freopen("sample.in", "r", stdin);
    freopen("sample.out", "w", stdout);*/

    int n;
    cin>>n;
    //输入数据并复制到对应的记录表中
    for(int i=0;i<n;i++){
        cin>>cf[i].sa>>cf[i].ea>>cf[i].sb>>cf[i].eb;
        trans.push_back(cf[i].sa);
        trans.push_back(cf[i].ea);
        trans.push_back(cf[i].sb);
        trans.push_back(cf[i].eb);
        cfina[i]=make_pair(i,cf[i].sa);
        cfina[n+i]=make_pair(i,cf[i].ea);
        cfinb[i]=make_pair(i,cf[i].sb);
        cfinb[n+i]=make_pair(i,cf[i].eb);
    }
    //离散化
    sort(trans.begin(),trans.end());
    trans.erase(unique(trans.begin(),trans.end()),trans.end());
    //按时间顺序排列
    sort(cfina,cfina+n*2,cmp);
    sort(cfinb,cfinb+n*2,cmp);
    //for(int i=0;i<n*2;i++) cout<<cfina[i].first<<' ';cout<<endl;
    //记录当前时间节点有哪些演讲
    set<int> cfinnow;
    //build()
    memset(f,0,sizeof(f));
    memset(tag,0,sizeof(tag));
    for(int i=0;i<n*2;i++){
        //判断当前时间节点是演讲的开始节点还是结束节点
        //如果是结束节点,那么对应的演讲序号应该已在Set中存在,反之亦然
        if(cfinnow.find(cfina[i].first)!=cfinnow.end()){
            //在A场地和B场地都删除当前演讲
            cfinnow.erase(cfina[i].first);
            modify(1,1,trans.size(),gettrans(cf[cfina[i].first].sb),gettrans(cf[cfina[i].first].eb),-1);
        }else
        {
            //在A场地和B场地均加入当前演讲
            cfinnow.insert(cfina[i].first);
            modify(1,1,trans.size(),gettrans(cf[cfina[i].first].sb),gettrans(cf[cfina[i].first].eb),1);
            //判断当前所有演讲是否在两个场地均冲突
            if(f[1]!=cfinnow.size()){
                cout<<"NO"<<endl;
                return 0;   
            }
        }
    }
    //再做一次B场地冲突是不是A场地也冲突
    cfinnow.clear();
    memset(f,0,sizeof(f));
    memset(tag,0,sizeof(tag));
    for(int i=0;i<n*2;i++){
        if(cfinnow.find(cfinb[i].first)!=cfinnow.end()){
            cfinnow.erase(cfinb[i].first);
            modify(1,1,trans.size(),gettrans(cf[cfinb[i].first].sa),gettrans(cf[cfinb[i].first].ea),-1);
        }else
        {
            cfinnow.insert(cfinb[i].first);
            modify(1,1,trans.size(),gettrans(cf[cfinb[i].first].sa),gettrans(cf[cfinb[i].first].ea),1);
            if(f[1]!=cfinnow.size()){
                cout<<"NO"<<endl;
                return 0;   
            }
        }
    }
    cout<<"YES"<<endl;

    /*fclose(stdin);
    fclose(stdout);*/

    return 0;
}

Codeforces Round #590 (Div. 3)

D Distinct Characters Queries

Portal

answer

With 26 BIT maintain it.

However, when the race to the title B autistic whole the (cycle written i--, the results could not find where is wrong), and finally flicker saying that we do no use the tree line.

Replenishment time also memset () parameter order wrong, and checked for a long time (VSCode me to develop a five, times and parameters can not remember).

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int f[100005][30];
int lowbit(int x){
    return x&(-x); 
}
void modify(int x,int c,int d,int len){
    while(x<=len){
        f[x][c]+=d;
        x+=lowbit(x);
    }
    return;
}
void query(int x,int *tf){
    while(x!=0){
        
        for(int i=0;i<26;i++){
            tf[i]+=f[x][i];
        }
        x-=lowbit(x);
    }
    return;
}
int main() {
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    
    char s[100005];
    cin>>s;
    int len=strlen(s);
    memset(f,0,sizeof(f));
    for(int i=0;i<len;i++){
        modify(i+1,s[i]-'a',1,len);
    }
    int n;
    cin>>n;
    int t,p,l,r;
    char ch;
    int tfl[30],tfr[30];
    int ans;
    for(int i=0;i<n;i++){
        /*cout<<s<<endl;
        for(int k=1;k<=len;k++){
            cout<<k<<' '<<lowbit(k)<<endl;
            for(int j=0;j<26;j++){
                cout<<f[k][j]<<" ";
            }
            cout<<endl;
        }*/
        cin>>t;
        if(t==1){
            cin>>p>>ch;
            modify(p,s[p-1]-'a',-1,len);
            s[p-1]=ch;
            modify(p,s[p-1]-'a',1,len);
        }
        else{
            cin>>l>>r;
            memset(tfl,0,sizeof(tfl));
            query(l-1,tfl);
            memset(tfr,0,sizeof(tfr));
            query(r,tfr);
            ans=0;
            /*for(int j=0;j<26;j++){
                cout<<tfl[j]<<' ';
            }
            cout<<endl;
            for(int j=0;j<26;j++){
                cout<<tfr[j]<<' ';
            }
            cout<<endl;*/
            for(int j=0;j<26;j++){
                if(tfl[j]!=tfr[j]){
                    ans++;
                }
            }
            cout<<ans<<endl;
        }
    }

    //fclose(stdin);
    //fclose(stdout);
}

Guess you like

Origin www.cnblogs.com/Dirt030/p/12178011.html