2019 南京网络赛

时间:2019-09-03
本文章向大家介绍2019 南京网络赛,主要包括2019 南京网络赛使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

 F、Greedy Sequence

You're given a permutation aa of length nn (1 \le n \le 10^51n105).

For each i \in [1,n]i[1,n], construct a sequence s_isi by the following rules:

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

Consider two sequences C = [c_1, c_2, ... c_n]C=[c1,c2,...cn] and D=[d_1, d_2, ..., d_n]D=[d1,d2,...,dn], we say the weight of CC is higher thanthat of DD if and only if there exists an integer kk such that 1 \le k \le n1kn, c_i=d_ici=di for all 1 \le i < k1i<k, and c_k > d_kck>dk.

If for each i \in [1,n]i[1,n], c_i=d_ici=di, the weight of CC is equal to the weight of DD.

For each i \in [1,n]i[1,n], print the number of non-zero elements of s_isi 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 \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 kk (1 \le n,k \le 10^51n,k105), the second line contains nn distinct integers a_1, a_2, ..., a_na1,a2,...,an (1 \le a_i \le n1ain) separated by a space, which is the permutation aa.

Output

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

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

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

题意:给n个数和一个数k,找n个符合条件的si序列,问si序列中非0元素的值的个数

条件:si序列的第一个值是 i;从i开始找小于i的数x,  且位置满足   i_pos-k<=x_pos<=i_pos+k;保证字典序最大,依次往下找直到结束

题解:暴力+排序

#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<math.h>
#include<string>
#include<string.h>
#include<vector>
#include<utility>
#include<map>
#include<queue>
#include<set>
#define mx 0x3f3f3f3f
#define ll long long
#define MAXN 100
using namespace std;
string s,ss;
int a[100005],b[100005];
int n,k,t;
struct node 
{
    int val;
    int pos;
}p[100005];
bool cmp(node x,node y)
{
    return x.val<y.val;
}
int find(int x)
{
    int ans=1,len=1;
    while(1)
    {
        if(x<=1)
            break;
        else if(x-len>=1&&abs(a[p[x-len].val]-a[p[x].val])<=k)
        {
            if(b[x-len]!=0)//直接使用前面处理过的数据,避免超时
            {
                ans=ans+b[x-len];
                return ans;
            }
            ans++;
            x=x-len;
            len=1;
            continue;
        }
        else if(x-len<1)
            break;
        len++;
    }
    return ans;
}
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n>>k;
        for(int i=1;i<=n;i++)
        {
            cin>>p[i].val;
            p[i].pos=i;
            a[p[i].val]=i;
        }
        sort(p+1,p+n+1,cmp);
        for(int i=1;i<=n;i++)
            b[i]=find(i);
        for(int i=1;b[i];i++)
        {
            if(i==1)
                cout<<b[i];
            else
                cout<<' '<<b[i];
        }
        cout<<endl;
    }
    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 \le t \le 51t5).

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 \le x,y<n0x,y<n,-10^9109≤w≤10^9109, x \not = yx=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 66 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

题意:直接求yx的最短路,如果为正输出负,为负输出正,然后再加上x到y的边

#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<queue>
#define ll long long
#define mx 0x3f3f3f3f
using namespace std;
int way[520][520],dis[520],vis[520],cnt[520];
//way记录路径关系,dis[i]记录起点到点j的最近距离,vis[i]标记点是否在队列中,cnt[i]记录点i进入队列的次数
int n,m;
void init()
{
    for(int i=0;i<n;i++)//先初始化way
    {
        for(int j=0;j<n;j++)
        {
            if(i==j)
                way[i][j]=0;
            else
                way[i][j]=mx;
        }
    }
}

void spfa(int st)
{
    for(int i=0;i<n;i++)//这里点编号是从0开始的
        dis[i]=mx;
    memset(vis,0,sizeof(vis));
    memset(cnt,0,sizeof(cnt));
    vis[st]=1;
    cnt[st]=1;
    dis[st]=0;
    queue<int>p;
    p.push(st);
    while(!p.empty())
    {
        int now=p.front();
        p.pop();
        vis[now]=0;
        for(int i=0;i<n;i++)
        {
            if(dis[now]+way[now][i]<dis[i])
            {
                dis[i]=dis[now]+way[now][i];
                if(vis[i]==0)//如果点不在队列里面
                {
                    p.push(i);
                    vis[i]=1;
                    cnt[i]++;
                    if(cnt[i]>n)//如果这个点加入超过n次,说明存在负圈,直接返回 
                        return ;
                }
            }
        }
    }

}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        init();
        for(int i=0;i<m;i++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            way[x][y]=z;
        }
        for(int i=0;i<6;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            spfa(y);//求y到x的最短路
            printf("%d\n",-1*dis[x]);
            way[x][y]=-1*dis[x];//加边
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/-citywall123/p/11453164.html