The Preliminary Contest for ICPC Asia Nanjing 2019

文章目录

  • [F - Greedy Sequence](https://nanti.jisuanke.com/t/41303)
  • [H - Holy Grail](https://nanti.jisuanke.com/t/41305)

F - Greedy Sequence

You’re given a permutation aa of length n ( 1 ≤ n ≤ 1 0 5 ) . n (1 \le n \le 10^5 ). n(1n105).

For each i ∈ [ 1 , n ] i \in [1,n] i[1,n], construct a sequence s i s_i si by the following rules:

  1. s i [ 1 ] = i s_i[1]=i si[1]=i;
  2. The length of s i s_i si is n n n, and for each j ∈ [ 2 , n ] j \in [2, n] j[2,n], s i [ j ] ≤ s i [ j − 1 ] s_i[j] \le s_i[j-1] si[j]si[j1]
  3. First, we must choose all the possible elements of s i s_i si from permutation aa. If the index of s i [ j ] s_i[j] si[j] in permutation a a a is p o s [ j ] pos[j] pos[j], for each j ≥ 2 j \ge 2 j2, ∣ p o s [ j ] − p o s [ j − 1 ] ∣ ≤ k ( 1 ≤ k ≤ 1 0 5 |pos[j]-pos[j-1]|\le k (1 \le k \le 10^5 pos[j]pos[j1]k(1k105). And for each s i s_i si , every element of s i s_i si must occur in a at most once.
  4. After we choose all possible elements for s i s_i si, if the length of s i s_i si is smaller than n n n, the value of every undetermined element of s i s_i si is 0 0 0;
  5. For each s i s_i si , we must make its weight high enough.

Consider two sequences C = [ c 1 , c 2 , . . . c n ] C = [c_1, c_2, ... c_n] C=[c1,c2,...cn] and D = [ d 1 , d 2 , . . . , d n ] D=[d_1, d_2, ..., d_n] D=[d1,d2,...,dn], we say the weight of C C C is higher than that of D D D if and only if there exists an integer k k k such that 1 ≤ k ≤ n , c i = d i 1 \le k \le n, c_i=d_i 1kn,ci=di for all 1 ≤ i < k 1 \le i < k 1i<k and c k > d k c_k > d_k ck>dk.

If for each i ∈ [ 1 , n ] i \in [1,n] i[1,n], c i = d i c_i=d_i ci=di , the weight of C C C is equal to the weight of D D D.

For each i ∈ [ 1 , n ] i \in [1,n] i[1,n], print the number of non-zero elements of s i s_i si separated by a space.

It’s guaranteed that there is only one possible answer.

Input
There are multiple test cases.

The first line contains one integer T ( 1 ≤ T ≤ 20 ) T(1 \le T \le 20) T(1T20), denoting the number of test cases.

Each test case contains two lines, the first line contains two integers nn and k ( 1 ≤ n , k ≤ 1 0 5 ) k (1 \le n,k \le 10^5) k(1n,k105), the second line contains nn distinct integers a 1 , a 2 , . . . , a n ( 1 ≤ a i ≤ n ) a_1, a_2, ..., a_n (1 \le a_i \le n) a1,a2,...,an(1ain) separated by a space, which is the permutation a a a.

Output
For each test case, print one line consists of nn integers ∣ s 1 ∣ , ∣ s 2 ∣ , . . . , ∣ s n ∣ |s_1|, |s_2|, ..., |s_n| s1,s2,...,sn separated by a space.

∣ s i ∣ |s_i| si is the number of non-zero elements of sequence s i s_i si.

There is no space at the end of the line.

样例输入

2
3 1
3 2 1
7 2
3 1 4 6 2 5 7

样例输出

1 2 3
1 1 2 3 2 3 3

有的人用的是主席树,我看好多暴力都过了,我也暴力

#include
using namespace std;
typedef long long ll;
const int MAXN = 1e5+7;
int s[MAXN];
int pos[MAXN];
int main() {
        // freopen("../in.txt","r",stdin);
        // freopen("../out.txt","w",stdout);
        int t,n,x,k;
        scanf("%d",&t);
        while(t--) {
                scanf("%d %d",&n,&k);
                for(int i=0;i<n;++i) {
                        cin>>x;
                        pos[x] = i;
                }
                for(int i=1;i<=n;++i)   s[i] = 1;
                for(int i=1;i<=n;++i)
                        for(int j=i-1;j>0;--j)
                                if(abs(pos[i]-pos[j])<=k) {
                                        s[i] += s[j];
                                        break;
                                }
                for(int i=1;i<=n;++i)
                        printf("%d%c",s[i],i==n?'\n':' ');

        }
        return 0;
}

H - Holy Grail

As the current heir of a wizarding family with a long history,unfortunately, you find yourself forced to participate in the cruel Holy Grail War which has a reincarnation of sixty years.However,fortunately,you summoned a Caster Servant with a powerful Noble Phantasm.When your servant launch her Noble Phantasm,it will construct a magic field,which is actually a directed graph consisting of n vertices and m edges.More specifically,the graph satisfies the following restrictions :

  • Does not have multiple edges(for each pair of vertices x and y, there is at most one edge between this pair of vertices in the graph) and does not have self-loops(edges connecting the vertex with itself).
  • May have negative-weighted edges.
  • Does not have a negative-weighted loop.
  • n<=300 , m<=500.

Currently,as your servant’s Master,as long as you add extra 6 edges to the graph,you will beat the other 6 masters to win the Holy Grail.

However,you are subject to the following restrictions when you add the edges to the graph:

  • Each time you add an edge whose cost is c,it will cost you c units of Magic Value.Therefore,you need to add an edge which has the lowest weight(it’s probably that you need to add an edge which has a negative weight).
  • Each time you add an edge to the graph,the graph must not have negative loops,otherwise you will be engulfed by the Holy Grail you summon.

Input
Input data contains multiple test cases. The first line of input contains integer t — the number of testcases ( 1 ≤ t ≤ 5 ) (1 \le t \le 5) (1t5).

For each test case,the first line contains two integers n,m,the number of vertices in the graph, the initial number of edges in the graph.

Then m lines follow, each line contains three integers x, y and w ( 0 ≤ x , y ≤ n , − 1 0 9 ≤ w ≤ 1 0 9 , x ! = y ) (0 \le x,y\le n, -10^9≤w≤10^9, x != y) (0x,yn,109w109,x!=y) denoting an edge from vertices x to y (0-indexed) of weight w.

Then 6 lines follow, each line contains two integers s,t denoting the starting vertex and the ending vertex of the edge you need to add to the graph.

It is guaranteed that there is not an edge starting from s to t before you add any edges and there must exists such an edge which has the lowest weight and satisfies the above restrictions, meaning the solution absolutely exists for each query.

Output
For each test case,output 6 lines.

Each line contains the weight of the edge you add to the graph.

样例输入
1
10 15
4 7 10
7 6 3
5 3 3
1 4 11
0 6 20
9 8 25
3 0 9
1 2 15
9 0 27
5 2 0
7 3 -5
1 7 21
5 0 1
9 3 16
1 8 4
4 1
0 3
6 9
2 1
8 7
0 4
样例输出
-11
-9
-45
-15
17
7

因为有负权,要用SPFA

6次SPFA
每次找到t到s的最短路之后,在图中加一条s到t的负边


#include
using namespace std;
typedef long long ll;
const int MAXN = 1e3+7;
const int INF = 0x3f3f3f3f;
struct node{
        int v,w;
        node(){}
        node(int _v,int _w):v(_v), w(_w){}
};

vector<node> G[MAXN];
int n,m,dis[MAXN];
bool vis[MAXN];
// int cont[MAXN];
int SPFA(int s,int t) {
        memset(dis,0x3f,sizeof(dis));
        memset(vis,0,sizeof(vis));
        // memset(cont,0,sizeof(cont));
        queue<int> Q;
        Q.push(s);
        dis[s] = 0;
        // ++cont[s];
        while(!Q.empty()) {
                int u = Q.front(); Q.pop();
                vis[u] = false;
                for(int i=0;i<(int)G[u].size();++i) {

                        int v = G[u][i].v;
                        int w = G[u][i].w;
                        // cout<
                        if(dis[v]>dis[u]+w) {
                                dis[v] = dis[u] + w;
                                if(!vis[v]) {
                                        Q.push(v);
                                        vis[v] = true;
                                }
				// if(++cont[v] > n)//有负环
				// 	return -1;

                        }
                }
        }
        return dis[t];
}

int main() {
        // freopen("../in.txt","r",stdin);
        // freopen("../out.txt","w",stdout);
        int t;
        scanf("%d",&t);
        while(t--) {
                node e;
                scanf("%d %d",&n,&m);
                for(int i=0;i<=n;++i)   G[i].clear();
                for(int i=0;i<m;++i) {
                        int x,y,z;
                        scanf("%d %d %d",&x,&y,&z);
                        e.v = y;
                        e.w = z;
                        G[x].push_back(e);
                }
                for(int i=0;i<6;++i) {
                        int s,t;
                        scanf("%d %d",&s,&t);
                        int ans = -SPFA(t,s);
                        printf("%d\n",ans);
                        e.v = t;
                        e.w = ans;
                        G[s].push_back(e);
                }
        }
        return 0;
}

你可能感兴趣的:(最短路,思维,SPFA)