【NOIP專題】NOIP2018

NOIP2018

評價:這年的NOIP感覺質量很屑,完全是在亂搞,聽說Day2T2是因爲漏題的原因直接換成了清華校內集訓的T1。分明就是我太菜了

T1

題面

解答

水題,做個差分,把正的加起來就行了。

//
// Created by dhy on 18-11-10.
//
#include <cstdio>
#include <cstdlib>
const int MAXN = 100010;
int read(){
    int x = 0,f = 1;
    char c = getchar();
    while(c>'9'||c<'0'){ if(c=='-')f = -1;c = getchar(); }
    while(c>='0'&&c<='9'){ x = (x<<1)+(x<<3)+c-'0';c = getchar();}
    return x*f;
}
int a[MAXN];
int d[MAXN];
int main(){
    int n = read();
    for(int i = 1;i<=n;i++){
        a[i] = read();
        d[i] = a[i]-a[i-1];
    }
    int ans = 0;
    for(int i = 1;i<=n;i++){
        if(d[i]>0)ans+=d[i];
    }
    printf("%d\n",ans);
    return 0;
}

T2

題面

解答

揹包,排個序,然後嘗試用小的更新大的。
代碼:

//
// Created by dhy on 18-11-10.
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
using namespace std;
int read(){
    int x = 0,f = 1;
    char c = getchar();
    while(c>'9'||c<'0'){ if(c=='-')f = -1;c = getchar(); }
    while(c>='0'&&c<='9'){ x = (x<<1)+(x<<3)+c-'0';c = getchar();}
    return x*f;
}
const int MAXVAL = 25010;
int a[MAXVAL];
bool f[MAXVAL];
int main(){
    int T = read();
    while(T--){
    	memset(f,false,sizeof(f));
        int n = read();
        int ans = n;
        for(int i = 1;i<=n;i++){
            a[i] = read();
        }
        sort(a+1,a+1+n);
        f[0] = true;
        for(int i = 1;i<=n;i++){
        	if(f[a[i]]){
        		ans--;
        		continue;
			}
			for(int j = a[i];j<=a[n];j++)f[j] = f[j]|f[j-a[i]];
		}
        printf("%d\n",ans);
    }
    return 0;
}

T3

題面

解答

首先不難看出是個二分加樹上的一個操作。考慮如何判斷解是否可行。記f[i]f[i]表示節點ii的子樹中,滿足條件的路徑有多少種,g[i]g[i]表示節點ii的子樹中,最長的一條鏈的長度,然後對於每個節點,直接合並子樹中的ff然後把gg全部丟到multiset裏面,每次取最小的,再找一個剛好可以拼湊成midmid的值拼湊,更新ff
代碼:

// luogu-judger-enable-o2
#include <iostream>
#include <cstring>
#include <cstdio>
#include <set>
#include <algorithm>
#define clean(x) memset(x,0,sizeof(x))
using namespace std;
const int MAXN = 101000;
struct edge{int t,w,next;}edges[MAXN<<1];
int head[MAXN],top;
void add(int f,int t,int w){
	edges[++top].next = head[f];
	edges[top].t = t;
	edges[top].w = w;
	head[f] = top;
}
int f[MAXN],g[MAXN];
multiset<int> st[MAXN];
int n,m;
void dfs(int x,int fa,int mid){
	st[x].clear();f[x] = 0,g[x] = 0;
	for(int i = head[x];i;i = edges[i].next){
		int t = edges[i].t;
		if(t==fa)continue;
		dfs(t,x,mid);
		g[x]+=g[t];
		int v = f[t]+edges[i].w;
		if(v>=mid)g[x]++;
		else st[x].insert(v);
	}
	while(!st[x].empty()){
		if(st[x].size()==1){
			f[x] = max(f[x],*st[x].begin());
			break;
		}else{
			multiset<int>::iterator it;
			it = st[x].lower_bound(mid-*st[x].begin());
			if(it==st[x].begin()&&st[x].count(*it)==1)it++;
			if(it==st[x].end()){
				f[x] = max(f[x],*st[x].begin());
				st[x].erase(st[x].find(*st[x].begin()));
			}else{
				g[x]++;
				st[x].erase(st[x].find(*it));
				st[x].erase(st[x].find(*st[x].begin()));
			}
		}
	}
}
bool check(int mid){
	dfs(1,1,mid);
	return g[1]>=m;
}
int main(){
	scanf("%d%d",&n,&m);
	int f,t,w;
	int sum = 0;
	for(int i = 1;i<n;i++){
		scanf("%d%d%d",&f,&t,&w);
		add(f,t,w);add(t,f,w);
		sum+=w;
	}
	int l = 0,r = sum;
	while(l<r){
		int mid = l+r+1>>1;
		if(check(mid))l = mid;
		else r = mid-1;
	}
	printf("%d\n",l);
	return 0;
}

T4

題面

解答

暴力枚舉斷一條邊,然後貪心做就好了
代碼:

// luogu-judger-enable-o2
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>

using namespace std;
vector<int> G[5010];
int vis[5010];
namespace tree{
    vector<int> ans;
    void dfs(int x){
        vis[x] = true;
        ans.push_back(x);
        for(int i = 0;i<G[x].size();i++){
            if(!vis[G[x][i]])dfs(G[x][i]);
        }
    }
    void main(){
        dfs(1);
        for(int i = 0;i<ans.size();i++)cout<<ans[i]<<' ';
    }
}
int n,m;
int edge[5010][2];
namespace ringBasedTree{
    vector<int> ans,temp;
    int del_f,del_t;
    bool chect(int u,int v){
        if((u==del_f&&v==del_t)||(u==del_t&&v==del_f)){
            return false;
        }
        return true;
    }
    void dfs(int x){
        vis[x] = true;
        temp.push_back(x);
        for(int i = 0;i<G[x].size();i++){
            if(chect(x,G[x][i])&&!vis[G[x][i]]){
                dfs(G[x][i]);
            }
        }
    }
    void main(){
        for(int i = 1;i<=n;i++)ans.push_back(9999);
        for(int i = 1;i<=m;i++){
                memset(vis,false, sizeof(vis));
                del_f = edge[i][0],del_t = edge[i][1];
                temp.clear();
                dfs(1);
                if(temp<ans&&temp.size()==n)ans = temp;
        }
        for(int i = 0;i<ans.size();i++)cout<<ans[i]<<' ';
    }
}
int main(void){
    cin>>n>>m;
    int x,y;
    for(int i = 1;i<=m;i++){
        cin>>x>>y;
        edge[i][0] = x;
        edge[i][1] = y;
        G[x].push_back(y),G[y].push_back(x);
    }
    for(int i = 1;i<=n;i++)sort(G[i].begin(),G[i].end());
    if(m==n-1){
        tree::main();
    }else{
        ringBasedTree::main();
    }
    return 0;
}

T5

題面
我不會,據說可以dfs套dfs找規律得50分

T6

題面
我不會,DDP板子題

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章