ACM 个人总结部分

 

一、常用的C++函数

    排序函数的qsort

     int  cmp(const void *a,const void *b){

              return *(int *)a - *(int *)b;

          }

     qsort(数组名,长度,sizeof(int),cmp);

  求最大公约数的算法(仅仅限于int

     int gcb(int a,int b)

    {

         return (b= =0?a:gcb(b,a%b));

      }

获取一行字符串的以回车为

cin.Getline(char *a , int size);

对于string的争端处理则用的是:getline ( cin ,字符串名字);

堆的排序(最小堆)

#include<iostream>

using namespace std;

int heap[10010];//堆所储存的结点元素个数

int n;//数的个数

void heapf(int k)//建立堆的结构调整 k代表要从k的位置开始调整堆的结构

{

    int mins=k,lchild=2*k,rchild=2*k+1;//分别找他的子节点

    if(lchild<=n && heap[lchild]<heap[mins])//和左节点比较

        mins=lchild;//如果满足条件就交换其最小的下标

    if(rchild<=n && heap[rchild]<heap[mins])//和右节点比较

         mins=rchild;//如果满足条件就交换其最小值得下标和值

    if(mins!=k) {   //如果发生下标的改变就交换

         int temp=heap[k];    heap[k]=heap[mins];   heap[mins]=temp;

         heapf(mins); //重复的调换位置直到达到最小堆为止

    }

}

void increases(int k)//增加子节点 k 代表 插入的元素的位置

{

     int parent=k/2;

    if(parent>0 && heap[parent]>heap[k]){//和它的父节点比较如果满足条件就交换

        int temp=heap[parent];   heap[parent]=heap[k];    heap[k]=temp;

          increases(parent);//继续增加        

     }    

}

int getmins()//获得最小值

{

    int v=heap[1];//给出最小值

    heap[1]=heap[n];//获得最小元素以后把最后一个堆元素赋值给第一个

    n--;//堆的大小减去一个

    heapf(1);//重新调整对的结构是他满足最小堆

    return v;//返回最小值

}

void inserts(int v)//插入元素 V代表要插入的数

{

    heap[++n]=v;//每次插入元素长度增加一个

    increases(n);//调用增加堆的函数 

}

void buildheaps()//建立一个堆

{

    for(int i=n/2;i>=1;i--)//创建堆的大小一定是它的叶子节点小于n/2

     heapf(i);//建立堆的时候并且调整堆 从倒数第二排开始建立堆

}

int main(){

    int sum=0,rsum,m;

    scanf("%d",&n);

    m=n;

    for(int i=1;i<=n;i++)

        scanf("%d",&heap[i]);

    buildheaps();//开始建立堆

    return 0;

}

Milking Time

Time Limit: 1000MS

 

 65536K

 

 

 

Description

Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she decides to schedule her next N (1 ≤ N ≤ 1,000,000) hours (conveniently labeled 0..N-1) so that she produces as much milk as possible.

Farmer John has a list of M (1 ≤ M ≤ 1,000) possibly overlapping intervals in which he is available for milking. Each interval i has a starting hour (0 ≤ starting_houriN), an ending hour (starting_houri < ending_houriN), and a corresponding efficiency (1 ≤ efficiencyi ≤ 1,000,000) which indicates how many gallons of milk that he can get out of Bessie in that interval. Farmer John starts and stops milking at the beginning of the starting hour and ending hour, respectively. When being milked, Bessie must be milked through an entire interval.

Even Bessie has her limitations, though. After being milked during any interval, she must rest R (1 ≤ RN) hours before she can start milking again. Given Farmer Johns list of intervals, determine the maximum amount of milk that Bessie can produce in the N hours.

Input

* Line 1: Three space-separated integers: N, M, and R
* Lines 2..M+1: Line i+1 describes FJ's ith milking interval withthree space-separated integers: starting_houri , ending_houri , and efficiencyi

Output

* Line 1: The maximum number of gallons of milk that Bessie can product in the N hours

Sample Input

12 4 2

1 2 8

10 12 19

3 6 24

7 10 31

Sample Output

43

#include <iostream>

#include <algorithm>

using namespace std;

const int M=1001;

struct Work

{

 int start;int end;int value;int prev;

}

w[M];

bool operator <(const Work & a,const Work & b)

{

 return a.start<b.start;

}

int main()

{

 int i,j,n,m,r;

 cin>>n>>m>>r;

 for(i=0;i<m;i++)

 {

  cin>>w[i].start>>w[i].end>>w[i].value;

  w[i].end+=r;

  if(w[i].end>n)

   w[i].end=n;

  w[i].prev=0;

 }

 sort(w,w+m);

 for(i=0;i<m;i++)

 {

  int tmp=w[i].prev+w[i].value;

  for(j=i+1;j<m;j++)

  {

   if(w[i].end<=w[j].start)

   {

    if(tmp>w[j].prev)

     w[j].prev=tmp;

   }

  }

 }

 int maxn=0;

 for(i=m-1;i>=0;i--)

 {

  if(maxn<w[i].prev+w[i].value)

   maxn=w[i].prev+w[i].value;

 }

 cout<<maxn<<endl;

 return 0;

}

iChess

Time Limit: 1000MS

 

 65536K

 

 

 

Description

The Jury of NEERC’07 quarterfinals is proud to present you a new game — chess patience. This patience is played not with cards, but with black and white square tiles. The goal of the game is to place these tiles on a flat surface so that they form a square colored in a chess-like pattern. The square should be totally filled and be of the maximal possible size. There may remain some spare tiles, if they do not fit into the resulting square.

To make this game more popular, a computer version of this patience named iChess was developed. The rules are the same with the exception that the player is given the number of tiles, not the actual tiles. Also, the result of the patience is not the actual layout, but the side length (measured in tiles) of the maximal square with the required layout.

Your task is to write a program which can play iChess patience.

Input

The input file contains two integer numbers b and w — the number of black and white tiles respectively (0 ≤ b, w ≤ 10 000).

Output

The first line of the input file must contain a single integer number s — the side length of the maximum possible square made of at most b black and w white tiles.

If no square can be formed with the given tiles, output a single word “Impossible”.

Sample Input

#1

12 15

#2

0 0

Sample Output

#1

5

#2

Impossible

#include<iostream>

#include<cmath>

using namespace std;

int dl(int a,int b,int c);

int deal(int a,int b,int c);

int dl(int a,int b,int c)

{

    int t=(c/2)*c;

    if(a>=t&&b>=t) return c;

    else{ if((c-1)%2==0)  return dl(a,b,c-1);

        return deal(a,b,c-1);}

}

int deal(int a,int b,int c)

{

    int t=c/2;

    int m=(t+1)*(t+1)+t*t;

    int n=2*(t+1)*t;

    if((a>=m&&b>=n)||(a>=n&&b>=m))  return c;

    else {  if((c-1)%2==0) return dl(a,b,c-1);

        return deal(a,b,c-1); }

}

int God(double a,double b)

{

    int sum=(int)pow(a+b,0.5);

    if(sum%2==0)  return dl(a,b,sum);

    return deal(a,b,sum);  

}

int main()

{

    int a;

    double B,W;

    while(cin>>B>>W){

        if(B==0&&W==0) a=0;

        else if(B==1||W==1) a=1;

        else   a=God(B,W);    

        if(a==0) cout<<"Impossible"<<endl;

        else  cout<<a<<endl;

    }

    return 0;

}

And Then There Was One

Time Limit: 5000MS

 

 65536K

 

 

 

Description

Let’s play a stone removing game.

Initially, n stones are arranged on a circle and numbered 1, …, n clockwise (Figure 1). You are also given two numbers k and m. From this state, remove stones one by one following the rules explained below, until only one remains. In step 1, remove stone m. In step 2, locate the k-th next stone clockwise from m and remove it. In subsequent steps, start from the slot of the stone removed in the last step, make k hops clockwise on the remaining stones and remove the one you reach. In other words, skip (k − 1) remaining stones clockwise and remove the next one. Repeat this until only one stone is left and answer its number. For example, the answer for the case n = 8, k = 5, m = 3 is 1, as shown in Figure 1.


Initial state


Step 1


Step 2


Step 3


Step 4


Step 5


Step 6


Step 7


Final state

 

Figure 1: An example game

Initial state: Eight stones are arranged on a circle.

Step 1: Stone 3 is removed since m = 3.

Step 2: You start from the slot that was occupied by stone 3. You skip four stones 4, 5, 6 and 7 (since k = 5), and remove the next one, which is 8.

Step 3: You skip stones 1, 2, 4 and 5, and thus remove 6. Note that you only count stones that are still on the circle and ignore those already removed. Stone 3 is ignored in this case.

Steps 4–7: You continue until only one stone is left. Notice that in later steps when only a few stones remain, the same stone may be skipped multiple times. For example, stones 1 and 4 are skipped twice in step 7.

Final State : Finally, only one stone, 1, is on the circle. This is the final state, so the answer is 1.

Input

The input consists of multiple datasets each of which is formatted as follows.

n k m

The last dataset is followed by a line containing three zeros. Numbers in a line are separated by a single space. A dataset satisfies the following conditions.

2 ≤ n ≤ 10000, 1 ≤ k ≤ 10000, 1 ≤ mn

The number of datasets is less than 100.

Output

For each dataset, output a line containing the stone number left in the final state. No extra characters such as spaces should appear in the output.

Sample Input

8 5 3

100 9999 98

10000 10000 10000

0 0 0

Sample Output

1

93

2019

#include<iostream>

using namespace std;

int main()

{

  int a,b,c,res,i;

  while (scanf("%d%d%d",&a,&b,&c)){

     if (!a) break;

     res=0;

     for (i=2;i<a;++i) res=(res+b)%i;

     ++res;

     res=(res+c)%a;

     if(!res) printf("%d/n",a);

     else printf("%d/n",res);}

 return 0;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值