US Mission Written

Particular sequence
time limit: C / C ++ language 2000MS; Other Languages 4000MS
memory limit: C / C ++ language 131072KB; Other Languages 655360KB
Title Description:
defined maximum length decrease subsequence of a sequence to this sequence is removed the least number of , so that each remaining number greater than equal to the number of his own front. For example, the maximum length decrease 1,0,0,1,1 sequence is 0,0,1,1, wherein the first one is removed, the remaining number of the latter are 0,0,1,1 number greater than equal to the previous number.
There is now a special sequence, the sequence numbers are all 0 or 1. You need to perform two tasks in the order given subject:
1. 0 1,1 a certain interval becomes 0 becomes
a sequence length of the longest sub not fall 2. Ask the whole sequence.
It will result in changes to the sequence after each operation, which means that the entire sequence will change constantly occur.
Input
The first line number 2 n, m, representative of sequence length and the number of interrogation
second row of n numbers, with no spaces. Each number 0 or 1, the i-th sequence in the i-th representative of the magnitude of
the next m lines of a query. Wherein the interrogation mode two operation is as follows:
1.cxy the interval [x, y] is 0 to 1, 1 to 0.
2.q longest query sequence does not decrease the whole length of the sequence.
Note that the first reference position from the beginning of the sequence, the subscript meaning the entire sequence is ... n-1,2
1≤n≤100000, 1≤m≤100000, 1≤x≤y≤n
output
for the second operation : q inquiry type, the output of the whole sequence of the longest sequence length is not reduced.
Sample input
55
10011
Q
C. 1. 5
Q
C. 1. 3
Q
sample output
. 4
. 3
. 4
prompts
Sample explain
1. The first inquiry, the original sequence is 10011, the answer is 0011
2. The second modification, the original sequence of 10011, was modified 01100
3. the third inquiry, the original sequence is 01,100, 011 or 000 the answer is
4. the fourth modification, the original sequence of 01100, 10000 revised
the fifth inquiry, the original sequence of 10000, the answer is 0000

Solution:
First, to explain what is called the longest sequence is not reduced, that is, 0 and 1 pick from a certain original sequence, to retain the relative positions of these figures constitute a substring not reduced, the maximum required length of the substring.

My approach is:

  • First find all 0s (all 1) does not decrease the length of this substring, as a backup option;
  • Then find the successive substring does not drop, the recording start position and an end position, see how many zeros before a start position, which is not 0 may be added continuously drop the longest substring constituting the sequence is not reduced, see the end position empathy after how much a 1;
  • Comparing the results of two steps, taking the maximum
from collections import Counter
def is_not_dec(l):
    cnt = Counter(l)
    mmax = max(cnt[0], cnt[1])
    l.append('$')
    s, e, sub_max = 0, 0, 0
    for i in range(1, len(l)):
        if l[i]=='$' or l[i-1] > l[i]:
            if e - s + 1 >= sub_max:
                m_s, m_e = s, e
                sub_max = e - s + 1
            s = i
        else:
            e = i
    cnt_left, cnt_right = Counter(l[:m_s]), Counter(l[e + 1:])
    mmax = max(mmax, sub_max + cnt_left[0] + cnt_right[1])
    return mmax

n, m = map(int, input().split())
s, l = input(), []
for i in s:
    l.append(int(i))
for i in range(m):
    cmd = input()
    if cmd[0] == 'q':
        print(is_not_dec(l))
    else:
        x, y = map(int, cmd[1:].
                   split())
        for i in range(x - 1, y):
            l[i] = abs(l[i] - 1)
    

Time limit: C / C ++ language 1000MS; other languages 3000MS
Memory Limit: C / C ++ language 65536KB; other languages 589824KB
subject description:
CNR is a love of running children, to this day, he was ready to run just k meters. The city's roads where he can be regarded as n points, m article composed of FIG undirected edges, each side having a fixed length.
CNR has obsessive-compulsive disorder, he went to run a certain destination to go the shortest path (of course, a number of the most short-circuit can be free to choose).
CNR wanted to know, he just ran and went to number k meters to go. Note that the edge point and the destination may be in the figure, and the shortest distance from the starting point to the destination exactly k CNR m.
If k is greater than the sum of all paths of such natural destination does not exist, a natural output is zero.
Input
Input the number of the first three rows, n-, m, s point in the figure represents the number of edges, and the number of the starting point of the CNR
Subsequently m rows, each row of three numbers u, v, w describe an undirected edges , the representative point v to the point u to have a free edge, the length w.
Next, a row number k, described CNR desired distance run.
Output
outputs a number representative of the number of different destinations.
Sample input
. 3. 3. 1
. 1 2 2
2. 3. 3
. 1. 4. 3
. 4
sample output
2
prompts
for 30% of data n, m≤100
to 100% of the data 0≤n, m≤100000, 0≤w≤1000, 1≤k≤10 ^ 9, 1≤u, v≤n
For example: 4 m CNR wishes to run, he ran directly No. 3 along a third edge node, this time running distance is 4. He can also start and the first edge 2 m, 2 m pitted second sides, stopped at an intermediate position of the edge of the second 2/3. Arguably the most short-circuit these two destinations are the No. 1 node 4

Solution:
destination only two emulated: (1) a vertex (2) in a certain way

So, let's find out by dijkstra shortest distance from the starting point to each vertex, if the distance is equal to the distance run, then the vertex is a destination; if the distance is less than the distance run, we first ran the shortest distance to the current vertex, vertex look at the current remaining paths whether emulated Composition (2)

n, m, s = map(int, input().split())
s = s - 1

# initialize the graph
g = [[2000 for j in range(n)] for i in range(n)]
for i in range(m):
    u, v, w = map(int, input().split())
    u, v = u - 1, v - 1
    g[u][v] = w
    g[v][u] = w
    
# dijkstra 
dis = g[s]
nn = len(dis)
flag = [0 for i in range(nn)]
flag[s] = 1
for i in range(nn - 1):
    mmin = 2000
    for j in range(nn):
        if flag[j] == 0 and dis[j] < mmin:
            mmin = dis[j]
            u = j
    flag[u] = 1
for v in range(nn):
    if flag[v] == 0 and g[u][v] < 2000:
        if dis[v] > dis[u] + g[u][v]:
            dis[v] = dis[u] + g[u][v]

miles = int(input())
res = []
for i in range(len(dis)):
    if dis[i] == miles:
        res.append(str(i))
    if dis[i] < miles:
        for j in range(len(g[i])):
            if g[i][j] != 2000 and g[i][j] + dis[i] > miles:
                tmp = str(i) + str(j)
                if tmp not in res:
                    res.append(tmp)
print(len(res))

Time limit: C / C ++ language 1000MS; other languages 3000MS
Memory Limit: C / C ++ language 65536KB; other languages 589824KB
subject description:
Now a total of n tasks can be completed. For each task, there are k sub-tasks can be done. I sub-task and the time it takes is ti. We can think of a sub-task requires time and this is only the first of several sub-tasks related subtasks, and this is not about which task belongs. That time i sub-task n tasks required are the same.
Each task can be completed only once, while each sub-task can only be completed once, can not repeat any task completed.
Every time you complete a mission you'll get sub p points, and when you complete a task k subtasks, you'll get extra points q, which means you'll get pk + q points.
Now you have a total of m time, you need to find the maximum score.
Input
of the first row of three integers n, k, m. (1≤n≤100), (1≤k≤100), (0≤m≤2e9)
a second row of two integers p, q. (1≤p, q≤100)
the third row of k represents an integer of time required for each subtask. (1≤ ti≤1e6)
output
the maximum score is output over a time m can be obtained.
Sample input
3. 8 2
3. 1
. 9 5
Sample Output
3
prompts
input sample
2 2 3
. 1 2
. 1. 1
Sample Output
5

Solution:
regarded as a knapsack problem, there is a little problem, is the size of the matrix dp - I subtasks for each task gave his party, when n and k values larger matrix will become very huge dp

n, k, m = map(int, input().split())
score = [[0 for j in range(m + 1)] for j in range(n * (k + 1) + 1)]
p, q = map(int, input().split())
v = []
for i in range(k):
    v.append(p)
v.append(k * p + q)
v.insert(0, 0)
w = list(map(int, input().split()))
k_w = 0
for i in w:
    k_w += i
w.append(k_w)
w.insert(0, 0)
res = 0

for i in range(1, n * (k + 1) + 1):
    for j in range(1, m + 1):
        ii = i % (k + 1) if i % ( k + 1) != 0 else k + 1
        if j >= w[ii]:
            score[i][j] = max(score[i - 1][j], score[i - 1][j - w[ii]] + v[ii])
            res = max(res, score[i][j])
        else:
            score[i][j] = score[i - 1][j]
            res = max(res, score[i][j])
print(res)  

Maximum rise substring configured
time limit: C / C ++ language 1000MS; Other Languages 3000MS
memory limit: C / C ++ language 65536 KB; Other Languages 589824KB
Title Description:
given a sequence of length n is a positive whole you need from to delete a positive integer, it is clear that you have a variety of ways to delete, you need to delete the sequence of positive integers after seeking the longest substring rise, will delete all the programs, the longest rise substring length is.
Gives rise longest substring defined herein: i.e., for a sequence in several successive positive integers such that a_ {i + 1}> a_i , claimed that several consecutive sub-sequence of integers rising substring in All rise substring, referred to as the longest length of the longest substring rise.
Input
input of the first line contains only a positive integer n, the length of the sequence given. (1 <= n <= 100000 )
The next line has n positive integer, i.e. this sequence, separated by a space. (1 <= a_i <= 100000 )
outputs
the output contains only a positive integer, the length of the longest substring rise after a digit is deleted.
Sample input
. 5
2 3 2. 1. 5
Sample Output
3

Solution:
Search method according to ascending trip traversing all substrings list maintains a list storing each substring ascending start position and end position, the size of the list 2, when the list in ascending two intermediate sub-strings of more than one spacer element when the first string is thrown; only just as a spacer element, the two sub-strings before and after it is determined whether the intermediate space by deleting elements constituting a substring longer ascending

def is_up(l):
    s, e, mmax, res = 0, 0, 0, []
    l.append('$')
    for i in range(1, len(l)):
        if l[i] == '$' or l[i - 1] >= l[i]:
            if s != e:
                res.append((s, e))
                mmax = max(mmax, e - s  + 1)
            if  len(res) == 2:  
                s_tmp, e_tmp = res.pop(0)
                if  e_tmp + 1 == s:
                    if l[e_tmp] >= l[s] and l[e_tmp] < l[s + 1]:
                        mmax = max(mmax, e_tmp - s_tmp + e - s +1)
            s = i
            e = s
        else:
            e = i
    return(mmax)

n = int(input())
l = list(map(int, input().split()))
mmax = is_up(l)
print(mmax)


Published 152 original articles · won praise 22 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_38204302/article/details/104984480