1. 程式人生 > >ACM-ICPC 2018 焦作賽區網絡預賽 B. Mathematical Curse << DP

ACM-ICPC 2018 焦作賽區網絡預賽 B. Mathematical Curse << DP

each tween integer pac while all initial ann oms

A prince of the Science Continent was imprisoned in a castle because of his contempt for mathematics when he was young, and was entangled in some mathematical curses. He studied hard until he reached adulthood and decided to use his knowledge to escape the castle.

There are NN rooms from the place where he was imprisoned to the exit of the castle. In the i^{th}ith room, there is a wizard who has a resentment value of a[i]a[i]. The prince has MM curses, the j^{th}jth curse is f[j]f[j], and f[j]f[j] represents one of the four arithmetic operations, namely addition(‘+‘

), subtraction(‘-‘), multiplication(‘*‘), and integer division(‘/‘). The prince‘s initial resentment value is KK. Entering a room and fighting with the wizard will eliminate a curse, but the prince‘s resentment value will become the result of the arithmetic operation f[j]f[j] with the wizard‘s resentment value. That is, if the prince eliminates the j^{th}jth curse in the i^{th}ith room, then his resentment value will change from xx to (x\ f[j]\ a[i]x f[j] a[i]), for example, when x=1, a[i]=2, f[j]=x=1,a[i]=2,f[j]=‘+‘
, then xx will become 1+2=31+2=3.

Before the prince escapes from the castle, he must eliminate all the curses. He must go from a[1]a[1] to a[N]a[N] in order and cannot turn back. He must also eliminate the f[1]f[1] to f[M]f[M] curses in order(It is guaranteed that N\ge MNM). What is the maximum resentment value that the prince may have when he leaves the castle?

Input

The first line contains an integer T(1 \le T \le 1000)T(1T1000), which is the number of test cases.

For each test case, the first line contains three non-zero integers: N(1 \le N \le 1000), M(1 \le M \le 5)N(1N1000),M(1M5) and K(-1000 \le K \le 1000K(?1000K1000), the second line contains NN non-zero integers: a[1], a[2], ..., a[N](-1000 \le a[i] \le 1000)a[1],a[2],...,a[N](?1000a[i]1000), and the third line contains MM characters: f[1], f[2], ..., f[M](f[j] =f[1],f[2],...,f[M](f[j]=‘+‘,‘-‘,‘*‘,‘/‘, with no spaces in between.

Output

For each test case, output one line containing a single integer.

樣例輸入

3
2 1 5
2 3
/
3 2 1
1 2 3
++
4 4 5
1 2 3 4
+-*/

樣例輸出

2
6
3
題意:輸入n個數m個字符,然後說,必須要把運算符一個一個按順序都用完,開始有個數k然後你在利用每個運算符和數字,盡量使這個數最大,
思路:然後因為裏面含有負數,所有的話我們要考慮最大值最小值兩種情況去
,然後還要考慮當前不取得情況,我們取最優的,當前位置取最優的最大值與最小值
 #include<bits/stdc++.h>
 2 using namespace std;
 3 const long long INF=0x3f3f3f3f3f3f3f3f;
 4 long long dpmax[1005][6];
 5 long long dpmin[1005][6];
 6 long long room[1005];
 7 int main()
 8 {
 9     int T;
10     scanf("%d",&T);
11     while(T--)
12     {
13         long long n,m,k;
14         scanf("%lld%lld%lld",&n,&m,&k);
15         for(int i=1;i<=n;i++)
16             scanf("%lld",&room[i]);
17         char op[10];
18         scanf("%s",op+1);
19         memset(dpmax,0,sizeof(dpmax));
20         memset(dpmin,0,sizeof(dpmin));
21         dpmax[0][0]=k;
22         dpmin[0][0]=k;
23         for(int i=1;i<=n;i++)
24         {
25             for(int j=0;j<=m;j++)
26             {
27                 if(j==0)
28                     dpmax[i][j]=k,dpmin[i][j]=k;
29                 else{
30                     long long temp=dpmax[i-1][j-1],temp2=dpmin[i-1][j-1];
31                     long long maxx,minn;
32                     if(op[j]==+)
33                     {
34                         maxx=max(temp+room[i],temp2+room[i]);
35                         minn=min(temp+room[i],temp2+room[i]);
36                     }
37                     else if(op[j]==-)
38                     {
39                         maxx=max(temp-room[i],temp2-room[i]);
40                         minn=min(temp-room[i],temp2-room[i]);
41                     }
42                     else if(op[j]==/)
43                     {
44                         maxx=max(temp/room[i],temp2/room[i]);
45                         minn=min(temp/room[i],temp2/room[i]);
46                     }
47                     else{
48                         maxx=max(temp*room[i],temp2*room[i]);
49                         minn=min(temp*room[i],temp2*room[i]);
50                     }
51                     dpmax[i][j]=max(maxx,dpmax[i-1][j]);
52                     dpmin[i][j]=min(minn,dpmin[i-1][j]);
53                     if(i==j){
54                         dpmax[i][j]=maxx;
55                         dpmin[i][j]=minn;
56                     }
57                 }
58             }
59         }
60         printf("%lld\n",dpmax[n][m]);
61     }
62 
63 }

ACM-ICPC 2018 焦作賽區網絡預賽 B. Mathematical Curse << DP