<!DOCTYPE html>
	<html lang="vi" xmlns="http://www.w3.org/1999/xhtml" prefix="og: http://ogp.me/ns#">
	<head>
<title>Khởi nghiệp | Đề thi HSG tin 12 Nghệ An | năm học 2022 - 2023</title>
<meta name="description" content="Khởi nghiệp | Đề thi HSG tin 12 Nghệ An | năm học 2022 - 2023 - Savefile - Tin tức -...">
<meta name="author" content="BÀI TẬP Ở NHÀ">
<meta name="copyright" content="BÀI TẬP Ở NHÀ [ducluu80@gmail.com]">
<meta name="robots" content="index, archive, follow, noodp">
<meta name="googlebot" content="index, archive, follow, noodp">
<meta name="msnbot" content="all,index,follow">
<meta name="generator" content="NukeViet v4.5">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="content-language" content="vi">
<meta property="og:title" content="Khởi nghiệp | Đề thi HSG tin 12 Nghệ An | năm học 2022 - 2023">
<meta property="og:type" content="website">
<meta property="og:description" content="Savefile - Tin tức - https&#x3A;&#x002F;&#x002F;baitaponha.com&#x002F;savefile&#x002F;giai-de-tin-hoc&#x002F;cau-4-khoi-nghiep-de-thi-hsg-tinh-lop-12-nam-hoc-2022-2023-35.html">
<meta property="og:site_name" content="BÀI TẬP Ở NHÀ">
<meta property="og:url" content="https://baitaponha.com/savefile/giai-de-tin-hoc/cau-4-khoi-nghiep-de-thi-hsg-tinh-lop-12-nam-hoc-2022-2023-35.html">
<link rel="shortcut icon" href="https://baitaponha.com/uploads/logo.ico">
<link rel="canonical" href="https://baitaponha.com/savefile/giai-de-tin-hoc/cau-4-khoi-nghiep-de-thi-hsg-tinh-lop-12-nam-hoc-2022-2023-35.html">
<link rel="alternate" href="https://baitaponha.com/rss/" title="Tin tức" type="application/rss+xml">
<link rel="alternate" href="https://baitaponha.com/rss/lap-trinh-python-co-ban/" title="Tin tức - Lập trình Python cơ bản." type="application/rss+xml">
<link rel="alternate" href="https://baitaponha.com/rss/giai-de-tin-hoc/" title="Tin tức - Giải đề tin học" type="application/rss+xml">
<link rel="alternate" href="https://baitaponha.com/rss/boi-gioi-tin-hoc/" title="Tin tức - Bồi giỏi tin học" type="application/rss+xml">
<link rel="alternate" href="https://baitaponha.com/rss/lap-trinh-c/" title="Tin tức - Lập trình C++" type="application/rss+xml">
<link rel="alternate" href="https://baitaponha.com/rss/thu-thuat-may-tinh/" title="Tin tức - Thủ thuật máy tính" type="application/rss+xml">
<link rel="preload" as="style" href="https://baitaponha.com/assets/css/font-awesome.min.css" type="text/css">
<link rel="preload" as="style" href="https://baitaponha.com/themes/egov/css/bootstrap.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://baitaponha.com/themes/egov/css/style.css" type="text/css">
<link rel="preload" as="style" href="https://baitaponha.com/themes/egov/css/style.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://baitaponha.com/themes/egov/css/custom.css" type="text/css">
<link rel="preload" as="style" href="https://baitaponha.com/themes/egov/css/style-green.css" type="text/css">
<link rel="preload" as="style" href="https://baitaponha.com/themes/egov/css/news.css" type="text/css">
<link rel="preload" as="script" href="https://baitaponha.com/assets/js/jquery/jquery.min.js" type="text/javascript">
<link rel="preload" as="script" href="https://baitaponha.com/assets/js/language/vi.js" type="text/javascript">
<link rel="preload" as="script" href="https://baitaponha.com/assets/js/DOMPurify/purify3.js" type="text/javascript">
<link rel="preload" as="script" href="https://baitaponha.com/assets/js/global.js" type="text/javascript">
<link rel="preload" as="script" href="https://baitaponha.com/assets/js/site.js" type="text/javascript">
<link rel="preload" as="script" href="https://baitaponha.com/themes/default/js/news.js" type="text/javascript">
<link rel="preload" as="script" href="https://baitaponha.com/assets/js/jquery/jquery.cookie.js" type="text/javascript">
<link rel="preload" as="script" href="https://baitaponha.com/themes/egov/js/main.js" type="text/javascript">
<link rel="preload" as="script" href="https://baitaponha.com/themes/egov/js/custom.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.googletagmanager.com/gtag/js?id=G-4JBZJ8SEPL" type="text/javascript">
<link rel="preload" as="script" href="https://baitaponha.com/themes/egov/js/bootstrap.min.js" type="text/javascript">
<link rel="StyleSheet" href="https://baitaponha.com/assets/css/font-awesome.min.css">
<link rel="StyleSheet" href="https://baitaponha.com/themes/egov/css/bootstrap.non-responsive.css">
<link rel="StyleSheet" href="https://baitaponha.com/themes/egov/css/style.css">
<link rel="StyleSheet" href="https://baitaponha.com/themes/egov/css/style.non-responsive.css">
<link rel="StyleSheet" href="https://baitaponha.com/themes/egov/css/custom.css">
<link rel="StyleSheet" href="https://baitaponha.com/themes/egov/css/style-green.css">
<link rel="StyleSheet" href="https://baitaponha.com/themes/egov/css/news.css">
<style type="text/css">
	body{background: #fff;}
</style>

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3247389617576546"
     crossorigin="anonymous"></script>
     <!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-4JBZJ8SEPL"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-4JBZJ8SEPL');
</script>
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-W4C9RPT');</script>
<!-- End Google Tag Manager -->
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-6V0WCB5SCB"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-6V0WCB5SCB');
</script>
<script async custom-element="amp-auto-ads"
        src="https://cdn.ampproject.org/v0/amp-auto-ads-0.1.js">
</script>		
<script async custom-element="amp-ad" src="https://cdn.ampproject.org/v0/amp-ad-0.1.js"></script>

<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-262364265-1"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-262364265-1');
</script>
<script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-262364265-1', '.baitaponha.com');
ga('send', 'pageview');
</script>
</head>
	<body>
	    <amp-auto-ads type="adsense"
                    data-ad-client="ca-pub-3247389617576546">
        </amp-auto-ads>
<!-- BEGIN Fanpage Facebook -->
<div id="fb-root"></div>
<script async defer crossorigin="anonymous" src="https://connect.facebook.net/vi_VN/sdk.js#xfbml=1&version=v16.0&appId=390459604647856&autoLogAppEvents=1" nonce="H8AT1lQt"></script>

<!-- END Fanpage Facebook -->
        <!-- Google Tag Manager (noscript) -->
            <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-W4C9RPT"
            height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
        <!-- End Google Tag Manager (noscript) -->
<div id="print">
	<div id="hd_print">
		<h2 class="pull-left">BÀI TẬP Ở NHÀ</h2>
		<p class="pull-right"><a title="BÀI TẬP Ở NHÀ" href="https://baitaponha.com/">https://baitaponha.com</a></p>
	</div>
	<div class="clear"></div>
	<hr />
	<div id="content">
		<h1>Khởi nghiệp | Đề thi HSG tin 12 Nghệ An | năm học 2022 - 2023</h1>
		<ul class="list-inline">
			<li>Thứ bảy - 06/05/2023 04:25</li>
			<li class="hidden-print txtrequired"><em class="fa fa-print">&nbsp;</em><a title="In ra" href="javascript:;" onclick="window.print()">In ra</a></li>
			<li class="hidden-print txtrequired"><em class="fa fa-power-off">&nbsp;</em><a title="Đóng cửa sổ này" href="javascript:;" onclick="window.close()">Đóng cửa sổ này</a></li>
		</ul>
		<div class="clear"></div>
		<div id="hometext">
			<p><strong>Câu 4. Khởi nghiệp - Đề thi HSG tin 12 tỉnh Nghệ An năm 2022-2023</strong></p>
Đức là vừa tốt nghiệp đại học loại xuất sắc chuyên ngành Công Nghệ Thông tin tại một trường đại học danh tiếng. Đức đã tìm hiểu, lên kế hoạch khởi nghiệp từ thời đang là sinh viên và nay là thời điểm mà Đức sẽ thực hiện kế hoạch đó. Qua tìm hiểu, Đức biết được n công ty tiềm năng và có liên quan đến công việc làm của mình nên sẽ hợp tác làm việc với n công ty này. Các công ty được đánh số thứ tự lần lượt là 1, 2, 3, ....n. Điều kiện để hợp tác với công ty thứ i (i= 1, 2, ...,n) là: Đức đã hợp tác được với ít nhất a<sub>i</sub> công ty khác (trong n − 1 công ty còn lại) hoặc là mua một món quà tinh thân có giá trị b<sub>i</sub> (đồng) để tặng cho công ty thứ i.
		</div>
				<div class="imghome">
			<img alt="KHỞI NGHIỆP" src="https://baitaponha.com/uploads/news/2023_05/anh-chup-man-hinh-2023-05-06-152656.png" width="460" class="img-thumbnail" />
		</div>
		<div class="clear"></div>
		<div id="bodytext" class="clearfix">
			<p>Ban đầu, Đức chưa hợp tác được với công ty nào. Hãy tính chi phí ít nhất để Đức có thể hợp tác với tất cả n công ty.<br />
Dữ liệu cho trong file văn bản KhoiNghiep.Inp gồm:</p>

<ul>
	<li>
	<p>Dòng 1 ghi số nguyên dương n là số công ty.</p>
	</li>
	<li>
	<p>n dòng tiếp theo, dòng thứ i ghi hai số nguyên a<sub>i</sub>,b<sub>i&nbsp;</sub>(1≤i≤n;0≤a<sub>i</sub> ≤n; 0 ≤b<sub>i</sub> ≤10^4)</p>
	</li>
</ul>

<p>Kết quả ghi ra file văn bản KhoiNghiep.Out gồm một số nguyên duy nhất là chi phí ít nhất để Đức có thể hợp tác với tất cả n công ty.</p>

<h2><strong>Giải:</strong></h2>

<pre>
<code># Đọc dữ liệu từ file input
with open(&quot;KhoiNghiep.Inp&quot;, &quot;r&quot;) as f:
    n = int(f.readline().strip())
    companies = &#91;&#93;
    for i in range(n):
        a, b = map(int, f.readline().strip().split())
        companies.append((a, b))

# Sắp xếp các công ty theo giá trị bi tăng dần
companies.sort(key=lambda x: x&#91;1&#93;)

# Khởi tạo danh sách công ty đã hợp tác
partnered = &#91;False&#93; * n

e = 0
total_cost = 0
i = 0
for i in range(n):
    if not partnered&#91;i&#93;:
        if e &gt;= companies&#91;i&#93;&#91;0&#93;:  # Kiểm tra công ty đang xét có free không nếu đuợc thì vào.
            partnered&#91;i&#93; = True
            e += 1
        else:  # Kiểm tra các công ty còn lại có được free không.
            j = i + 1
            while j &lt; n:
                if e &gt;= companies&#91;j&#93;&#91;0&#93; and not partnered&#91;j&#93;:
                    partnered&#91;j&#93; = True
                    e += 1
                    j = i  # Nếu được vào free thì kiểm tra lại từ i đến hết vì e đã thay đổi.
                    continue
                else:
                    j += 1
            if not partnered&#91;i&#93;: # Thoát khỏi vòng while nên không còn có công ty hợp tác free nữa.
                total_cost += companies&#91;i&#93;&#91;1&#93;
                partnered&#91;i&#93; = True
                e += 1
with open(&#039;KhoiNghiep.Out&#039;, &#039;w&#039;) as f:
    f.write(str(total_cost))</code></pre>
&nbsp;

<h2><strong>Giải thích:</strong></h2>

<p>Trong đoạn code trên, ta sử dụng một vòng lặp while để kiểm tra từng công ty. Nếu công ty đó đã được hợp tác, ta tăng giá trị của i lên 1 và tiếp tục kiểm tra công ty tiếp theo. Nếu công ty đó chưa được hợp tác, ta kiểm tra xem có thể hợp tác free hay không. Nếu có, ta đánh dấu công ty đó đã hợp tác và kiểm tra trong các công ty chưa được hợp tác xem còn công ty nào có thể hợp tác free không. Nếu không có, ta tìm công ty có giá trị bi nhỏ nhất để hợp tác. Nếu không có công ty nào có thể hợp tác, ta tăng tổng chi phí lên giá trị bi của công ty hiện tại và đánh dấu công ty đó đã hợp tác.<br />
&nbsp;</p>

<p>Thuật toán mà tôi đã sử dụng để giải quyết bài toán này là một phương pháp tham lam. Cụ thể, ta sẽ kiểm tra từng công ty để xem có thể hợp tác free không. Nếu không thể hợp tác free, ta giải quyết bài toán bằng cách tìm công ty có giá trị bi nhỏ nhất để hợp tác.</p>

<p>Thuật toán này được gọi là phương pháp tham lam vì ta luôn chọn công ty có giá trị bi nhỏ nhất để hợp tác, mà không đánh giá tác động của quyết định đó đến việc hợp tác với các công ty khác ở những bước sau. Tuy nhiên, trong bài toán này, phương pháp tham lam này lại cho kết quả chính xác, mà không cần tìm cách tối ưu toàn bộ quy trình hợp tác.</p>
		</div>
				<div id="author">
						<p>
				<strong>Tác giả:</strong>
				<a href="https://baitaponha.com/author/admin/">admin</a>
			</p>
		</div>
	</div>
	<div id="footer" class="clearfix">
		<div id="url">
			<strong>URL của bản tin này: </strong><a href="https://baitaponha.com/savefile/giai-de-tin-hoc/cau-4-khoi-nghiep-de-thi-hsg-tinh-lop-12-nam-hoc-2022-2023-35.html" title="Khởi nghiệp | Đề thi HSG tin 12 Nghệ An | năm học 2022 - 2023">https://baitaponha.com/savefile/giai-de-tin-hoc/cau-4-khoi-nghiep-de-thi-hsg-tinh-lop-12-nam-hoc-2022-2023-35.html</a>

		</div>
		<div class="clear"></div>
		<div class="copyright">
			&copy; BÀI TẬP Ở NHÀ
		</div>
		<div id="contact">
			<a href="mailto:ducluu80@gmail.com">ducluu80@gmail.com</a>
		</div>
	</div>
</div>
        <div id="timeoutsess" class="chromeframe">
            Bạn đã không sử dụng Site, <a onclick="timeoutsesscancel();" href="https://baitaponha.com/#">Bấm vào đây để duy trì trạng thái đăng nhập</a>. Thời gian chờ: <span id="secField"> 60 </span> giây
        </div>
        <div id="openidResult" class="nv-alert" style="display:none"></div>
        <div id="openidBt" data-result="" data-redirect=""></div>
<div id="run_cronjobs" style="visibility:hidden;display:none;"><img alt="cron" src="/index.php?second=cronjobs&amp;p=s1T2F78O" width="1" height="1" /></div>
<script src="https://baitaponha.com/assets/js/jquery/jquery.min.js"></script>
<script>var nv_base_siteurl="/",nv_lang_data="vi",nv_lang_interface="vi",nv_name_variable="nv",nv_fc_variable="op",nv_lang_variable="language",nv_module_name="news",nv_func_name="savefile",nv_is_user=0, nv_my_ofs=-4,nv_my_abbr="EDT",nv_cookie_prefix="btol",nv_check_pass_mstime=21538000,nv_area_admin=0,nv_safemode=0,theme_responsive=0,nv_recaptcha_ver=2,nv_recaptcha_sitekey="",nv_recaptcha_type="image",XSSsanitize=1;</script>
<script src="https://baitaponha.com/assets/js/language/vi.js"></script>
<script src="https://baitaponha.com/assets/js/DOMPurify/purify3.js"></script>
<script src="https://baitaponha.com/assets/js/global.js"></script>
<script src="https://baitaponha.com/assets/js/site.js"></script>
<script src="https://baitaponha.com/themes/default/js/news.js"></script>
<script src="https://baitaponha.com/assets/js/jquery/jquery.cookie.js"></script>
<script src="https://baitaponha.com/themes/egov/js/main.js"></script>
<script src="https://baitaponha.com/themes/egov/js/custom.js"></script>
<script async src="https://www.googletagmanager.com/gtag/js?id=G-4JBZJ8SEPL"></script>
<script>window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments)}gtag('js',new Date);gtag('config','G-4JBZJ8SEPL');</script>
<script src="https://baitaponha.com/themes/egov/js/bootstrap.min.js"></script>
</body>
</html>