zoukankan      html  css  js  c++  java
  • SGU

    186. The Chain

    time limit per test: 0.25 sec.
    memory limit per test: 4096 KB
    input: standard input
    output: standard output



    Smith has N chains. Each chain is the sequence of successively connected links. The length of each chain is known: the first chain contains L1 links, the second - L2, ..., the last one - LN. 
    He can make a following series of actions in a minute: 
    1. to unchain one link 
    2. to remove or to put into the unchained link some other links of any chain 
    3. to chain the link 
    Your task is to determine the minimum time which will take the smith to connect all the chains in one line, i.e. the chain will look like a chain made up of successively connected links.

    Input
    The first line contains natural number N<=100. The second line contains L1, L2, ..., LN (1<=Li<=100, for all i = 1..N).

    Output
    Output the only integer number - the solution to the problem.

    Sample test(s)

    Input
    
    
    2 3 4
    Output
    
    
    1

    Author: Michael R. Mirzayanov
    Resource: ACM International Collegiate Programming Contest 2003-2004 
    North-Eastern European Region, Southern Subregion
    Date: 2003 October, 9











    思路:好无耻的英语。说的这么拐弯抹角真的好吗?题意应该是这种。先从一个chain上拆下一个link。然后能够连接两个chain,仅仅到全部chain都连起来为止,这里算一分钟。所以先从短链開始拆更省时,贪心....


    AC代码:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    int a[105];
    
    int main() {
    	int n;
    	while(scanf("%d", &n) != EOF) {
    		for(int i = 0; i < n; i++) {
    			scanf("%d", &a[i]);
    		}
    		
    		sort(a, a+n);
    		
    		int i = 0, j = n-1, ans = 0;
    		while(i < j) {
    			a[i]--;
    			j--;
    			if(a[i] == 0) i++;
    			ans++;
    		}
    		
    		printf("%d
    ", ans);
    	}
    	return 0;
    } 











  • 相关阅读:
    简单实现缓存需求
    Visual Studio 项目模板、文件模板、代码段和环境设置
    Memcached 基础应用
    Visual Studio 2010 模型设计工具 基本应用
    委托和事件 委托
    C# 编码规范
    锂电池正确使用方法
    HTC W800(XV6750) USB 连接 Windows 7 设备错误 解决办法
    UML 面向对象分析与设计
    注册表项写入与删除简便方法
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/7207807.html
Copyright © 2011-2022 走看看