1.1 How to use USACO

26 篇文章 0 订阅

5/23更新:
在这个网站找到正确使用xcode方法
采用的排名第二的step by step 方法
Use the Product Menu(软件打开上面那一条)
Select the Scheme option(下面就直接找就行了…)
Select the Edit Scheme option
Click the Options Tab
Tick the “Working Directory” item
Click the small icon at the right end of the text box.
Select the directory

文件流

c

/*
ID: leidar11
LANG: C
TASK: test
*/
#include <stdio.h>
main () {
    FILE *fin  = fopen ("test.in", "r");
    FILE *fout = fopen ("test.out", "w");
    int a, b;
    fscanf (fin, "%d %d", &a, &b);  /* the two input integers */
    fprintf (fout, "%d\n", a+b);
    exit (0);
}

实际xcode路径

FILE *fin = fopen("/Users/macbook/Desktop/in.txt", "r");
FILE *fout = fopen("/Users/macbook/Desktop/out.txt", "w");

c + +

Below is a simple solution in the C++ programming language. Note the use of 'return (0);', which is usually required to exit properly.

/*
ID: leidar11
PROG: test
LANG: C++                  
*/
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
    ofstream fout ("test.out");
    ifstream fin ("test.in");
    int a, b;
    fin >> a >> b;
    fout << a+b << endl;
    return 0;
}

实际Xcode路径:

 ofstream fout ("/Users/macbook/Desktop/out.txt");
 ifstream fin ("/Users/macbook/Desktop/in.txt");

另一种方法

  string inputFilename = "beads.in", outputFilename = "beads.out";  
  input.open(inputFilename.c_str(), ios::in);
  output.open(outputFilename.c_str(), ios::out);

限制

The restrictions are few:

  • One second runtime limit unless other specified (700 MHz Pentium III)
  • About 16MB datasize limit
  • About 1MB stacksize limit
  • In C/C++, ints are 32 bits (char is 8; short is 16; long is 32; long long is 64)
  • Be sure your program exits with status 0
  • Be sure you print complete lines (with terminating newline), - - not just a few words or numbers
  • Don’t use files other than the specified input, output, and auxiliary files
  • Other common sense rules that need not be listed

题型

  • Dynamic Programming
    • Knapsack背包
  • Greedy

  • search

    • Complete Search
    • Flood Fill
    • Shortest Path
    • Recursive Search Techniques
    • Heuristic Search启发式
    • Approximate Search
  • Minimum Spanning Tree

  • Computational Geometry
  • Network Flow
  • Eulerian Path
  • Two-Dimensional Convex Hull

  • BigNums

  • Ad Hoc Problems 临时问题

1_1_2 ride 字符串处理

analysis_hash

int hash(char *s)
{
    int i, h;

    h = 1;
    for(i=0; s[i] && isalpha(s[i]); i++)
        h = ((s[i]-'A'+1)*h) % 47;
    return h;
}

1_1_5分糖

字符串处理比较

#include <string.h>
#include <assert.h>

void addperson(char *name){
    assert(npeople < MAXPEOPLE);
        strcpy(people[npeople].name, name);
    npeople++;
}

Person* lookup(char *name){
    int i;
    /* look for name in people table */
    for(i=0; i<npeople; i++)
    if(strcmp(name, people[i].name) == 0)
        return &people[i];
    assert(0);  /* should have found name */
}

map水过..BUT
gift写成gist RE两次

1_1_6黑色星期五

判断闰年 暴力枚举
Brute force

int isleap(int y){
    return y%4==0 && (y%100 != 0 || y%400 == 0);
}

int mtab[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

/* return length of month m in year y */
int mlen(int y, int m){
    if(m == 1)    /* february */
        return mtab[m]+isleap(y);
    else
        return mtab[m];
}

1_1_7 deads

神烦循环字符串…
题解都复制了一遍:)
题解二算一边= =

Daniel Bundala had an O(n) solution:

Dynamic Programming is good method for solving this problem in O(N). If we consider two copies of the string we easy transform cyclic configuration of the necklace to linear. Now we can compute for each breaking point how many beads of the same color can be collected on the left and on the right from the breaking point. I show how we can compute it only for the left side. For right side it is analogical. Let r[p] and b[p] be the number of red / blue beads that can be collected, when necklace is broken in point p. If we know this and color of next bead (c) we can compute r[p+1] and b[p+1].
 r[0] = p[0] = 0
 If c = 'r' then r[p+1] = r[p] + 1 and b[p+1] = 0
        because the length of the blue beads is 0.
 if c = 'b' then b[p+1] = b[p] + 1 and r[p+1] = 0
 if c = 'w' then both length of the red and length of blue beads
             can be longer.
so r[p+1] = r[p]+1 and b[p+1] = b[p] + 1.
The number of beads that can be collected in breaking point p is then max(left[r[p]], left[b[p]]) + max(right[r[p]], right[b[p]]). And the maximum from this value is answer for the problem.
#include <stdio.h>
#include <string.h>
#include <algorithm>

using namespace std;

FILE *in,*out;

int main () {
   in = fopen("beads.in", "r");
   out = fopen ("beads.out", "w");
    int n;
   char tmp[400], s[800];
   fscanf(in, "%d %s", &n, tmp);

   strcpy(s, tmp);
   strcat(s, tmp);

   int left[800][2], right[800][2];
   left[0][0] = left[0][1] = 0;

   for (int i=1; i<= 2 * n; i++){
       if (s[i - 1] == 'r'){
           left[i][0] = left[i - 1][0] + 1;
           left[i][1] = 0;
       } else if (s[i - 1] == 'b'){
           left[i][1] = left[i - 1][1] + 1;
           left[i][0] = 0;
       } else {
           left[i][0] = left[i - 1][0] + 1;
           left[i][1] = left[i - 1][1] + 1;
       }
     }

   right[2 * n][0] = right[2 * n][1] = 0;
   for (int i=2 * n - 1; i >= 0; i--){
       if (s[i] == 'r'){
           right[i][0] = right[i + 1][0] + 1;
           right[i][1] = 0;
       } else if (s[i] == 'b'){
           right[i][1] = right[i + 1][1] + 1;
           right[i][0] = 0;
       } else {
           right[i][0] = right[i + 1][0] + 1;
           right[i][1] = right[i + 1][1] + 1;
       }
   }

   int m = 0;
   for (int i=0; i<2 * n; i++)
       m = max(m, max(left[i][0], left[i][1]) + max(right[i][0], right[i][1]));
   m = min(m, n);
   fprintf(out, "%d\n", m);
   fclose(in); fclose(out);
   return 0;
}

题解二:

for(i=0; i<n; i++) {
    c = (char) s[i];
    if(c == 'w')
      state = 0;
    else
      state = 1;
    j = i;
    current = 0;

    while(state <= 2) { 
      // dont go further in second string than starting position in first string
      while(j<n+i && (s[j] == c || s[j] == 'w')) { 
        current++;
        j++;
      } 
      state++;
      c = s[j];
    } // while
    if(current > max)
      max = current;
  } // for
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值