问题 C: Criss-Cross Cables

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

提交 状态 题面

题目描述

As a participant in the BAPC (Bizarrely Awful Parties Competition) you are preparing for your next show. Now,you do not know anything about music, so you rip off someone else’s playlist and decide not to worry about that any more. What you do worry about, though, is the aesthetics of your set-up: if it looks too simple, people will be unimpressed and they might figure out that you are actually a worthless DJ.

It doesn’t take you long to come up with a correct and fast solution to this problem. You add a long strip with a couple of useless ports, and add some useless cables between these ports. Each of these cables connects two ports, and these special ports can be used more than once. Everyone looking at the massive tangle of wires will surely be in awe of your awesome DJ skills.

However, you do not want to connect the same two ports twice directly. If someone notices this, then they will immediately see that you are a fraud!

You’ve made a large strip, with the ports in certain fixed places, and you’ve found a set of cables with certain lengths that you find aesthetically pleasing. When you start trying to connect the cables, you run into another problem. If the cables are too short, you cannot use them to connect the ports! So you ask yourself the question whether you’re able to fit all of the cords onto the strip or not. If not, the aesthetics are ruined, and you’ll have to start all over again.

输入

The first line has 2 ≤ n ≤ 5 · 105 and 1 ≤ m ≤ 5 · 105, the number of ports on the strip and the number of wires.

• The second line has integers 0 ≤ x1 < · · · < xn ≤ 109, the positions of the n sockets.

• The third line has m integers l1, . . . , lm, the lengths of the wires, with 1 ≤ li ≤ 109.

输出

Print yes if it is possible to plug in all the wires, or no if this is not possible.

样例输入

4 4

0 2 3 7

1 3 3 7

样例输出

yes

#pragma GCC optimize(3,"Ofast","inline")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
#include <vector>
#include <map>
#include <cmath>
#include <stack>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1010;
const int MAXN = 1000010;
const int Hash = 13131;
const int mod =1e9+7;
ull base[MAXN],h[MAXN];
struct node
{
    int l;
    int r;
    int val;
    bool operator < (const node &x)const{
        return val>x.val;
    }
    node (int a,int b,int n){
        l=a,r=b,val=n;
    }
};
char str[MAXN];
int x[MAXN];
priority_queue<node>len;
priority_queue<int,vector<int>,greater<int> >lenans;
int main()
{
    int n,m;
    scanf("%d %d",&n,&m);
    if(m>n*(n-1)/2){
        cout<<"no"<<endl;
        return 0;
    }
    //cout<<"start"<<endl;
    for(int i=0;i<n;i++){
        scanf("%d",&x[i]);
    }
    int ttmp;
    for(int i=0;i<m;i++){
        scanf("%d",&ttmp);
        lenans.push(ttmp);
    }
    for(int i=1;i<n;i++){
        node newnode(i-1,i,x[i]-x[i-1]);
        len.push(newnode);
    }
    bool flag=true;
    while(!lenans.empty()){
        //cout<<lenans.top()<<" "<<lenans.size()<<endl;
        node tmp=len.top();
        len.pop();
 
        if(lenans.top()<tmp.val){
            flag=false;
            break;
        }
        lenans.pop();
        if(tmp.r+1<n){
            node newnode(tmp.l,(tmp.r)+1,x[tmp.r+1]-x[tmp.l]);
            len.push(newnode);
        }
 
    }
    if(flag)cout<<"yes"<<endl;
    else cout<<"no"<<endl;
    return 0;
}

问题 E: Eating Everything Efficiently

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

提交 状态 题面

题目描述

Margriet A. is in pizza heaven! She has bought a one-day access pass to Pizza World. Pizza World is a food festival, where all stands have their own special type of pizza. Margriet would really like to try many different types of pizza, but she thinks that she can only eat two pizzas in total. Therefore, she has come up with a cunning plan: at each stall she visits she decides whether she wants to buy this pizza or not. At the first stall where she decides to make a purchase, she will buy and eat exactly one pizza.

At the second one, she will buy and eat half a pizza, and at the third she will eat one quarter of a pizza, etc.. Therefore, at the kth stall where she decides to buy some pizza, she will eat part of a pizza. This way she makes sure that she will never get full!

In order to ensure that the flow of people in the park is adequate, the pizza stalls are connected by one-way paths, and to make sure that everyone will leave the festival, it is made impossible to visit a pizza stall more than once. However, every stall can be reached from the stall at the entrance, which is the stall with number 0.

Of course, Margriet has her own taste: she will like some pizzas more than others. Eating pizza from a stall will give her a certain amount of satisfaction which is equal to Margriet’s personal stall satisfaction number multiplied by the fraction of a whole pizza she eats there.

Her total satisfaction is the sum of satisfactions of every stall she visits. Can you help Margriet plot a route between the pizza stalls that satisfies her the most?

输入

• Two integers 1 ≤ n ≤ 5 · 105 and 0 ≤ m ≤ 5 · 105, the number of pizza stalls and the number of one way connections.

• The second line has n integers c0, . . . , cn−1, 0 ≤ ci ≤ 109, the amount of enjoyment Margriet gets from eating one pizza at stall i.

• The next m lines each contain 2 integers 0 ≤ s < n and 0 ≤ t < n, ndicating a one way path from stall s to stall t. No connection will appear twice in the input.

输出

Print the maximal amount of enjoyment Margriet can reach at the pizza fair. Your answer will be considered correct if it differs from the actual answer by at most 10−6 absolutely or relatively.

样例输入

5 5

1 4 6 2 100

0 1

1 2

0 3

2 4

3 4

样例输出

100

#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
 
using namespace std;
const int N=1000005;
typedef long long ll;
 
struct node
{
    int to,index;
}G[N];
int n,m;
double dp[N],ans;
int head[N],cnt;
int c[N],deg[N];
void add(int u,int v)
{
    G[++cnt].to=v;
    G[cnt].index=head[u];
    head[u]=cnt;
}
 
void dfs(int x)
{
    if(dp[x]>0) return ;
    dp[x]=c[x];
    for(int i=head[x];i;i=G[i].index)
    {
        int v=G[i].to;
        dfs(v);
        dp[x]=max(dp[x],max(dp[v],dp[v]/2+c[x]));
    }
    ans=max(ans,dp[x]);
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++) scanf("%d",&c[i]);
    for(int i=0;i<m;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        add(u,v);
        deg[v]++;
    }
    for(int i=0;i<n;i++) if(!deg[i]) dfs(i);
    printf("%f\n",ans);
    return 0;
}

问题 I: Riddle

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

提交 状态 题面

题目描述

Kiana wants to give you a riddle. She gives you a piece of paper with n numbers on it. Any two numbers are considered different although they may have the same value.

Some numbers on the paper represent the weight of some toys, while the others represent the weight of some giftbags containing those toys. It is NOT required that each toy should be put into a giftbag, but each giftbag whose weight written on the paper should contain at least one toy. The weight of a giftbag is the total weight of toys it contains. Two toys or two giftbags are considered different if their weights are represented by different numbers.

The riddle is how many different ways Kiana can get these numbers on the paper. Two ways are considered different if one of the numbers represents different kinds of weight (In one way it represents the weight of a toy but in the other way it represents the weight of a giftbag) or one of the giftbags contains different toys.

输入

Read from the standard input.

There are several test cases in the data. The first line contains one integer T(T ≤ 5), the number of cases. Then for each test case:

The first line contains one integer n denoting how many numbers are written on the paper. It is guaranteed that n is no more than 15.

The second line contains n integers, which represent the numbers Kiana writes on the paper. It is guaranteed that each of the numbers is no more than 2000.

输出

Write to the standard output.

For each test case, output one integer denoting how many different ways Kiana can get these numbers.

样例输入

3

3

1 1 1

5

1 1 2 2 3

10

1 2 3 4 5 6 7 8 9 10

样例输出

7

15

127

提示

In the first test case, there are 7 different ways to get these numbers:

1.All three numbers represent the weight of toys, which means there are no giftbags.

2.The first number represents the weight of a giftbag containing a toy whose weight is represented by the second number. The third number represents the weight of a toy not in a giftbag.

3.The first number represents the weight of a giftbag containing a toy whose weight is represented by the third number. The second number represents the weight of a toy not in a giftbag.

4.The second number represents the weight of a giftbag containing a toy whose weight is represented by the first number. The third number represents the weight of a toy not in a giftbag.

5.The second number represents the weight of a giftbag containing a toy whose weight is represented by the third number. The first number represents the weight of a toy not in a giftbag.

6.The third number represents the weight of a giftbag containing a toy whose weight is represented by the first number. The second number represents the weight of a toy not in a giftbag.

7.The third number represents the weight of a giftbag containing a toy whose weight is represented by the second number. The first number represents the weight of a toy not in a giftbag.

Notice that 2. and 3. are different ways although they both mean one giftbag containing only one toy with weight 1. But the toy in the giftbag in two ways are considered to be different because their weights are represented by different numbers.

状压DP

对所有的东西进行分组,状态压缩,比方说3个物品,101(2)表示这一组中是第一个和第三个,资一个组表示的是,其中一个是包,其他都是的是这个包里面的玩具。sum数组存的就是这个组所有物品的权值的总值,然后对于一个组j,如果有一个物品a[i]的权值,满足sum[j]-a[i]==a[i],说明对于这一个组,第i个物品可以当包,然后其他的物品当玩具放在这个包里,那么,对于每个组,就会有几个不同的物品当包,用bag数组来表示对于i状态这个组,有几种情况来对应。

最后就是转移,对于状态组i,之前的状态j一定要有(i&j==0),因为一个物品不可能既在a组又在b组,然后对于符合条件的i,j两种状态,转移方程为dp[i|j]+=dp[j]*bag[i],即把两个状态和起来,然后把之前的情况和这个组的可能情况乘起来就是i,j合并后的,对于所有的i|j的所有可能和起来就行

在最后一步转移的过程中,对于组i,怎么快速找到对应所有的j是个问题,如果全部遍历一遍的话,复杂度是o((1<<15)*(1<<!5)),会T掉,在这里用为运算取了个巧,对于i,只需要j=(1<<n)-1-i开始,每次更改成j=(j-1)&((1<<n)-1-i),试过8位这样是密集的,但更高位没法证明。

#pragma GCC optimize(3,"Ofast","inline")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
#include <vector>
#include <map>
#include <cmath>
#include <stack>
#include <set>
using namespace std;
typedef  long long ll;
const int maxn = 20;
int dp[1<<16],sum[1<<16],bag[1<<16],a[maxn];
int main()
{
    ios::sync_with_stdio("false");
    int _;
    cin>>_;
    while(_--){
        int n;
        cin>>n;
        //cout<<n<<endl;
        for(int i=0;i<=(1<<n);i++){
            //cout<<i<<endl;
            dp[i]=1;
            sum[i]=0;
            bag[i]=0;
        }
        //cout<<"input"<<endl;
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        //cout<<"start"<<endl;
 
        for(int i=1;i<(1<<n);i++)
            for(int j=1;j<=n;j++)
                if(i&(1<<(j-1)))
                    sum[i]+=a[j];
        //cout<<'a'<<endl;
        for(int i=1;i<(1<<n);i++)
            for(int j=1;j<=n;j++)
                if(i&(1<<(j-1)))
                    if(sum[i]-a[j]==a[j])
                        bag[i]++;
        //cout<<'b'<<endl;
        for(int i=1;i<(1<<n);i++)
            for(int j=((1<<n)-1-i);;j=(j-1)&((1<<n)-1-i)){
                dp[i|j]+=dp[j]*bag[i];
                if(j==0) break;
            }
        cout<<dp[(1<<n)-1]<<endl;
    }
    return 0;
}

问题 B: Niuniu’s Clock

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

提交 状态 题面

题目描述

Niuniu has a clock with two hands, an hour hand and a minute hand. Sometimes it looks like just having one hand because the two hands coincide. Niuniu wants to know how many times the two hands coincide from A’o clock to B’o clock (0≤A<B≤24). (If the two hands coincide at A’o clock, it will be counted. But coincidence will not be counted at B’o clock.)

输入

Read from the standard input.

The first line contains an integer T (0 ≤ T ≤ 300) which is the number of cases.

The next T lines give two integers A and B for each case.

输出

Write to the standard output.

Output T lines, each line containing an integer as the answer of each case.

样例输入

1

14 17

样例输出

3

提示

From 14’o clock to 17’o clock, the two hands coincide three times at about 14:11,15:16 and 16:22 respectively.

#include <bits/stdc++.h>
 
using namespace std;
 
int main()
{
    int t;
    cin>>t;
    while (t--) {
        int A,B;
        scanf ("%d%d",&A,&B);
        if(A<12) {
            if(B<12) {
                printf ("%d\n",B-A);
            }
            else if(B>=12) {
                if(B==24) printf ("%d\n",B-A-2);
                else printf ("%d\n",B-A-1);
            }
        }
        else if(A>=12) {
            if(B==24) printf ("%d\n",B-A-1);
            else printf ("%d\n",B-A);
        }
    }
    return 0;
}

问题 C: Color

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

提交 状态 题面

题目描述

Alice, Bob and Yazid are good friends.

Each of them has a color, red, green or blue. Everyone’s color is different from the others’. They can describe their own colors in the format of name is color., such as Yazid is green..

Now they have made their descriptions in some order. After that, Yazid will do the following operations:

1.Connect the 3 sentences to get the initial string.

2.Remove all non-alphabetic characters.

3.Change all uppercase letters to lowercase.

For example, if the initial string is Yazid is green.Alice is red.Bob is blue., then after Yazid’s all operations, it will be turned to yazidisgreenaliceisredbobisblue.

Finally, Alice and Bob will insert any lowercase letters into any positions in this string to get the final string.

You are given the final string. Your task is to find the initial string. In particular:

•If there are multiple solutions, please output the minimum one in lexical order.

•If there is no solution, please output No solution. instead.

输入

Read from the standard input.

The first line contains one integer T (T ≤ 500), representing the number of test cases. For each case:

•One line of a string containing only lowercase letters, representing the final string.

•It is guaranteed that the length of the final string won’t exceed 600.

输出

Write to the standard output.

For each case:

•One line of a string, representing the initial string or No solution..

样例输入

4

aliceisredbobisblueyazidisgreen

aliceisgreenbobisgreenyazidisgreen

aliceisyellowbobisblueyazidisgreen

xxyazidxxisxxgreenxxbobisblueaxlxixcxexixsxrxexdx

样例输出

Alice is red.Bob is blue.Yazid is green.

No solution.

No solution.

Yazid is green.Bob is blue.Alice is red.

#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
 
using namespace std;
const int N=1000;
 
string s;
bool vis[30];
string outp,geti;
string name[3];
string color[3];
string out[100];
bool visn[3],visc[3];
int cnt;
 
void build(int len,string temp)
{
    if(len==3)
    {
        //cout<<temp<<endl;
        out[cnt++]=temp;
        return;
    }
    for(int i=0;i<3;i++)
    {
        if(visn[i]) continue;
        for(int j=0;j<3;j++)
        {
            if(visc[j]) continue;
            string t=temp+name[i]+" is "+color[j]+".";
            visn[i]=true;
            visc[j]=true;
            build (len+1,t);
            visn[i]= false;
            visc[j]=false;
        }
    }
}
void init()
{
    ios::sync_with_stdio (false);
    s="aliceisredbobisblueyazidisgreen";
    for(int i=0;i<s.size ();i++)  if(isalpha (s[i])) vis[s[i]-'a']= true;
    name[0]="Alice";
    name[1]="Bob";
    name[2]="Yazid";
    color[2]="red";
    color[1]="green";
    color[0]="blue";
    build(0,"");
    sort(out,out+cnt);
   // cout<<out[0]<<endl;
}
 
int main()
{
    init ();
    int T;
    cin>>T;
    while (T--)
    {
        cin>>geti;
        outp.clear();
        for(int i=0;i<geti.size();i++) if(vis[geti[i]-'a']) outp.push_back (geti[i]);
        bool flag= true;
        for(int i=0;i<cnt;i++)
        {
            int j=0;
            for(int k=0;k<outp.size ();k++)
            {
                if(outp[k]==tolower (out[i][j]))
                {
                    j++;
                    while (out[i][j]==' ' || out[i][j]=='.') j++;
                }
            }
            if(j==out[i].size ())
            {
                cout<<out[i]<<endl;
                flag=false;
                break;
            }
        }
        if(flag) cout<<"No solution."<<endl;
    }
}

问题 E: Fruit Slicer

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

提交 状态 题面

题目描述

John, a student who is taking the game development course, recently developed a mobile game called Fruit Slicer for his coursework. In the game the player slices fruits that are throw into the air by swiping the touch screen. However the game is quite simple because John was not able to write code for the geometry required for a more complex version. In the game each slice is a straight line of infinite length, and all fruits have the same shape of a circle with unit radius. The figure shows a cool snapshot of John’s game.

John introduces his game to his best friend Sean, who soon gets bored of playing the simple game. But as a teaching assistant of the algorithm course, Sean decides to turn the game into a homework assignment. He asks the students in the algorithms course to write a program that can compute the best slice at any given moment of the game. Given the locations of the fruits, the program should determine the maximum number of fruits that can be sliced with a single straight-line swipe.

As a student in Sean’s class, you are now the one who is facing this challenge.

输入

The first line has a single integer n (1≤n≤100). The next n lines each have two real numbers giving the x and y coordinates of a fruit. All coordinates have an absolute value no larger than 104 and are given with exactly two digits after the decimal point. Fruits may overlap.

输出

Output the maximum number of fruits that can be sliced with one straight-line swipe. A swipe slices a fruit if the line intersects the inner part or the boundary of the fruit.

样例输入

5

1.00 5.00

3.00 3.00

4.00 2.00

6.00 4.50

7.00 1.00

样例输出

4

计算几何

#include <bits/stdc++.h>
 
#define rep(i , a , b) for(int i=(a);i<=(b);i++)
using namespace std;
typedef long long ll;
 
struct Point {
    ll x , y;
 
    Point () {}
 
    Point (double _x , double _y) {
        x = _x;
        y = _y;
    }
 
    Point operator- (const Point &b) const {
        return Point (x - b.x , y - b.y);
    }
 
    ll operator^ (const Point &b) const {
        return x * b.y - y * b.x;
    }
 
    ll operator* (const Point &b) const {
        return x * b.x + y * b.y;
    }
};
 
Point operator* (Point A , ll p) {
    return Point (A.x * p , A.y * p);
}
 
struct Circle {
    Point c;
    ll r;
 
    Circle () {}
 
    Circle (Point c , ll r) : c (c) , r (r) {}
 
    bool operator< (const Circle &a) const {
        if ( a.c.x != c.x ) return c.x < a.c.x;
        return c.y < a.c.y;
    }
} e[111];
 
ll dist (Point p1 , ll a , ll b , ll c) {
    ll x = p1.x;
    ll y = p1.y;
    if ( abs (x * a + y * b + c) >= 1e9 ) return - 1;
    return ( x * a + y * b + c ) * ( x * a + y * b + c );
}
 
bool inter (ll a , ll b , ll c , Circle c1) {
    if ( dist (c1.c , a , b , c) == - 1 ) return false;
    return dist (c1.c , a , b , c) <= 1ll * 40000 * ( a * a + b * b );
}
 
bool same (Circle a , Circle b) {
    if ( a.c.x == b.c.x && a.c.y == b.c.y ) return true;
    return false;
}
 
ll read(string s) {
    ll op;
    ll ans=0;
    if(s[0]=='-') op=-1;
    else op=1;
    for(int i=0;i<s.length ();i++) {
        if(s[i]=='.'||s[i]=='-') continue;
        ans=ans*10+s[i]-'0';
    }
    return ans*op;
}
int main () {
    int n;
    scanf ("%d" , &n);
    for ( int i = 1 ; i <= n ; i ++ ) {
       string s1,s2;
       cin>>s1>>s2;
       e[ i ].c.x = read(s1);
       e[ i ].c.y = read(s2);
       e[ i ].r = 1ll * 100;
    }
    if ( n == 1 ) {
        printf ("1\n");
        return 0;
    }
    if ( n == 2 ) {
        printf ("2\n");
        return 0;
    }
    int ans = 0;
    sort (e + 1 , e + 1 + n);
    rep (i , 1 , n) {
        int res = 0;
        rep (j , 1 , n) {
            if ( same (e[ i ] , e[ j ])) res ++;
        }
        ans = max (ans , res);
    }
    int ma = 0;
    rep (i , 1 , n) {
        rep(j , i + 1 , n) {
            if ( same (e[ i ] , e[ j ])) continue;
            ll x1 = e[ i ].c.x , y1 = e[ i ].c.y;
            ll x2 = e[ j ].c.x , y2 = e[ j ].c.y;
            ll a = y2 - y1;
            ll b = x1 - x2;
            ll c = x2 * y1 - x1 * y2;
            Point v = e[ j ].c - e[ i ].c;
            int ans1 = 2;
            int ans2 = 2;
            rep (k , 1 , n) {
                if ( k == i || k == j ) continue;
                Point v1 = e[ k ].c - e[ i ].c;
                if ( inter (a , b , c , e[ k ]) && (v ^ v1) <= 0 ) ans1 ++;
                if ( inter (a , b , c , e[ k ]) && (v ^ v1) >= 0 ) ans2 ++;
            }
            ma = max (ma , max (ans1 , ans2));
        }
    }
    ans = max (ma , ans);
    cout << ans << endl;
    return 0;
}

问题 L: Superdoku

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

提交 状态 题面

题目描述

Alice and Bob are big fans of math. In particular, they are very excited about playing games that are related to numbers. Whenever they see a puzzle like Sudoku, they cannot stop themselves from solving it. The objective of Sudoku is to fill a 9×9 grid with digits so that each column, each row, and each of the nine (3×3) subgrids that compose the grid (also called “boxes”, “blocks”, or “regions”) contains all of the digits from 1 to 9. The puzzle setter provides a partially completed grid, which for a well-posed puzzle has a single solution.

After many years of solving Sudoku problems, Alice and Bob are tired of Sudoku. They have been trying to develop a harder variation of Sudoku, which they are calling Superdoku. In Superdoku, the grid is bigger – n×n instead of just 9×9. However, the “block” constraints are impossible to formulate when there are no further constraints on n. Therefore, there are no block constraints in Superdoku. Instead, the goal is simply to make sure that each column and each row in the grid contains all of the integers from 1 to n. After playing for a while in the standard way (where any of the grid cells may have previously been filled in), they decide that the game is too difficult and they want to simplify it. Therefore, they decide to make the initial grid further constrained. They constrain the board by filling in the first k rows completely.

Alice and Bob both believe that Superdoku is solvable. However, since n could be very big, it may still take a long time to figure out a solution. They don’t want to spend too much time on this single game, so they are asking for your help!

输入

The input consists of a single test case. The first line lists two space-separated integers 1≤n≤100 and 0≤k≤n, denoting the size of the grid (n×n) and the number of rows k that are already filled in. Each of the following k lines contains n space-separated integers, denoting the first k given rows. All integers in these k lines are between 1 and n.

输出

Output either “yes” or “no” on the first line, indicating if there is a solution.

样例输入

4 2

1 2 3 4

2 3 4 1

样例输出

yes

直接验证给的k行合不合法就可以了

#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
 
using namespace std;
typedef  long long ll;
const int N  = 110;
typedef pair<int,int>P;
 
int n,k;
int nu[N][N];
bool visc[N][N],visr[N][N];
 
int main()
{
    bool flag=true;
    scanf("%d%d",&n,&k);
    for(int i=1;i<=k;i++)
    {
        for(int j=1;j<=n;j++)
        {
            scanf("%d",&nu[i][j]);
            if(visc[j][nu[i][j]] || visr[i][nu[i][j]]) flag=false;
            visc[j][nu[i][j]]=true;
            visr[i][nu[i][j]]=true;
        }
    }
    if(!flag)
    {
        puts("no");
        return 0;
    }
    puts("yes");
    return 0;
}

问题 K: Run-Length Encoding, Run!

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

提交 状态 题面

题目描述

Forrest lives in a prehistoric era of “dial-up Internet.” Unlike the fast streaming of today’s broadband era, dial-up connections are only capable of transmitting small amounts of text data at reasonable speeds. Forrest has noticed that his communications typically include repeated characters, and has designed a simple compression scheme based on repeated information. Text data is encoded for transmission, possibly resulting in a much shorter data string, and decoded after transmission to reveal the original data.

The compression scheme is rather simple. When encoding a text string, repeated consecutive characters are replaced by a single instance of that character and the number of occurrences of that character (the character’s run length). Decoding the encoded string results in the original string by repeating each character the number of times encoded by the run length. Forrest calls this encoding scheme run-length encoding. (We don’t think he was actually the first person to invent it, but we haven’t mentioned that to him.)

For example, the string HHHeelllo is encoded as H3e2l3o1. Decoding H3e2l3o1 results in the original string. Forrest has hired you to write an implementation for his run-length encoding algorithm.

输入

Input consists of a single line of text. The line starts with a single letter: E for encode or D for decode. This letter is followed by a single space and then a message. The message consists of 1 to 100 characters.

Each string to encode contains only upper- and lowercase English letters, underscores, periods, and exclamation points. No consecutive sequence of characters exceeds 9 repetitions.

Each string to decode has even length. Its characters alternate between the same characters as strings to encode and a single digit between 1 and 9, indicating the run length for the preceding character.

输出

On an input of E output the run-length encoding of the provided message. On an input of D output the original string corresponding to the given run-length encoding.

样例输入

E HHHeellloWooorrrrlld!!

样例输出

H3e2l3o1W1o3r4l2d1!2

暴力模拟大水题

#pragma GCC optimize(3,"Ofast","inline")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
#include <vector>
#include <map>
#include <cmath>
#include <stack>
#include <set>
using namespace std;
typedef  long long ll;
int main()
{
    string a,b;
    cin>>a>>b;
    //cout<<a<<" "<<b<<endl;
    if(a=="E"){
        int number=0;
        char ch='\0';
        bool flag=true;
        for(int i=0;i<b.size();i++){
            if(ch==b[i]){
                number++;
            }else{
                if(!flag) printf("%d",number);
                putchar(b[i]);
                number=1;
                flag=false;
            }
            ch=b[i];
        }
        printf("%d\n",number );
    }else{
        int number=0;
        char ch = '\0';
        for(int i=0;i<b.size();i+=2){
            ch=b[i];
            number=b[i+1]-'0';
            for(int j=0;j<number;j++){
                putchar(ch);
            }
        }
        putchar('\n');
    }
    return 0;
}

问题 G: Left and Right

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

提交 状态 题面

题目描述

With modern technology advancement, it is now possible to deliver mail with a robot! There is a neighborhood on a long horizontal road, on which there are n houses numbered 1 to n from left to right. Every day a mail delivery robot receives a pile of letters with exactly one letter for each house. Due to mechanical restrictions, the robot cannot sort the letters. It always checks the letter on top of the pile, visits the house that should receive that letter and delivers it. The robot repeats this procedure until all the letters are delivered. As a result, each of the n houses is visited by the robot exactly once during the mail delivery of a single day.

The mail delivery robot has a tracking device that records its delivery route. One day the device was broken, and the exact route was lost. However, the technical team managed to recover the moving directions of the robot from the broken device, which are represented as a string consisting of n−1 letters. The i-th letter of the string is ‘L’ (or ‘R’) if the (i+1)-th house visited by the robot is on the left (or right) of the i-th house visited. For example, if n=4 and the robot visited the houses in the order of 2,4,3,1, its moving directions would be “RLL”.

With the moving directions, it may be possible to determine the order in which the robot visited the houses. The technical team has asked you to write a program to do that. There can be multiple orders producing the same moving directions, among which you should find the lexicographically earliest order.

输入

The input has a single integer n (2≤n≤2⋅105) on the first line. The second line has a string of length n−1 consisting of letters ‘L’ and ‘R’ giving the moving directions of the robot.

输出

Output the lexicographically earliest order in which the robot may have visited the houses and delivered the letters according to the moving directions. Consider two different integer sequences of equal length A=(a1,a2,…,ak) and B=(b1,b2,…,bk), and let 1≤i≤k be the lowest-numbered index where ai≠bi. Then A is lexicographically earlier than B if ai<bi; otherwise B is lexicographically earlier than A.

样例输入

3

LR

样例输出

2

1

3

#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
 
using namespace std;
const int maxn = 2e5+10;
bool vis[maxn];
int ans[maxn];
int main()
{
    int n;
    scanf("%d",&n);
    string s;
    cin>>s;
    int l=s.length ();
    int now=n;
    int p=n;
    for(int i=l-1;i>=0;i--) {
       if(s[i]=='R') {
           ans[i+2]=now;
           now=now-(p-i-1);
           p=i+1;
       }
    }
    for(int i=1;i<=n;i++) {
        if(ans[i]==0) ans[i]=now--;
        else {
            p=i;
            break;
        }
    }
    for(int i=p+1;i<=n;i++) {
        if(ans[i]==0) ans[i]=ans[i-1]-1;
    }
    for(int i=1;i<=n;i++) printf("%d\n",ans[i]);
    return 0;
}
通过 WordPress.com 设计一个这样的站点
从这里开始