现在的位置: 首页 > 综合 > 正文

usaco Chapter 1 Section 3

2013年02月03日 ⁄ 综合 ⁄ 共 5705字 ⁄ 字号 评论关闭

 

1. 简单贪心,价格低的优先考虑。

code:

#include<cstdio>
#include<cstring>
#include<fstream>
#include<cstdlib>
using namespace std ;
#define read    freopen("milk.in", "r", stdin)
#define write   freopen("milk.out", "w", stdout)
struct node{
    int pri, cnt ;
}q[5001] ;
int cmp(const void *a, const void *b){
    return (*(node *)a).pri < (*(node *)b).pri ? -1:1 ;
}
int main(){
    int m, n, i, j, sum = 0 ;
    read ;
    write ;
    scanf("%d%d", &m, &n) ;
    for(i=0; i<n; i++)
        scanf("%d%d", &q[i].pri, &q[i].cnt) ;
    qsort(q, n, sizeof(q[0]), cmp) ;
    for(i=0; i<n; i++){
        if(m<q[i].cnt){
            sum += m * q[i].pri ;
            break ;
        }
        sum += q[i].cnt * q[i].pri ;
        m -= q[i].cnt ;
    }
    printf("%d\n", sum) ;
    return 0 ;}

 

 2. 贪心。O(n)求出相邻两个牛的间距,对间距排序,间距大的优先舍弃。

code:

#include<cstdio>
#include<cstring>
#include<fstream>
#include<cstdlib>
using namespace std ;
#define read    freopen("barn1.in", "r", stdin)
#define write   freopen("barn1.out", "w", stdout)
int data[201], t[201] ;
int cmp(const void *a, const void *b){
    return *(int *)a > *(int *)b ? -1 : 1 ;
}
int main(){
    int m, s, c, i, j, min, max ;
    read ;
    write ;
    scanf("%d%d%d", &m, &s, &c) ;
    for(i=0; i<c; i++)
        scanf("%d", &data[i]) ;
    qsort(data, c, sizeof(int), cmp) ;
    for(i=0; i<c-1; i++)
        t[i] = data[i] - data[i+1] ;
    qsort(t, c, sizeof(int), cmp) ;
    s = data[0] - data[c-1] + 1 ;
    for(i=0; i<m-1&&i<c-1; i++){
        s -= t[i] - 1 ;
    }
    printf("%d\n", s) ;
    return 0 ;} 

 

 3. 听说这题好像可以用后缀数组,试了一下没写出来。有时间再写一遍。

枚举回文串中间位置,默认为奇数长度,若是str[i]==str[i+1],则长度可能为偶数。

其实可以预处理出一个数组记录每个字母在原字符串中的位置,这样输出就不用那么麻烦了。

code:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<fstream>
#include<cstdlib>
#include<memory>
#include<algorithm>
#define read    freopen("calfflac.in", "r", stdin)
#define write   freopen("calfflac.out", "w", stdout)
#define rd      ifstream cout("calfflac.out") ;
#define wt      ifstream cin("calfflac.in") ;
using namespace std ;
char str[20001], s[20001] ;
int main(){
    read ;
    write ;
    int i, j, k, n=0, count=0, len ;
    while(cin.get(str[n])){
        if(str[n]>='a'&&str[n]<='z')
            s[count++] = str[n] ;
        if(str[n]>='A'&&str[n]<='Z')
            s[count++] = char(str[n] + 32) ;
        n ++ ;
    }
    int ans, max = 1, id=0, f=0 ;
    for(i=0; i<count; i++){
        ans = 0 ;
        j = i-1 ;
        k = i+1 ;
        while(j>=0&&k<count){
            if(s[j]!=s[k])  break ;
            j -- ;
            k ++ ;
            ans ++ ;
        }
        if(max<ans){max = ans ; id = i ; f = 0 ;}
        if(s[i]==s[i+1]){
            j = i-1 ;
            k = i+2 ;
            while(j>=0&&k<count){
                if(s[j]!=s[k])  break ;
                j -- ;
                k ++ ;
                ans ++ ;
            }
            if(max<ans){max = ans ;id = i ;f = 1 ;}
        }
    }
    int c = 0 ;
    if(f)
        ans = 2*(max + 1) ;
    else
        ans = 2*max + 1 ;
    cout << ans << endl ;
    id -= max ;
    for(i=0; i<n; i++){
        if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z')) c ++ ;
        if(c==id+1){
            c = 0 ;
            while(true){
                if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z')) c ++ ;
                cout << str[i++] ;
                if(c==ans){
                    cout << endl ;
                    return 0 ;
                }
            }
        }
    }
    return 0 ;} 

 

 4. 说真的,我不知道写这种题目意义何在。

code:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<fstream>
#include<cstdlib>
#include<memory>
#include<algorithm>
#define read    freopen("crypt1.in", "r", stdin)
#define write   freopen("crypt1.out", "w", stdout)
#define rd      ifstream cout("crypt1.out") ;
#define wt      ifstream cin("crypt1.in") ;
int vis[10] ;
int main(){
    int n, i, j, k, h, g, ans = 0, n1, n2, n3, n4 ;
    read ;
    write ;
    scanf("%d", &n) ;
    memset(vis, 0sizeof(vis)) ;
    for(i=0; i<n; i++){
        scanf("%d", &j) ;
        vis[j] = 1 ;
    }
    n1 = n2 = n3 = n4 = 0 ;
    for(i=1; i<10; i++){
        if(!vis[i]) continue ;
        for(j=1; j<10; j++){
            if(!vis[j]) continue ;
            for(k=1; k<10; k++){
                if(!vis[k]) continue ;
                for(h=1; h<10; h++){
                    if(!vis[h]) continue ;
                    for(g=1; g<10; g++){
                        if(!vis[g]) continue ;
                        n1 = i * 100 + j * 10 + k ;
                        n2 = n1 * g ;
                        n3 = n1 * h ;
                        if(n2>=1000||n3>=1000)    continue ;
                        int f = 0 ;
                        int t = n2 ;
                        while(t){
                            if(!vis[t%10]){
                                f = 1 ;
                                break ;
                            }
                            t /= 10 ;
                        }
                        if(f)   continue ;
                        t = n3 ;
                        while(t){
                            if(!vis[t%10]){
                                f = 1 ;
                                break ;
                            }
                            t /= 10 ;
                        }
                        if(f)   continue ;
                        n4 = n3 * 10 + n2 ;
                        t = n4 ;
                        if(n4>=10000continue ;
                        while(t){
                            if(!vis[t%10]){
                                f = 1 ;
                                break ;
                            }
                            t /= 10 ;
                        }
                        if(f)   continue ;
                        ans ++ ;
                    }
                }
            }
        }
    }
    printf("%d\n", ans) ;
    return 0 ;}

 

抱歉!评论已关闭.