2019 simulation game 09 games solving report

2019 simulation game 09 games solving report

Tags (separated by spaces): problem solving report Forever_chen 2019.8.20


Catalog la ~~


A question: teleport

[Problem of]
one day, twilight shiny suddenly how a sequence of integers a1, a2, ..., an order of a sequence of play does not drop interest. As a young unicorn, she can only be called "unit conversion" (unit shift) operation. In other words, she may be the last element in the sequence is moved to its starting position:
$$ A1, A2, ..., AN → AN, A1, A2, ..., AN-$$. 1
Please help twilight shiny calculate: her minimum number of operations needed to sort this sequence is how much?
[Input format
of the first line of an integer n (2≤n≤10 ^ 5).
The second row represents n integers a1, a2, ..., an ( 1≤ai≤10 ^ 5).
[Output Format]
If the sequence is not ordered, the output of -1
or a minimum operation frequency and outputs it shiny twilight sort required.
| Input Sample 1 | Sample Output 1 |
| ------ | ------ |
| 2
2 1 | 1 |
| Sample Input 2 | Sample Output 2 |
|. 1
. 1. 3 2 | -1 |
| sample input. 3 | sample output. 3 |
| 2
. 1 2 | 0 |

  • 【Analysis of Algorithms】
  • 模拟To meet the given topic does not drop sequence, we can give an example, if you want becomes $ 4, 5 $ to the sequence number of columns is only: $ 1,2,3,4,5; 5 , 1,2,3,4,; 4,5,1,2,3; 3,4,5,1,2; 2,3,4,5,1 $
    sequences observed are satisfied, we can see a law , that there will be a two or ordered does not drop sequence, and converted to a sequence of operations does not decrease the number is $ n-x + 1 $ (Total $ - $ reference number 1 $ + $ position, However, the operation sequence does not satisfy this law is already $ $ 1,2,3,4,5 not reduced sequence, requires special determination)
  • According to this rule, we can come up with a determination method, we may be a series of numbers that the smallest number as a reference number, as described above $ 5 $ sequences, we can each sequence $ 1 $ as a reference number, there will be $ 3 $ case
  • $ $ Reference number 1. In the first sequence, such as 1,2,3,4,5 $ $, and only the entire sequence is not reduced only established sequence, $ 0 or $ output (special judgment, can not be $ n-x + 1 $)
  • $ 2 $ benchmark in the last sequence, such as $ 2,3,4,5,1 $, only the sequence before the reference number in order to obtain a sequence not fall, otherwise the output of $ 0 $
  • 3,4,5,1,2 $, we can; $ 3 $ reference number in the middle of the sequence, e.g., $ 5,1,2,3,4; 4,5,1,2,3 before baseline sequences and sequence reference numbers respectively sorted less than descending column comparison, as described above $ 3 $ sequence $ 1 $ back to $ 2 $, $ 2 $ followed by $ 3 $, $ 1 $ front $ 5 $, $ 5 $ in front of $ 4 $, we can observe and draw law (a is an input sequence, b is the row sequence sorted, i is the position number of the reference (without changes operation), j is the number of positions prior to baseline or post-baseline (j incremented in the forward or backward decrement)):
    a number satisfying (the reference number of the former J $ - $): $ a [j ] = b [n-i + j + 1] $
    after the reference count satisfies ($ j ++ $): $ a [j] = b [n-i + j + 1] $

  • Note : the same number may occur, such as $ 2,3,4,1,1 $ (reference number appears more than once), so we have to add an action in the above methods: the plurality of reference number, as long as there is a reference number of meet all of the operations we give, we can not fall back sequence (outputs number of operations)
    A[给出序列ai] -->|排序后找到基准数| B(基准数b1)
    B --> C{基准数的三种情况}
    C -->|基准数在序列的第一位| D[满足整个序列为不下降序列]
    C -->|基准数在序列的中间| E[基准数前后都是不下降序列且a和b序列中的数字满足关系]
    C -->|基准数在序列的最后一位| F[满足基准数前序列为不下降序列]
    D --> G{可以进行操作}
    E --> G{可以进行操作}
    F --> G{可以进行操作}
  • [AC] Code
#include<bits/stdc++.h>
using namespace std;
int n,a[100010],b[100010],ans,x;
bool f=0,fg=0,fg2=0;
bool cmp(int x,int y) {
    return x<y;
}
bool work(int i) {
    bool f=0;
    x=i;//x记录i的值(基准数的位置),在最后运算时用
    if(i==1) {//基准数在序列的第一位
        for(int j=i+1; j<=n; j++) {
            if(a[j]!=b[j]) {
                f=1;
                break;
            }
        }
    } else {
        if(i==n) {//基准数在序列的最后一位
            for(int j=i-1; j>=1; j--) {
                if(a[j]!=b[j+1]) {
                    f=1;
                    break;
                }
            }
        } else {//基准数在序列的中间
            for(int j=i-1; j>=1; j--) {//对基准数前的数字进行判断
                if(a[j]!=b[n-i+j+1]) {
                    f=1;
                    break;
                }
            }
            for(int j=i+1; j<=n; j++) {//对基准数后的数字进行判断
                if(a[j]!=b[j-i+1]) {
                    f=1;
                    break;
                }
            }
        }
    }
    return f;
}
int main() {
    scanf("%d",&n);
    for(int i=1; i<=n; i++) {
        scanf("%d",&a[i]);
        b[i]=a[i];
    }
    sort(b+1,b+1+n,cmp);
    for(int i=1; i<=n; i++) {//特判,当输入的序列就为不下降序列时
        if(a[i]!=b[i]) {
            fg=1;
        }
    }
    if(fg==0) {
        printf("0");
        return 0;
    } else {
        for(int i=1; i<=n; i++) {
            if(a[i]==b[1]) {
                if(work(i)==0) {//只要一个基准数满足
                    printf("%d",n-x+1);
                    return 0;
                }
            }
        }
        printf("-1");
        
    }   
    //system("pause");
    return 0;
}

Question two: Ordering food

[Problem of]
the New Year coming, Lele opened a famous restaurant, called "chocolate restaurant.
This restaurant offers n vegetables, the first of which is priced vegetables i ci, ai copies were produced from restaurant orders on display, there m clients will pick a restaurant a visit today, the j-th customer will buy the first copies ti kinds of food dj, j + 1 and the first customers will come only after the j-th customer to buy. If the restaurant this person can not meet the demand for the first ti grow vegetables, then he will buy still has the lowest price of food to meet his needs, he will continue to buy enough food after the current minimum minimum food bought, until meeting his needs (Tip: If all dishes are sold out, but still can not meet his needs, then direct output 0, because the restaurant does not meet this person's request, the customer will angrily leave, regardless of how many dishes before, the customer will not pay, King meal to eat). If the restaurant can satisfy the people's needs, to output the total consumption of this person.
in short everyone must buy the kind of food he wants, not quantity When to buy the lowest price, until he needed to buy up the number, of course, all the dishes are buying can not meet his needs, then output 0. Your task is to calculate the total consumption of each person.
[Input Format ]
the first line of the input integers n and m (1≤n, m≤10 ^ 5) , representing the total number of different types of dishes and the total number of customers.
the second row input n positive integers a1, a2, ..., an ( 1≤ai≤10 ^ 7), ai represents the i-th initial number of vegetables.
the third input row n positive integers c1, c2, ..., cn ( 1≤ci≤10 ^ 6), ci denotes an i vegetables price.
m line below, one per line customer orders. j-th row input two positive integers tj and dj (1≤tj≤n, 1≤dj≤10 ^ 7) , each j-th client the kinds and amount of food ordered
[format] output
. the output common-m output line j-th row j-th total number of consumer customers (this does not meet current customer demand output 0).
| sample input 1 | sample Example output 1 |
| ------ | ------ |
|. 5. 8
. 8. 4. 5. 6. 7. 5. 1 2
. 6. 3. 3 2. 6. 3 2 2
2. 8
. 1. 4
. 4. 7
. 3. 4
. 6 10 | 22 is
24
14
10
39 |
| sample input 2 | sample output 2 |
|. 6. 6
. 6. 6. 6. 6. 6. 6
. 6 666 6666 66666 66 666666
. 1. 6
2. 6
. 3. 6
. 4. 6
. 5. 6
. 6 66 | 36
396
3996
39 996
399 996
0 |
| sample enter. 3 | sample output. 3 |
|. 6. 6
. 6. 6. 6. 6. 6. 6
. 6 666 6666 66666 66 666666
. 1. 6
2 13 is
. 3. 6
. 4. 11
. 5. 6
6 6 | 36
11058
99 996
4333326
0
0 |
[explain] Sample
Sample a: Client will provide the following five ways:
1. Gu supplied to a second user 6 parts vegetables, this time can not meet the his request, so bought the cheapest fourth and sixth vegetables 1 part 1 part vegetables. Price 6 3 + 1 2 + 1 2 = 22. 8 is the remaining number of vegetables {8,0,2,0,4,4,7,5}.
2. The second customer is provided to a first 4 parts of vegetables. Cost 4
6 = 24. {4,0,2,0,4,4,7,5} for the remaining number.
3. The third customer needs vegetables 7 parts of 4, since the 4 vegetables sold out, so the parts can only provide 4 6 8 3 parts of vegetables and vegetables. 4 Cost 2 + 3 2 = 14. {4,0,2,0,4,0,7,2} for the remaining number.
4. Fourth customer needs 4 parts of 3 vegetables, the restaurant will provide 2 parts 3 parts 2 and 8 vegetables vegetables. Cost 2 3 + 2 2 = 10. {4,0,0,0,4,0,7,0} for the remaining number.
5. Fifth customer needs 6 10 parts vegetables, vegetables sixth sold out, so the restaurant customer will provide vegetables 7 7 parts, 3 parts of a first vegetables. Cost 7 3 + 3 6 = 39. {1,0,0,0,4,0,0,0} for the remaining number.
Sample II: Every customer service according to their order, except the last one client, he angrily left and without payment. For example, the second customer needs vegetables second parts 6, so the cost is 66 6 = 396.
Sample III: Some customers may not get their orders for the service. For example, the second parts of the second client 13 need vegetables, it can only buy the most expensive parts of 3 6 vegetables, vegetables 1 4 parts, the cost was 66
6 + 666 6 + 6666 1 = 11058

  • 【Analysis of Algorithms】
    • 模拟We can enter keywords dish double sorted according to their price, they record the serial number of the original $ (id) $, the row good sequence to survive in a similar way to the list
    • Note : If the restaurant did not send to meet customer demand, customers will eat the rest of the dish, but do not pay (I really do not know did not pay, why should anger)
  • [AC] Code
#include<bits/stdc++.h>
using namespace std;
long long n,m,cnt;//cnt 总共还有多少菜
struct forever {
    long long c,num,nxt,id;//c价格,num数量,nxt下一个比a[i].c大的菜的id,id 序号
} a[100010];
bool cmp1(forever x,forever y) {
    return x.c<y.c;
}
bool cmp2(forever x,forever y) {
    return x.id<y.id;
}
int main() {
    scanf("%lld%lld",&n,&m);
    for(int i=1; i<=n; i++) {
        scanf("%lld",&a[i].num);
        a[i].id=i;
        a[i].nxt=1e9;//用nxt记录最大的菜 
        cnt+=a[i].num;
    }
    for(int i=1; i<=n; i++) {
        scanf("%lld",&a[i].c);
    }
    sort(a+1,a+1+n,cmp1);//按照菜的价格排序
    for(int i=1; i<n; i++) {//将下一个比a[i].c大的菜的id记下
        a[i].nxt=a[i+1].id;
    }
    int p=a[1].id;//p是目前存在的最便宜的菜的id
    sort(a+1,a+1+n,cmp2);//排回菜原始的id顺序 
    while(m--) {
        long long iid,inum,ans=0;
        scanf("%lld%lld",&iid,&inum);
        if(cnt<inum) {//如果菜不够,就输出0 
            cnt=0;
            printf("0\n");
            continue;
        }
        cnt-=inum;
        if(a[iid].num>=inum) {//如果顾客的菜够,就将菜的数量乘顾客所需的数量 
            a[iid].num-=inum;
            ans=a[iid].c*inum;
        } else {//如果顾客的菜不够
            ans+=a[iid].num*a[iid].c;//买完需要的菜 
            inum-=a[iid].num;
            a[iid].num=0;
            while(a[p].num<inum) {//找下一个价格最小的菜 
                inum-=a[p].num;
                ans+=a[p].c*a[p].num;
                a[p].num=0;
                p=a[p].nxt;//找下一个价格最小的菜 
            }
            ans+=inum*a[p].c;
            a[p].num-=inum;

        }
        printf("%lld\n",ans);
        while(p!=1e9 &&a[p].num==0)p=a[p].nxt;//如果当前菜刚好用完,找出现有的最小的菜
    }
    //system("pause");
    return 0;
}

Question three: hoof prints

[Problem of]
Although Belize when the cow to balance the sequence very happy, but he now presents a sequence of higher demand, is to ask left parenthesis before each sequence must be followed by a number of left parenthesis same the number of right parenthesis. For example: (((()))), it is a perfect balance sequence.
Belize one day when walking on the farm, he found hoof prints in the ground, the farm is a N N squares, each has a small box in the hoof prints. Belize want to start from the top left corner of the place squares, then each can be up or down or left or right to move one step, so that each small box he came in the hoof prints can form a perfect the balance of the sequence. Of course, Belize can not be duplicated through any small box.
Please help Belize in the N
to find out the length of the longest sequence of perfect squares in N.
[Input format
of the first line of a positive integer N, the size of the farm.
Next N lines of N characters, represents the distribution of the N * N squares of marks going.
[] Output format
is only a line integer representing the longest sequence length perfect, so if a perfect sequence (e.g., the right parenthesis is the start position) is not present, the output 0.
| Input Sample 1 | Sample Output 1 |
| ------ | ------ |
|. 4
(())
() ((
(() (
)))) |. 8 |

  • 【Analysis of Algorithms】
    • dfsBecause we have to find a figure in the 'perfect balance sequence', first of all think of search: $ dfs $, we can start the search from the coordinates $ (1,1) $, if the search will be to the left parenthesis left parenthesis the number $ l + 1 $, when the search will be to the right bracket right bracket number $ r + 1 $, when the search and the number of left parenthesis of the right parenthesis is the same answers $ $ max operation, and finally returns the maximum
    • Note : When $ (1,1) $, is a right parenthesis can directly output the $ 0 $ ( 'the perfect balance series' start from left parenthesis)
  • [Agreed] data range
  • $ 2 <= N <= 5 $; Description: sample, the cows walk sequence is such that:
    $$ 1 ()) $$
    $$ 2) (($$
    $$ 345 ($$
    $$ 876) $$
  • [AC] Code
#include<bits/stdc++.h>
using namespace std;
int n,ans;
int go1[4]={1,0,-1,0},go2[4]={0,1,0,-1};
char ch[10][10];
bool f[10][10];
void dfs(int x,int y,int l,int r) {
    if(l==r) {//当搜索到的左括号和右括号数量相同时
        //cout<<ans<<endl;
        ans=max(l*2,ans);//将搜索到的右括号和左括号的数量加起来(l+r或l*2或r*2就是‘平衡的完美序列’的长度)
        return;
    }
    for(int i=0; i<=3; i++) {
        if(x+go1[i]>0 && x+go1[i]<=n && y+go2[i]>0 && y+go2[i]<=n && f[x+go1[i]][y+go2[i]]==0) {//在满足不会超出图的边界的情况下且路没有被走过
            f[x+go1[i]][y+go2[i]]=1;
            if((ch[x+go1[i]][y+go2[i]]=='(')&&!r) {//搜索到左括号
                dfs(x+go1[i],y+go2[i],l+1,r);//左括号数量++
            }
            if(ch[x+go1[i]][y+go2[i]]==')') {//搜索到右括号
                dfs(x+go1[i],y+go2[i],l,r+1);//右括号的数量++
            }
            f[x+go1[i]][y+go2[i]]=0;
        }
    }
}
int main() {
    scanf("%d",&n);
    f[1][1]=1;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin>>ch[i][j];
        }
    }
    if(ch[1][1]==')'){//如果(1,1)为右括号
        printf("0");//直接输出0
        return 0;
    }
    dfs(1,1,1,0);
    printf("%d",ans);
    //system("pause");
    return 0;
}

Question four: Landscaped

[Problem of]
Farmer John has recently decided to beautify his garden, he needs to transport a lot of dirt. Garden by N blocks flowerbeds thereof. The i-th block when there Ai flowerbed initial quantities of mud. For purposes of beautifying, the soil must be such that the number of blocks Ai of the i flowerbeds becomes Bi.
John has three options: First, he can buy a unit into the soil in flowerbeds any, X-expense; the second, he can be removed from a unit of a soil in a flower bed, the expense of the Y; third, he a i-th unit block flower garden soil may be transported to the j-th block flower garden, the cost of Z * | ij |.
Please help John computational cost to achieve the minimum required spend.
[Input format
of the first line four integers are N, X, Y, Z.
Next N lines of two integers, respectively, Ai and Bi.
[Output Format]
only one line an integer representing the minimum cost.

Sample input 1 Sample output 1
4 100 200 1
1 4
2 3
3 2
4 0
210

[Note]
Data range: 1 <= N <= 100,0 <= Ai, Bi <= 10,0 <= X, Y, Z <= 1000. Description: All of the soil must be removed from the fourth flower garden, where 1 unit of soil are removed directly, at the expense of 200, and the remaining three units of four flower garden soil from a flowerbed to the first.

  • 【Analysis of Algorithms】
    • dpThis question we each are separate flower garden soil, such as the number of clay we will $ $ into $ 1,2,2,3,3,3,4,4,4,4 1,2,3,4 $. A 1 represents a flowerbed earthy 1 unit, 2 denotes a second block flowerbed two earthy units 2, 3 shows a third three flowerbed earthy units 3, 4 represents four the fourth earth flower garden has 4 units. Operation is moving the character to be carried out then into the desired character
    • $ 1 $ insert a character: $ f [i, j] = f [i, j-1] + x $ ($ f [i, j-1] $ denote the string $ 1 to I $ portion of the target string $ 1 to j-1 $ identical part, to make the string entered at this time $ I $ 1 to $ 1 to a target string J $ part of the same, it should insert a character, to pay $ X $)
    • 2. $ $ a character delete $ f [i, j] = f [i-1, j] + y $
    • $ 3 $ to move a character and to convert any character $ f [i, j] = f [i-1, j-1] + z * abs (a [i] -b [j]) $ $ a $ is entered string, $ b $ i.e., the target string
    • Initialization: When the input string is empty, insert the same character string length for an objective consideration of I $ X $
      , when the target string is empty, to be omitted with the same original character string length, the cost is $ I
      Y $
  • [AC] Code
#include<bits/stdc++.h> 
using namespace std;
int x,y,z,n,xx,yy,a[1001],b[1001],f[1001][1001],l,ll;
int main() {
    scanf("%d%d%d%d",&n,&x,&y,&z);
    for (int i=1; i<=n; i++) {
        scanf("%d%d",&xx,&yy);
        for (int j=1; j<=xx; j++){
            a[++l]=i;
        } 
            
        for (int j=1; j<=yy; j++){
            b[++ll]=i;
        } 
    }
    memset(f,0x7f,sizeof(f));
    for (int i=0; i<=l; i++){//初始化,代价为i*y
        f[i][0]=i*y;
    }
    for (int i=0; i<=ll; i++){//初始化,代价为i*x
        f[0][i]=x*i;
    }
    for (int i=1; i<=l; i++){
        for (int j=1; j<=ll; j++){
            f[i][j]=min(f[i-1][j-1]+z*abs(a[i]-b[j]),min(f[i-1][j]+y,f[i][j-1]+x));
        } 
    } 
    printf("%d",f[l][ll]);
    //system("pause"); 
    return 0;
}

Guess you like

Origin www.cnblogs.com/Forever-chen/p/11386729.html