http://codeforces.com/contest/828

哇这是我打的第一场cf,第一题都wa了无数次,然后第二题差几分钟交 ,第二天一交就AC了内心是崩溃的。果然我还是太菜l....

A. Restaurant Tables
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

In a small restaurant there are a tables for one person and b tables for two persons.

It it known that n groups of people come today, each consisting of one or two people.

If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group.

If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group.

You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.

Input

The first line contains three integers na and b (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ 2·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables.

The second line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 2) — the description of clients in chronological order. If ti is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.

Output

Print the total number of people the restaurant denies service to.

Examples
input
4 1 2
1 2 1 1
output
0
input
4 1 1
1 1 2 1
output
2
Note

In the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served.

In the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients.

题解:直接模拟,当时傻了b--就直接a++,wa了一年,废话太多我。。。上代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn=2e5+5;
int da[maxn],n,a,b;
int main()
{
    int ans=0;int c=0; 
    scanf("%d %d %d",&n,&a,&b);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&da[i]);
    }
    for(int i=0;i<n;i++)
    {
        bool flag=false;
        if(da[i]==1)
        {
            if(a>0)
            {
                a--;
            }
            else if(b>0)
            {
                b--;c++;
            }
            else if(c>0)
            {
                c--;
            }
            else
            {
                ans++;
            }
        }
        else
        {
            if(b>0)
            {
                b--;
            }
            else
            {
                ans+=2;
            } 
        
        }
    }
    cout<<ans<<endl;
    return 0;
 } 
B. Black Square
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarp has a checkered sheet of paper of size n × m. Polycarp painted some of cells with black, the others remained white. Inspired by Malevich's "Black Square", Polycarp wants to paint minimum possible number of white cells with black so that all black cells form a square.

You are to determine the minimum possible number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. The square's side should have positive length.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the sizes of the sheet.

The next n lines contain m letters 'B' or 'W' each — the description of initial cells' colors. If a letter is 'B', then the corresponding cell is painted black, otherwise it is painted white.

Output

Print the minimum number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. If it is impossible, print -1.

Examples
input
5 4
WWWW
WWWB
WWWB
WWBB
WWWW
output
5
input
1 2
BB
output
-1
input
3 3
WWW
WWW
WWW
output
1
Note

In the first example it is needed to paint 5 cells — (2, 2), (2, 3), (3, 2), (3, 3) and (4, 2). Then there will be a square with side equal to three, and the upper left corner in (2, 2).

In the second example all the cells are painted black and form a rectangle, so it's impossible to get a square.

In the third example all cells are colored white, so it's sufficient to color any cell black.

题意:给一个n*m的B,W的图,求画一黑正方形需要的最少墨水。如果不能画输出-1.

题解:找到最下,左,右,上,的点。从而取长的作为边长。以及还有几种特殊情况。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn=100+5;
char map[105][105];
bool vis[105][105];
int di[4][2]={-1,0,0,-1,1,0,0,1};
int n,m,lx=250,rx=-1,uy=250,dy=-1;
struct node
{
    int x,y;
};
 node a1,a2;
int main()
{
    scanf("%d %d",&n,&m);
    getchar();
    for(int i=1;i<=n;i++)
    {
        gets(map[i]+1);
    }
    memset(vis,false,sizeof(vis));
    int tmp1=0,tmp2=0;bool flag=false;
    a1.x=a1.y=250;
    a2.x=a2.y=-1;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(map[i][j]=='B')
            {
            //    cout<<i<<" "<<j<<endl;
            flag=true;
                lx=min(lx,j);
                
                rx=max(rx,j);
                uy=min(uy,i);//cout<<uy<<endl;
                dy=max(dy,i);
            /*    if(i<a1.x||j<a1.y)
                {
                    a1.x=j;a1.y=i;
                }
                if(i>a2.x||j>a2.y)
                {
                    a2.x=j;a2.y=i;
                } */
                tmp2++;
            }
            
        }
    }
    //cout<<lx<<"  "<<rx<<" "<<uy<<" "<<dy<<" "<<a1.x<<" "<<a1.y<<"  "<<a2.x<<" "<<a2.y<<endl;
    //cout<<tmp2<<endl;
    if(!flag)
    {
        printf("1
");return 0;
    }
    int l=max(rx-lx+1,dy-uy+1);
    if(l>n||l>m)
    {
        printf("-1
");return 0;
    }
    //cout<<l<<endl;
    printf("%d
",l*l-tmp2);
    
 }
C. String Reconstruction
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun of him and hid the string s. Ivan preferred making a new string to finding the old one.

Ivan knows some information about the string s. Namely, he remembers, that string ti occurs in string s at least ki times or more, he also remembers exactly ki positions where the string ti occurs in string s: these positions are xi, 1, xi, 2, ..., xi, ki. He remembers n such strings ti.

You are to reconstruct lexicographically minimal string s such that it fits all the information Ivan remembers. Strings ti and string sconsist of small English letters only.

Input

The first line contains single integer n (1 ≤ n ≤ 105) — the number of strings Ivan remembers.

The next n lines contain information about the strings. The i-th of these lines contains non-empty string ti, then positive integer ki, which equal to the number of times the string ti occurs in string s, and then ki distinct positive integers xi, 1, xi, 2, ..., xi, ki in increasing order — positions, in which occurrences of the string ti in the string s start. It is guaranteed that the sum of lengths of strings ti doesn't exceed106, 1 ≤ xi, j ≤ 106, 1 ≤ ki ≤ 106, and the sum of all ki doesn't exceed 106. The strings ti can coincide.

It is guaranteed that the input data is not self-contradictory, and thus at least one answer always exists.

Output

Print lexicographically minimal string that fits all the information Ivan remembers.

Examples
input
3
a 4 1 3 5 7
ab 2 1 5
ca 1 4
output
abacaba
input
1
a 1 3
output
aaa
input
3
ab 1 1
aba 1 3
ab 2 3 5
output
ababab
题意:给n个字符串和位置求最小字典序的的字符串。
题解:直接模拟的话肯定会超时,用结构体储存字符串的位置然后排序一能免去重复的赋值。
#include<iostream>
#include<cstdio>
#include<cstring>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
using namespace std;
const int maxn=2e6+10;
struct p
{
    int k,l,r; 
}tmp; 
bool cmp(const p &a,const p &b) 
{
    if(a.l==b.l)
    return a.r>b.r;    
    else
    return a.l<b.l; 
} 
/*bool cmp(p a,p b) {
   if (a.l == b.l) return a.r > b.r;
   return a.l < b.l;
}*/ 
vector<p>a;
string b; 
vector<string>base; 
int t,k,maxl,n;
int main()
{
    std::ios::sync_with_stdio(false);
    // scanf("%d",&n);
     cin>>n; 
    // getchar(); 
     while(n--)
     {
         cin>>b>>t; 
        base.push_back(b); 
        //scanf("%d",&t);
        for(int i=1;i<=t;i++)
        {
            //scanf("%d",&k); 
            cin>>k; 
            tmp=(p){(int) base.size()-1,k,k+(int)b.size()-1};
            a.push_back(tmp); 
        } 
     } 
     sort(a.begin(),a.end(),cmp);
     b="";
     k=1;
    for(int i=0;i<(int)a.size();i++)
     {
         if(a[i].r<k)continue; 
         while(k<a[i].l) {b+='a';k++;} 
         for(int j=a[i].l;k<=a[i].r;k++)
         {
             b+=base[a[i].k][k-a[i].l];// cout<<"1"<<endl; 
         } 
     }
     cout<<b<<endl; 
     //printf("%s
",a+1); 
}
D. High Load
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Arkady needs your help again! This time he decided to build his own high-speed Internet exchange point. It should consist of n nodes connected with minimum possible number of wires into one network (a wire directly connects two nodes). Exactly k of the nodes should be exit-nodes, that means that each of them should be connected to exactly one other node of the network, while all other nodes should be connected to at least two nodes in order to increase the system stability.

Arkady wants to make the system as fast as possible, so he wants to minimize the maximum distance between two exit-nodes. The distance between two nodes is the number of wires a package needs to go through between those two nodes.

Help Arkady to find such a way to build the network that the distance between the two most distant exit-nodes is as small as possible.

Input

The first line contains two integers n and k (3 ≤ n ≤ 2·105, 2 ≤ k ≤ n - 1) — the total number of nodes and the number of exit-nodes.

Note that it is always possible to build at least one network with n nodes and k exit-nodes within the given constraints.

Output

In the first line print the minimum possible distance between the two most distant exit-nodes. In each of the next n - 1 lines print two integers: the ids of the nodes connected by a wire. The description of each wire should be printed exactly once. You can print wires and wires' ends in arbitrary order. The nodes should be numbered from 1 to n. Exit-nodes can have any ids.

If there are multiple answers, print any of them.

Examples
input
3 2
output
2
1 2
2 3
input
5 3
output
3
1 2
2 3
3 4
3 5
Note

In the first example the only network is shown on the left picture.

In the second example one of optimal networks is shown on the right picture.

Exit-nodes are highlighted.

题意:求n个点的链接方式有k个端点,要使得最长的的路最短。

题解:从一个点往k个方向平均发散,这样能使最长的路最短。

#include<iostream>
#include<cstdio>
#include<cstring>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
using namespace std;
int n,k; 
int main() 
{
    scanf("%d %d",&n,&k);
    int tmp=(n-1)%k; 
    int d=(n-1)/k; 
    if(tmp==0)
    {
        printf("%d
",(n-1)/k*2+1-1); 
    }
    else if(tmp>=2) 
    {
        printf("%d
",(n-1)/k*2+3-1);
    }
    else if(tmp==1)
    {
        printf("%d
",(n-1)/k*2+2-1); 
    } 
    int px=2; 
    for(int i=1;i<=k;i++)
    {
         if(tmp>0)
         {
             for(int j=1;j<=d+1;j++) 
             {
                 if(j==1) 
                 printf("1 %d
",px); 
                 else
                 printf("%d %d
",px-1,px);
                 px++; 
             } 
             tmp--; 
         } 
         else
         {
             for(int j=1;j<=d;j++)
             {
                 if(j==1) 
                 printf("1 %d
",px); 
                 else
                 printf("%d %d
",px-1,px);
                 px++; 
             } 
         } 
    } 
    return 0; 
} 
E. DNA Evolution
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Everyone knows that DNA strands consist of nucleotides. There are four types of nucleotides: "A", "T", "G", "C". A DNA strand is a sequence of nucleotides. Scientists decided to track evolution of a rare species, which DNA strand was string s initially.

Evolution of the species is described as a sequence of changes in the DNA. Every change is a change of some nucleotide, for example, the following change can happen in DNA strand "AAGC": the second nucleotide can change to "T" so that the resulting DNA strand is "ATGC".

Scientists know that some segments of the DNA strand can be affected by some unknown infections. They can represent an infection as a sequence of nucleotides. Scientists are interested if there are any changes caused by some infections. Thus they sometimes want to know the value of impact of some infection to some segment of the DNA. This value is computed as follows:

  • Let the infection be represented as a string e, and let scientists be interested in DNA strand segment starting from position l to position r, inclusive.
  • Prefix of the string eee... (i.e. the string that consists of infinitely many repeats of string e) is written under the string s from position lto position r, inclusive.
  • The value of impact is the number of positions where letter of string s coincided with the letter written under it.

Being a developer, Innokenty is interested in bioinformatics also, so the scientists asked him for help. Innokenty is busy preparing VK Cup, so he decided to delegate the problem to the competitors. Help the scientists!

Input

The first line contains the string s (1 ≤ |s| ≤ 105) that describes the initial DNA strand. It consists only of capital English letters "A", "T", "G" and "C".

The next line contains single integer q (1 ≤ q ≤ 105) — the number of events.

After that, q lines follow, each describes one event. Each of the lines has one of two formats:

  • 1 x c, where x is an integer (1 ≤ x ≤ |s|), and c is a letter "A", "T", "G" or "C", which means that there is a change in the DNA: the nucleotide at position x is now c.
  • 2 l r e, where lr are integers (1 ≤ l ≤ r ≤ |s|), and e is a string of letters "A", "T", "G" and "C" (1 ≤ |e| ≤ 10), which means that scientists are interested in the value of impact of infection e to the segment of DNA strand from position l to position r, inclusive.
Output

For each scientists' query (second type query) print a single integer in a new line — the value of impact of the infection on the DNA.

Examples
input
ATGCATGC
4
2 1 8 ATGC
2 2 6 TTT
1 4 T
2 2 6 TA
output
8
2
4
input
GAGTTGTTAA
6
2 3 4 TATGGTG
1 1 T
1 6 G
2 5 9 AGTAATA
1 10 G
2 2 6 TTGT
output
0
3
1
Note

Consider the first example. In the first query of second type all characters coincide, so the answer is 8. In the second query we compare string "TTTTT..." and the substring "TGCAT". There are two matches. In the third query, after the DNA change, we compare string "TATAT..."' with substring "TGTAT". There are 4 matches.

 题意:给一个字符串,然后可以对字符串可修改,问l~r字符匹配的次数。直接模拟的话会超时。这题用到树状数组,本菜鸡还不会,在网上找半天看树状数组,还是不太懂。只好找大佬问了。

题解:

因为|e|不大于10,所以字符串每一个字符在树状数组上,对于长度(1~10),长度取模后(0~9),字符类型(1~4),位置(1~n)动态维护贡献,然后利用树状数组前缀和性质,求出[l,r]关于e在指定的树状数组中找的每一个字符的贡献总和

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=1e5+5;
int f[11][11][4][maxn];
int n;
char s[maxn];
char c[maxn];
int lowbit(int x)
{
    return x&(-x); 
} 
int ch(char s)
{
    if(s=='A')return 1;
    else if(s=='T')return 2;
    else if(s=='G')return 3;
    else if(s=='C')return 4;
    return -1; 
}  
void Add(int *tr,int x,int d)
{
    for(;x<=n;x+=lowbit(x))
    {
        tr[x]+=d; 
    } 
}
int Sum(int *tr,int x) 
{
    if(x==0)return 0;
    int s=0;
    while(x)
    {
        s+=tr[x];
        x-=lowbit(x); 
    }
    return s; 
} 
int main()
{ 
      scanf("%s",s+1);
    n=strlen(s+1);
    for(int i=1;i<=n;i++)  
        for(int j=1;j<=10;j++)  
            Add(f[j][i%j][ch(s[i])] , i , 1);  
      
    int q,x,d,l,r;  
    scanf("%d",&q); 
     while(q--)
     {
         scanf("%d",&x);
         if(x==1)
         {
             scanf("%d%s",&d,c);
             for(int i=1;i<=10;i++)
             {
                 Add(f[i][d%i][ch(s[d])],d,-1);     
             } 
             s[d]=c[0];
                 for(int i=1;i<=10;i++)
                 Add(f[i][d%i][ch(s[d])],d,1); 
         }
         else
         {
             scanf("%d %d %s",&l,&r,c);
             int len=strlen(c);
             int ans=0;
             for(int i=0;i<len;i++)
             {
                ans+=Sum(f[len][(l+i)%len][ch(c[i])],r)-Sum(f[len][(l+i)%len][ch(c[i])],l-1); 
             } 
            // cout<<ans<<endl; 
            printf("%d
",ans); 
         } 
     }  
     return 0; 
      
} 
 
原文地址:https://www.cnblogs.com/lhclqslove/p/7161693.html