test20190803 training camp NOIP 19

60+100+0=160

Greed continent

The face of crazy ants attack, small FF's Tower defence failed ...... humans are the ants into just a bay on the Greed Island. Now, rearwardly FF is the ocean and is a variation of the front super ants. There are small FF promising future, he did not want to loss of life here, so he sent his men last batch transformation SCV arranged mines to block the ants attack.

Small FF last line of defense is a length N trenches, has numerous small FF various mines, SCV and the laying of each segment can be the same which is different from before the laying of the mines already in [L, R]. Since the case has been Urgent, small FF at some point may ask how many different mines you have in the [L ', R'] range, he wants you to reply as soon as possible.

Input format:
The first two acts of integers m and n; n represents a line of length, m denotes the total number of times and a small mine SCV FF inquiry.
Then there are m rows, each row of three integer Q, L, R; Q = 1 indicates that if the SCV [L, R] of this interval mines one kind of cloth, if Q = 2, said small current query FF [ L, R] range there are many kinds of mines.

Output formats:
For small FF every inquiry, an answer output (single line), indicates that the current range of the total number of mines.

Sample input:
. 5. 4
. 1. 1. 3
2 2. 5
. 1. 4 2
2. 3. 5

Sample Output:
. 1
2

Data range:
For 30% of the data: 0 <= n, m < = 1000;
100% data: 0 <= n, m < = 10 ^ 5.

Time limit:
lS
space constraints:
50M

answer

Look at the number of problems found within the endpoint answer = interval - the number of segments contained within the interval + contains the number of segments range.

Like the first block, the left end of the right end block sort out out a \ (O (n \ sqrt { n} \ log n) \) approach, abandoned.

Then find a balanced tree directly sets the tree line can do, find out the code set iterator does not support subtraction.

So I went to school __gnu_pbds :: tree, engage in a long time and finally right. Time complexity \ (O (n-\ log ^ 2) \) , 60 minutes. Listen to people say that the toilet block algorithm, 80 ......

#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define pbds __gnu_pbds
#define co const
#define il inline
template<class T> T read(){
    T x=0,w=1;char c=getchar();
    for(;!isdigit(c);c=getchar())if(c=='-') w=-w;
    for(;isdigit(c);c=getchar()) x=x*10+c-'0';
    return x*w;
}
template<class T>il T read(T&x){
    return x=read<T>();
}
typedef pbds::tree<std::pair<int,int>,pbds::null_type,std::less<std::pair<int,int> >,pbds::rb_tree_tag,pbds::tree_order_statistics_node_update> tree;

co int N=100000+1,INF=1e9;
#define lc (x<<1)
#define rc (x<<1|1)
int n,m;
tree ls,rs,s[N<<2];
void insert(int x,int l,int r,int p,int v,int id){
    s[x].insert(std::make_pair(v,id));
    if(l==r) return;
    int mid=(l+r)>>1;
    if(p<=mid) insert(lc,l,mid,p,v,id);
    else insert(rc,mid+1,r,p,v,id);
}
int qin(int x,int l,int r,int ql,int qr){
    if(ql<=l&&r<=qr) return s[x].order_of_key(std::make_pair(qr,INF))-s[x].order_of_key(std::make_pair(ql,-INF));
    int mid=(l+r)>>1;
    if(qr<=mid) return qin(lc,l,mid,ql,qr);
    if(ql>mid) return qin(rc,mid+1,r,ql,qr);
    return qin(lc,l,mid,ql,qr)+qin(rc,mid+1,r,ql,qr);
}
int qout(int x,int l,int r,int ql,int qr){
    if(r<ql) return s[x].order_of_key(std::make_pair(INF,INF))-s[x].order_of_key(std::make_pair(qr,INF));
    int mid=(l+r)>>1;
    if(ql<=mid+1) return qout(lc,l,mid,ql,qr);
    return qout(lc,l,mid,ql,qr)+qout(rc,mid+1,r,ql,qr);
}
int main(){
//  freopen("testdata.in","r",stdin),freopen("testdata.ans","w",stdout);
    read(n),read(m);
    for(int i=1;i<=m;++i){
        int q=read<int>(),l=read<int>(),r=read<int>();
        if(q==1){
            ls.insert(std::make_pair(l,i)),rs.insert(std::make_pair(r,i));
            insert(1,1,n,l,r,i);
        }
        else{
            int ans=0;
            ans+=ls.order_of_key(std::make_pair(r,INF))-ls.order_of_key(std::make_pair(l,-INF));
            ans+=rs.order_of_key(std::make_pair(r,INF))-rs.order_of_key(std::make_pair(l,-INF));
            ans-=qin(1,1,n,l,r);
            ans+=qout(1,1,n,l,r);
            printf("%d\n",ans);
        }
    }
    return 0;
}

Correct answer is Fenwick tree, we only need each buried mines, we only need to mark the beginning and at the end of the current range, we check, we only need to check 1 to the end of the current range contains the number at the beginning (a represents the beginning of a landmine) and then we checked before the interval 1 to the current position contains a number at the end (the end on behalf of a us a landmine buried over, what we see in front of several mines were buried over!) then do this poor, we can know how many mines are present in the current interval. Time complexity \ (O (n \ log n ) \)

Multiplayer backpack

DD and friends going to climb it! They are a total of K individuals, everyone will carry a package. Capacity of these packages is the same, is V. Can be put into the backpack of a total of N kinds of items, each item has a given volume and value.
In DD it seems reasonable arrangements for the backpack program is as follows:

  1. Each backpack filled with exactly equal to the total volume of the article pack capacity.
  2. At most one, but two different packages may be present in the same article for each item of each bag.
  3. Any two people, a list of items they can not be exactly the same bag.

Under the premise of meeting the above requirements, the total value of all goods of all bag the biggest is how much?

Input format:
The first line contains three integers: K, V, N.
Start a second N rows, each row having two integers, and the value representing the volume of this article.

Output format:
only output an integer, i.e., the maximum value of the total value of all items on the premise of satisfying the above requirements.

Sample input:
2 10. 5
. 3 12 is
. 7 20 is
2. 4
. 5. 6
. 1. 1

Sample output:
57

Data range:
the total number of K <= 50.
Each knapsack capacity V <= 5000.
The number of types of goods N <= 200.
Other positive integer not more than 5,000.
The input data to ensure that satisfies the requirements of the program.

Time limit:
1S
space restrictions:
128M

answer

I found that I would not expand the issues backpack ...... This is a k-optimal solution backpack.

F (i, j) represents the volume i, j optimal solution. And then transferred to two-hand scan.

#include<bits/stdc++.h>
#define co const
#define il inline
template<class T> T read() {
    T x=0,w=1;
    char c=getchar();
    for(; !isdigit(c); c=getchar())if(c=='-') w=-w;
    for(; isdigit(c); c=getchar()) x=x*10+c-'0';
    return x*w;
}
template<class T>il T read(T&x) {
    return x=read<T>();
}
using namespace std;
int k,v,n,ans,cnt,now[55];
int V[288],W[288],f[5008][55];
int main() {
    read(k),read(v),read(n);
    for(int i=0; i<=5000; i++)
        for(int j=0; j<=50; j++)f[i][j]=-20021003;
    f[0][1]=0;
    for(int i=1; i<=n; i++)
        read(V[i]),read(W[i]);
    for(int i=1; i<=n; i++)
        for(int j=v; j>=V[i]; j--) {
            int c1=1,c2=1,cnt=0;
            while(cnt<=k) {
                if(f[j][c1]>f[j-V[i]][c2]+W[i])
                    now[++cnt]=f[j][c1++];
                else now[++cnt]=f[j-V[i]][c2++]+W[i];
            }
            for(int c=1; c<=k; c++) f[j][c]=now[c];
        }
    for(int i=1; i<=k; i++) ans+=f[v][i];
    printf("%d",ans);
}

The new magic potion

DD MM magician wants to send a birthday present, but he did not have enough gold. Magic skilled DD naturally think of clever use their skills to Potions formulated earn some gold coins.
DD total of N types know potion (1..N in number), also master M method (in No. 1..M) formulated potion. The method of preparation of each potion his disposal are simply expressed as follows: several potions each bottle was poured into a crucible, wand while applying a stirring specific magic, and then concentrated under properly, you can get a the new bottle of potion.
There is a magic shop in the forest, where not only sell all kinds of potions, but also a slightly lower price than the price of the acquisition of various potions. DD's wishful thinking is this: first of all with their own V she saved coins to the store to buy some magic potion as a raw material, and one day at home trying to preparation, the preparation of the final finished products and then sold to the magic shop.
However, due to the magic repair, DD can only be applied once the K magic in one day.
DD wants you to help him do the math, how much gold he can make the most of it in the day time?

Input format:
The first line has four integers: N, M, V, K .
Start a second N rows, each row having two integers, the row i + 1 of two integers indicate the purchase price and the selling price of the i-th potion.
M rows of N + 2 row begins, each row having a plurality of integers, represents one potion DD known formulation method. Format each row is this: The first potion integer such method of preparation can be finished potion bottle number. Next integer n (n <N), this method of preparation requires n represents bottle material. Here n integer, n is the number of bottles which feedstock.

Output formats:
Just output an integer that represents the largest number of gold coins can be earned.

Sample input:
. 4 2. 6. 3
. 1 0
. 1 0
. 5. 3
20 is 15
. 3 2. 1 2
. 4. 3. 1 2. 3

Sample output:
12

Data range:
syrup type N <= 60.
The method of formulation number M <= 240.
The initial number of coins V <= 1000.
Per day may be administered to the magic number K <= 30.

Time limit:
1S
space restrictions:
128M

answer

I think the state, but no time to write. f (i, j) represents the i all items with a minimum cost at most j times magic. Then I do not know how transfer, came up with a most shorted.

First gold magic number to use, and pieces of magic, so transferred the job.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
int n,m,V,cnt;
int tmp[61][31];//表示当前处理到i次用了j魔法的最低费用
int f[61][31];//表示第i件物品用j次魔法组成的最低费用
//用tmp更新f
int dp[1001][31];//表示当前花费i元用了j次魔法的最大收益
//用dp[i][j]-i更新答案 
int v[61];//物品的售价 
int id[250],sum[250];//每种魔法的成品和需要的原材料个数
vector<int>s[250];//每种魔法所需的原料是什么 
int main()
{
    memset(f,63,sizeof(f)); 
    scanf("%d%d%d%d",&n,&m,&V,&cnt);
    for(int i=1;i<=n;i++) scanf("%d%d",&f[i][0],&v[i]);
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&id[i],&sum[i]);
        for(int j=1;j<=sum[i];j++)
        {
            int x; scanf("%d",&x);
            s[i].push_back(x);
        }
    }
    for(int i=1;i<=cnt;i++)
        for(int j=1;j<=m;j++)
        {
            memset(tmp,63,sizeof(tmp));
            tmp[0][0]=0;
            int T=i-1;
            for(int k=0;k<=T;k++)
                for(int l=1;l<=sum[j];l++)
                {
                    int x=s[j][l-1];
                    for(int a=0;a<=k;a++)
                        tmp[l][k]=min(tmp[l][k],tmp[l-1][k-a]+f[x][a]);
                }
            f[id[j]][i]=min(f[id[j]][i],tmp[sum[j]][T]);
            for(int k=i;k<=cnt;k++) f[id[j]][k]=min(f[id[j]][k],f[id[j]][i]);
        } 
    for(int i=1;i<=V;i++)
        for(int j=0;j<=cnt;j++)
        {
            for(int k=1;k<=n;k++)
                for(int l=0;l<=j;l++)
                    if(f[k][l]<=i)
                        dp[i][j]=max(dp[i][j],dp[i-f[k][l]][j-l]+v[k]-f[k][l]);
        }
    printf("%d",dp[V][cnt]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/autoint/p/test20190803.html