Pretty Matrix

Time Limit: 1 Second      Memory Limit: 65536 KB

DreamGrid's birthday is coming. As his best friend, BaoBao is going to prepare a gift for him.

As we all know, BaoBao has a lot of matrices. This time he picks an integer matrix with  columns from his collection, but he thinks it's not pretty enough. On the one hand, he doesn't want to be stingy, but some integers in the matrix seem to be too small. On the other hand, he knows that DreamGrid is not good at algebra and hates large numbers, but some integers in the matrix seem to be too large and are not suitable for a gift to DreamGrid.

Based on the above consideration, BaoBao declares that a matrix is pretty, if the following conditions are satisfied:

  1. For every integer .
  2. For every integer .

where  are two integers chosen by BaoBao.

Given the matrix BaoBao picks, along with the two integers , please help BaoBao change some integers in the matrix (BaoBao can change an integer in the matrix to any integer) so that the matrix becomes a pretty matrix. As changing integers in the matrix is tiring, please tell BaoBao the minimum number of integers in the matrix he has to change to make the matrix pretty.

Input

There are multiple test cases. The first line of input is an integer  (about 100), indicating the number of test cases. For each test case:

The first line contains four integers ). Their meanings are described above.

For the next ), representing the original matrix.

Output

For each test case output one line indicating the answer. If it's impossible to make the matrix pretty, print "No Solution" (without quotes) instead.

Sample Input

2
3 4 2 3
3 2 2 2
2 1 2 3
2 3 100 3
2 1 2 1
1
2

Sample Output

2
No Solution

#include <bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int T;
    cin>>T;
    while(T--)
    {
        int n,m,a,b,ans=0;
        cin>>n>>m>>a>>b;
        for(int i=0; i<n; i++)
            for(int j=0,x; j<m; j++)
            {
                cin>>x;
                if(x<a||x>b)ans++;
            }
        if(a>b)cout<<"No Solution\n";
        else cout<<ans<<"\n";
    }
    return 0;
}
PPAP

Time Limit: 1 Second      Memory Limit: 65536 KB

"I have a pen. I have an apple. Uh! Applepen."

"I have a pen. I have pineapple. Uh! Pineapplepen."

The above lyrics are taken from PPAP, a single by Pikotaro. It was released as a music video on YouTube on 25 August 2016, and has since become a viral video. As of October 2017, the official video has been viewed over 126 million times.

The 18th Zhejiang University Programming Contest Sponsored by TuSimple

Let's view this song from a mathematical perspective. In the lyrics there actually hides a function  as the input and works as follows:

  • First, calculate  here means string concatenation).
  • Then, capitalize the first character of .
  • Make  as its output, and the function is done.

For example, we have PPAP("pen", "apple") = "Applepen", and PPAP("pen", "pineapple") = "Pineapplepen".

Given two lowercased strings .

Input

The first line of the input contains an integer  (about 100), indicating the number of test cases. For each test case:

The first and only line contains two strings  consist of only lowercase English letters.

Output

For each test case output one line containing one string, indicating .

Sample Input

3
pen apple
pen pineapple
abc def

Sample Output

Applepen
Pineapplepen
Defabc

#include <bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int T;
    cin>>T;
    while(T--)
    {
        string s,c;
        cin>>s>>c;
        c[0]-=32;
        cout<<c<<s<<"\n";
    }
    return 0;
}
Mergeable Stack

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Given  initially empty stacks, there are three types of operations:

  • s v: Push the value -th stack.

  • s: Pop the topmost value out of the 

  • s t: Move every element in the -th stack in order.

    Precisely speaking, denote the original size of the .

    After this operation, the , this operation actually does nothing.

There are  operations in total. Please finish these operations in the input order and print the answer for every operation of the second type.

Input

There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

The first line contains two integers ), indicating the number of stacks and the number of operations.

The first integer of the following ), indicating the type of operation.

  • If ) follow, indicating an operation of the first type.
  • If ) follows, indicating an operation of the second type.
  • If ) follow, indicating an operation of the third type.

It's guaranteed that neither the sum of .

Output

For each operation of the second type output one line, indicating the answer.

Sample Input

2
2 15
1 1 10
1 1 11
1 2 12
1 2 13
3 1 2
1 2 14
2 1
2 1
2 1
2 1
2 1
3 2 1
2 2
2 2
2 2
3 7
3 1 2
3 1 3
3 2 1
2 1
2 2
2 3
2 3

Sample Output

13
12
11
10
EMPTY
14
EMPTY
EMPTY
EMPTY
EMPTY
EMPTY
EMPTY

 


卡函数,要用list的splice

就是这个合并很耗时间,用链表去模拟也是可以的

#include <bits/stdc++.h>
using namespace std;
const int N=3e5+5;
list<int>L[N];
int main()
{
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int T;
    cin>>T;
    while(T--)
    {
        for(int i=0; i<N; i++)L[i].clear();
        int n,q;
        cin>>n>>q;
        while(q--)
        {
            int op;
            cin>>op;
            if(op==1)
            {
                int a,b;
                cin>>a>>b;
                L[a].push_back(b);
            }
            else if(op==2)
            {
                int a;
                cin>>a;
                if(L[a].size())
                {
                    cout<<L[a].back()<<"\n";
                    L[a].pop_back();
                }
                else cout<<"EMPTY\n";
            }
            else
            {
                int a,b;
                cin>>a>>b;
                L[a].splice(L[a].end(),L[b]);
            }
        }
    }
    return 0;
}
Traffic Light

Time Limit: 1 Second      Memory Limit: 131072 KB

DreamGrid City is a city with .

At each intersection stands a traffic light. A traffic light can only be in one of the two states: 0 and 1. If the traffic light at the intersection  (of course, the destination must be another intersection in the city).

BaoBao lives at the intersection . After his departure, in each minute the following things will happen in order:

  • BaoBao moves from his current intersection to another neighboring intersection along a road. As a law-abiding citizen, BaoBao has to obey the traffic light rules when moving.
  • Every traffic light changes its state. If a traffic light is in state 0, it will switch to state 1; If a traffic light is in state 1, it will switch to state 0.

As an energetic young man, BaoBao doesn't want to wait for the traffic lights, and he must move in each minute until he arrives at DreamGrid's house. Please tell BaoBao the shortest possible time he can move from  to meet his friend, or tell him that this is impossible.

Input

There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

The first line contains two integers ), indicating the size of the city.

For the following .

The next line contains four integers ), indicating the starting intersection and the destination intersection.

It's guaranteed that the sum of .

Output

For each test case output one line containing one integer, indicating the shortest possible time (in minute) BaoBao can move from 

Sample Input

4
2 3
1 1 0
0 1 0
1 3 2 1
2 3
1 0 0
1 1 0
1 3 1 2
2 2
1 0
1 0
1 1 2 2
1 2
0 1
1 1 1 1

Sample Output

3
5
-1
0

Hint

For the first sample test case, BaoBao can follow this path: .

For the second sample test case, due to the traffic light rules, BaoBao can't go from .

For the third sample test case, it's easy to discover that BaoBao can only go back and forth between .

 按照要求dfs吧

#include <bits/stdc++.h>
using namespace std;
struct T
{
    int x,y,f;
} tt;
int dir[2]= {-1,1};
int solve()
{
    int n,m;
    scanf("%d%d",&n,&m);
    int a[n+5][m+5],vis[n+5][m+5];
    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++)
            scanf("%d",&a[i][j]),vis[i][j]=0;
    int x,y,s,t,f=0;
    scanf("%d%d%d%d",&x,&y,&s,&t);
    queue<T> q;
    vis[x][y]=1,q.push({x,y,f});
    while(!q.empty())
    {
        tt=q.front();
        q.pop();
        if(tt.x==s&&tt.y==t) return tt.f;
        int op=(a[tt.x][tt.y]+tt.f)%2;
        if(op==1)
        {
            for(int i=0; i<2; i++)
            {
                x=tt.x,y=tt.y+dir[i],f=tt.f+1;
                if(vis[x][y]||y<1||y>m) continue;
                vis[x][y]=1,q.push({x,y,f});
            }
        }
        else
        {
            for(int i=0; i<2; i++)
            {
                x=tt.x+dir[i],y=tt.y,f=tt.f+1;
                if(vis[x][y]||x<1||x>n) continue;
                vis[x][y]=1,q.push({x,y,f});
            }
        }
    }
    return -1;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)printf("%d\n",solve());
    return 0;
}

 

相关文章:

  • 2021-12-06
  • 2021-08-13
  • 2022-12-23
  • 2021-11-29
  • 2021-06-04
  • 2021-08-10
  • 2022-01-22
  • 2022-02-05
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-08-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案