UVA 11636 Hello World,LA 3602 DNA Consensus String,UVA 10970 Big Chocolate,UVA 10340 All in All,UVA 11039 Building Designing

课程好紧啊,只能刷点水题了,几乎都是贪心。

UVA 11636 Hello World

二的幂答案就是二进制长度减1,不是二的幂答案就是是二进制长度。

#include<cstdio>

int main()
{
    int n,kas = 0;
    while(scanf("%d",&n),n>0){
        int r = 0;
        for(n--;n;n>>=1) r++;
        printf("Case %d: %d\n",++kas,r);
    }
    return 0;
}

LA 3602 DNA Consensus String

贪心构造,每一位上选出现次数最多的。

#include<bits/stdc++.h>
using namespace std;
const int maxm = 55,maxn = 1e3+1;

int id[256];
char rid[] = "ACGT";
int ct[4][maxn];
char s[maxn+5];
int main()
{
    id[‘A‘] = 0; id[‘C‘] = 1; id[‘G‘] = 2; id[‘T‘] = 3;
    int T;cin>>T;
    while(T--){
        int m,n; scanf("%d%d",&m,&n);
        for(int i = 0; i < 4; i++) fill(ct[i],ct[i]+n,0);
        for(int i = 0; i < m; i++){
            scanf("%s",s);
            for(int j = 0; j < n; j++){
                ct[id[s[j]]][j]++;
            }
        }
        int sum = 0;
        for(int j = 0; j < n; j++){
            int k = 0;
            for(int i = 1; i < 4; i++) if(ct[i][j] > ct[k][j]) k = i;
            putchar(rid[k]); sum += m-ct[k][j];
        }
        printf("\n%d\n",sum);
    }
    return 0;
}

UVA 10970 Big Chocolate (等效转换)

题目问最少的刀数是迷惑人的,从块数来看,每次切只会增加一块巧克力。

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int m,n;
    while(~scanf("%d%d",&m,&n)){
        printf("%d\n",m*n-1);
    }
    return 0;
}

贪心选,两个指针,t中的当前元素能选就选,不选这个元素对答案就没有贡献了,一定不会更优。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5;
char s[maxn],t[maxn];
int main()
{
    while(~scanf("%s%s",s,t)){
        char *p = s,*q = t;
        while(*p){
            while(*q && *q != *p) q++;
            if(!*q) break;
            p++; q++;
        }
        if(!*p) puts("Yes");
        else puts("No");
    }
    return 0;
}

UVA 11039 Building Designing

贪心,按照绝对值排序,记录一下上次的符号,能选就选。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 5e5+5;
int a[maxn];
bool cmp(int a,int b) { return abs(a) < abs(b); }
int main()
{
    int T; cin>>T;
    while(T--){
        int n; scanf("%d",&n);
        for(int i = 0; i < n; i++) scanf("%d",a+i);
        sort(a,a+n,cmp);
        int ans = 1; bool fg = a[0]>0;
        for(int i = 1; i < n; i++){
            bool tfg = a[i]>0;
            if( (tfg&&!fg) || (!tfg&&fg) ) {
                fg = tfg; ans++;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-14 16:06:08

UVA 11636 Hello World,LA 3602 DNA Consensus String,UVA 10970 Big Chocolate,UVA 10340 All in All,UVA 11039 Building Designing的相关文章

LA 3602 DNA Consensus String (暴力枚举)

题意:给定m个长度为n的DNA序列,求一个最短的DNA序列,使得总Hamming距离最小. Hamming距离等于字符不同的位置个数. 析:看到这个题,我的第一感觉是算时间复杂度,好小,没事,完全可以暴力,只要对每个串的同一个位置, 都选出现最多的,如果有一样的选ASIIC码小的(因为要求字典序小).然后记录最字符和Hamming距离即可. 代码如下: #include <iostream> #include <cstdio> #include <algorithm>

LA 3602 DNA Consensus String

最近审题老是一错再错,Orz 题目中说求一个Hamming值总和最小的字符串,而不是从所给字符中找一个最小的 这样的话,我们逐列处理,所求字符串当前位置的字符应该是该列中出现次数最多其次ASCII值最小的 代码有点挫了,if语句太多了 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 using namespace std; 6 7 char gene[5

uva 1368 - DNA Consensus String(字符串处理)

uva 1368 - DNA Consensus String Figure 1. DNA (Deoxyribonucleic Acid) is the molecule which contains the genetic instructions. It consists of four different nucleotides, namely Adenine, Thymine, Guanine, and Cytosine as shown in Figure 1. If we repre

UVA - 1368 DNA Consensus String

DNA Consensus String Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  Figure 1. DNA (Deoxyribonucleic Acid) is the molecule which contains the genetic instructions. It consists of four different nuc

UVa1368.DNA Consensus String

原题链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4114 13845357 1368 DNA Consensus String Accepted C++ 0.022 2014-07-07 12:29:30  DNA Consensus String Figure 1. DNA (Deoxyribonucleic Acid) is t

uva 11039 Building designing (排序)

uva 11039 Building designing An architect wants to design a very high building. The building will consist of some floors, and each floor has a certain size. The size of a floor must be greater than the size of the floor immediately above it. In addit

UVA1368 UVALive3602 ZOJ3132 DNA Consensus String

Regionals 2006 >> Asia - Seoul 问题链接:UVA1368 UVALive3602 ZOJ3132 DNA Consensus String. 问题简述:给定m个长度为n的DNA序列,求一个DNA序列,使其到所有这些序列的总hamming距离尽量小,如果有多个解,输出字典顺序的最小解. 解题思路:每个DNA序列的长度相同,对每个DNA序列的每一位DNA码进行统计,选取出现次数最多的码,那么这一位的hamming距离最小.依次类推,每一位都选取出现次数最多的码,那么这

UVa 11039 - Building designing

题目:有n个绝对值不为0的数字,从中找到一个序列,正负交替,绝对值递增,求序列最大长度. 分析:dp,动态规划.因为绝对值要递增,所以先按绝对值排序. 设前k个数组成大的序列最长为f(k),则有如下地推关系: f(k)= f(k-1)        { data[k]*data[k-1] > 0,最后量元素不同时取 } = f(k-1)+ 1  { data[k]*data[k-1] < 0,最后量元素同时取 } (所有数据均不相同,且不为零) 说明:(⊙v⊙). #include <a

DNA Consensus String,( UVa, 1368 )

题目链接 :https://vjudge.net/problem/UVA-1368 题目首先得知道什么是Hamming距离(自行查找),然后注意出现多个解是选择字典序小者(直接将字典序排号比较),**注意数组大小. 1 #include<stdio.h> 2 #include<string.h> 3 #define maxn 1000 4 #define maxm 1000 5 6 char DNA[maxn][maxm]; 7 int main() 8 { 9 int N; 10