Hiển thị các bài đăng có nhãn greedy. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn greedy. Hiển thị tất cả bài đăng

Chủ Nhật, 6 tháng 1, 2019

Codeforces Round #529 (Div.3)- Bài 6

Bài 6: You are given an undirected graph consisting of $n$ vertices. A number is written on each vertex; the number on vertex $i$ is $a_i$. Initially there are no edges in the graph.

You may add some edges to this graph, but you have to pay for them. The cost of adding an edge between vertices $x$ and $y$ is $a_x+a_y$ coins. There are also $m$ special offers, each of them is denoted by three numbers $x,y$ and $w$, and means that you can add an edge connecting vertices $x$ and $y$ and pay $w$ coins for it. You don't have to use special offers; if there is a pair of vertices $x$ and $y$ that has a special offer associated with it, you still may connect these two vertices paying $a_x+a_y$ coins for it.

What is minimum number of coins you have to spend to make the graph connected? Recall that a graph is connected if it's possible to get from any vertex to any other vertex using only the edges belonging ti this graph.
Input
The first line contains two integers $n$ and $m(1\le n\le 2.10^5,0\le m\le 2.10^5)$- the number of vertices in the graph and the number of speacial offers, respectively.

The second line contains $n$ integers $a_1,a_2,...,a_n(1\le a_i\le 10^{12})$- the numbers written on the vertices.
Then $m$ lines follow, each containing three integers $x,y$ and $w(1\le x,y\le n,1\le w\le 10^{12},x\ne y)$ denoting a speacial offer: you may add an edge connecting vertex $x$ and vectex $y$, and this edge will cost $w$ coins.
Input
Print one integer- the minimum number of coins you have to pay to make the graph connected.
Examples
input
Copy
3 2
1 3 3
2 3 5
2 1 1
output
Copy
5
input
Copy
4 0
1 3 3 7
output
Copy
16
input
Copy
5 4
1 2 3 4 5
1 2 8
1 3 10
1 4 7
1 5 15
output
Copy
18
Solution:
#include<bits/stdc++.h>
#define int long long
using namespace std;const int N=2e5+7;
struct data{int u,v,w;}s[N*2];int n,m,i,j,k,a[N],fa[N],ans,cnt;
bool operator<(data a,data b){return a.w<b.w;}
int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
int32_t main(){
    for(k=1e13,cin>>n>>m,i=1;i<=n;++i){
        cin>>a[i];fa[i]=i;
        if(a[i]<k)k=a[i],j=i;
    }
    for(i=1;i<=n;++i)if(i!=j)s[++cnt]=data{i,j,a[i]+k};
    for(;m--;){cin>>i>>j>>k;s[++cnt]=data{i,j,k};}
    for(sort(s+1,s+cnt+1),i=1;i<=cnt;++i)if((j=find(s[i].u))!=(k=find(s[i].v)))fa[j]=k,ans+=s[i].w;
    cout<<ans<<endl;
}

Codeforces Round #529 (Div.3)- Bài 3

Bài 3: A positive integer $x$ is called a power of two if it can be represented as $x=2^y$, where $y$ is a non-negative integer. So, the powers of two are $1,2,4,...,16$.
You are given two positive integers $n$ and $k$. Your task is to represent $n$ as the sum of exactly $k$ powers of two.
Input
The only line of input contains two integers $n$ and $k(1\le n\le 10^9,1\le k\le 2.10^5)$.
Output
If it is impossible to represented $n$ as the sum of $k$ powers of two, print "NO"
Otherwise, print "YES", and then print $k$ positive integers $b_1,b_2,...,b_k$ such that each of $b_i$ is a power of two, and $\sum\limits_{i=1}^{k}b_i=n$. If there are multiple answers, you may print any of them.
Examples
input
Copy
9 4
output
Copy
YES
1 2 2 4 
input
Copy
8 1
output
Copy
YES
8 
input
Copy
5 1
output
Copy
NO
input
Copy
3 7
output
Copy
NO
Solution:
#include <bits/stdc++.h>
using namespace std;
int a[200001], n, m, k;
int main() {
 scanf("%d%d", &n, &k);
 if(__builtin_popcount(n)>k || n<k)
  return 0*puts("NO");
 for(int i=30; i>=0; i--)
  while(n && k && (n-(1<<i))>=k-1) {
   n-=1<<i;
   k--;
   a[m++]=1<<i;
  }
 puts("YES");
 while(m--) printf("%d ", a[m]);
}

Thứ Năm, 3 tháng 1, 2019

Codeforces Round 57- Bài 1

Bài 1: You are given a range of positive integers from $l$ to $r$
Find such a pair of integers $(x,y)$ that $l\le x,y\le r,x\ne y$ and $x$ divides $y$
If there are multiple answers, print any of them.
You are also asked to answer $T$ independent queries
Input
The first line contains a single integer $T(1\le T\le 1000)$- the number of queries. 
Each of the next $T$ lines contains two integers $l$ and $r(1\le l\le r\le 998244353)$ - inclusive borders of the range.
It is guaranteed that testset only includes queries, which have at least one suitable pair.
Output
Print $T$ lines, each line should contain the answer- two integer $x$ and $y$ such that $1\le x,y\le r,x\ne y$ and $x$ divides $y$. The answer in the $i-th$ lines should correspond to the $i-th$ query from the input
If there are multiple answers, print any of them.
Example
input
Copy
3
1 10
3 14
1 10
output
Copy
1 7
3 9
5 10
Solution:
#include<bits/stdc++.h>
using namespace std;
int main(){
    int t,l,r;
   cin>>t;
   while(t--){
    cin>>l>>r;
    cout<<l<<" "<<2*l<<'\n';
   }
   return 0;
}

Bài G - Educatioal Round 62

Đề bài: Bạn được cho 1 đồ thị vô hướng đặc biệt. Nó bao gồm $2n$ đỉnh được đánh số từ 1 đến 2n. Dưới đây là một số đặc tính của đồ thị: + ...