답안 #379716

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
379716 2021-03-19T05:02:05 Z wzy Džumbus (COCI19_dzumbus) C++11
50 / 110
317 ms 60652 KB
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, a, b) for(int i = b-1; i>=a ; i--)
#define trav(a, x) for(auto& a : x)
#define allin(a , x) for(auto a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef pair<ll,ll> pll;
typedef vector<string> vs;
typedef vector<pll> vpl;
typedef vector<int> vi;
std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
const int N = 1005;
const ll inf = (ll) 1e15;
int n , m , d[N] , vis[N] , sz[N];
vector<int> g[N];
vector< vl > dp[N];
ll cost[N];

void comb(vector<vl> &a , vector<vl> &b){
	vector<vl> c(sz(a) + sz(b) + 2, vl(3,inf));
	auto add = [&](int a , int b){
		if(a == 1){
			if(b == 1)
				return 2;
			else
				return (b > 0 ? 1 : 0);
		}
		if(a == 2){
			return (b == 1 ? 1 : 0);
		}
		return 0;
	};
	auto state = [&](int a , int b){
		if(a == 1){
			if(b > 0)
				return 2;
		}
		if(a == 2){
			if(b > 0)
				return 2;
		}
		return a;
	};
	for(int i = 0 ; i < sz(a) ; i ++){
		for(int j = 0 ; j < sz(b); j ++){
			for(int k = 0 ; k < 3; k ++){
				for(int w = 0 ; w < 3 ; w++){
					c[i+j+add(k,w)][state(k,w)] = min(c[i+j+add(k,w)][state(k,w)],a[i][k] + b[j][w]);
				}
			}
		}
	}
	swap(c,a);
}


void dfs(int x){
	sz[x] = 1;
	dp[x] = vector<vl>(1,vl(3,0)); 
	dp[x][0][1] = d[x];
	dp[x][0][2] = inf;
	for(auto w : g[x]){
		if(!vis[w]){
			vis[w] = vis[x];
			dfs(w);
			comb(dp[x] , dp[w]);
			sz[x] += sz[w];
		}
	}
}
int32_t main(){
	scanf("%d%d" , &n , &m);
	for(int i = 1 ; i <= n; i ++)
		scanf("%d" , &d[i]);
	for(int i = 1; i <= m; i ++){
		int u , v;
		scanf("%d%d" , &u , &v);
		g[u].push_back(v);
		g[v].push_back(u);
	}
	fill(cost,cost+N,inf);
	cost[0] = 0;
	for(int i = 1 ; i <= n; i ++){
		if(!vis[i]){
			vis[i] = i;
			dfs(i);
			for(int j = n; j >= 1; j --){
				for(int k = 0 ; k <= min(sz[i],j) ; k ++){
					cost[j] = min(cost[j] , cost[j-k] + min({dp[i][k][0] , dp[i][k][1] , dp[i][k][2]}));
				}
			}
			for(int j = 1; j <= n; j ++)
				cost[j] = min(cost[j] , cost[j+1]);
		}
	}
	vl p;
	for(int i = 0 ; i <= n; i ++){
		if(cost[i] >= inf)
			break;
		p.push_back(cost[i]);
	}	
	int q;
	scanf("%d" , &q);
	for(int _ = 0 ; _ < q; _ ++){
		ll s;
		scanf("%lld" , & s);
		auto ans = upper_bound(all(p) , s) - begin(p) - 1;
		printf("%d\n" , (int)ans);
	}
}	

Compilation message

dzumbus.cpp: In function 'int32_t main()':
dzumbus.cpp:80:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   80 |  scanf("%d%d" , &n , &m);
      |  ~~~~~^~~~~~~~~~~~~~~~~~
dzumbus.cpp:82:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   82 |   scanf("%d" , &d[i]);
      |   ~~~~~^~~~~~~~~~~~~~
dzumbus.cpp:85:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   85 |   scanf("%d%d" , &u , &v);
      |   ~~~~~^~~~~~~~~~~~~~~~~~
dzumbus.cpp:111:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  111 |  scanf("%d" , &q);
      |  ~~~~~^~~~~~~~~~~
dzumbus.cpp:114:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  114 |   scanf("%lld" , & s);
      |   ~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 249 ms 45676 KB Output is correct
2 Correct 283 ms 53228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 249 ms 45676 KB Output is correct
2 Correct 283 ms 53228 KB Output is correct
3 Correct 301 ms 60652 KB Output is correct
4 Correct 317 ms 46988 KB Output is correct
5 Correct 311 ms 44268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 2796 KB Output is correct
2 Runtime error 2 ms 748 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 249 ms 45676 KB Output is correct
2 Correct 283 ms 53228 KB Output is correct
3 Correct 301 ms 60652 KB Output is correct
4 Correct 317 ms 46988 KB Output is correct
5 Correct 311 ms 44268 KB Output is correct
6 Correct 54 ms 2796 KB Output is correct
7 Runtime error 2 ms 748 KB Execution killed with signal 11
8 Halted 0 ms 0 KB -