201809-1 201809-2 201809-4 Buy vegetables and then buy vegetables

The first time I went to take the CCF test, I was not fully prepared. Alas, it's a pity.

201809-1

Problem Description

  There are n shops selling vegetables on one street, arranged in a row in the order of 1 to n, and these shops all sell one kind of vegetable.
  On the first day, each store set its own price. The shopkeepers hope that the prices of their dishes will be consistent with those of other shops. The next day, each shop will adjust its prices according to the prices of itself and the neighboring shops. Specifically, each store will set the price of the next day's food as the average of the price of the first day's food in its own and neighboring stores (rounded by the tailing method).
  Note that the store numbered 1 has only one adjacent store 2, the store numbered n has only one adjacent store n-1, and the other stores numbered i have two adjacent stores i-1 and i+1 .
  Given the price of food in each store on the first day, please calculate the price of food in each store on the second day.

Input format

  The first line of input contains an integer n, which represents the number of stores.
  The second line contains n integers, which in turn represent the price of the dish on the first day of each store.

Output format

  Output a row, containing n positive integers, indicating the prices of the dishes in each store on the second day in turn.

Sample input

8
4 1 3 1 6 5 17 9

Sample output

2 2 1 3 4 9 10 13

Data size and convention

  For all evaluation use cases, 2 ≤ n ≤ 1000, the price of the food in each store on the first day is a positive integer not exceeding 10,000.

#include<iostream>
#include<vector>
using namespace std;

int main()
{
    int n;
    cin>>n;
    vector<int> price(n);
    vector<int> tomorrow(n);
    int i=0;
    
    for(i=0;i<n;i++)
    {
        cin>>price[i];
    }
    for(i=1;i<n-1;i++)
        tomorrow[i]=(price[i-1]+price[i]+price[i+1])/3;
    tomorrow[0]=(price[0]+price[1])/2;
    tomorrow[n-1]=(price[n-2]+price[n-1])/2;
    
    cout<<tomorrow[0];
    for(i=1;i<n;i++)
        cout<<" "<<tomorrow[i];
    
    return 0;
}

201809-2

Problem Description

  Little H and Little W came to a street, and they bought groceries separately. The process of buying groceries can be described as going to the store to buy some groceries and then going to a square next to load the groceries on the cart. Both of them buy n kinds Food, so it must be loaded n times. Specifically, for small H, there are n disjoint time periods [a1,b1],[a2,b2]...[an,bn] are loading, and for small W, there are n disjoint periods The time period [c1,d1],[c2,d2]...[cn,dn] is loading. Among them, a time period [s, t] represents the time from time s to time t, and the duration is ts.
  Since they are good friends, they all chat while loading the car in the square, and they want to know how long they can chat.

Input format

  The first line of input contains a positive integer n, which represents the number of time periods.
  In the next n lines, each line has two numbers ai, bi, describing the time period of each loading of small H.
  In the next n lines, each line has two numbers ci, di, describing the time period of each loading of small W.

Output format

  Output one line, a positive integer, indicating how long the two can talk.

Sample input

4
1 3
5 6
9 13
14 15
2 4
5 7
10 11
13 14

Sample output

3

Data size and convention

  For all evaluation use cases, 1 ≤ n ≤ 2000, ai <bi <ai+1, ci <di <ci+1, for all i (1 ≤ i ≤ n), 1 ≤ ai, bi, ci, di ≤ 1000000.

#include<iostream>
#include<vector>
using namespace std;

struct veg
{
    int start;
    int end;
};

int main()
{
    int n;
    cin>>n;
    vector<veg> h(n);
    vector<veg> w(n);
    int totaltime=0;
    
    int i=0;
    for(i=0;i<n;i++)
        cin>>h[i].start>>h[i].end;
    for(i=0;i<n;i++)
        cin>>w[i].start>>w[i].end;
    
    int j=0;
    i=0;
    int min=0,max=0;
    while(i!=n && j!=n)
    {
        if(h[i].start>=w[j].end)
        {
            j++;
            continue;
        }
        if(h[i].end <= w[j].start)
        {
            i++;
            continue;
        }
        
        if(h[i].start <= w[j].start)
        {
            min=w[j].start;
        }
        else
        {
            min=h[i].start;
        }
        if(h[i].end <= w[j].end)
        {
            max=h[i].end;
            i++;
        }
        else
        {
            max=w[j].end;
            j++;
        }
        totaltime+=(max-min);
        
    }
    cout<<totaltime;
    
    return 0;
}

201809-4

Problem Description

  There are n shops selling vegetables on one street, arranged in a row in the order of 1 to n, and these shops all sell one kind of vegetable.
  On the first day, each store set its own price in a positive integer. The shopkeepers hope that the prices of their dishes will be consistent with those of other shops. The next day, each shop will adjust its prices according to the prices of itself and the neighboring shops. Specifically, each store will set the price of the next day's food as the average of the price of the first day's food in its own and neighboring stores (rounded by the tailing method).
  Note that the store numbered 1 has only one adjacent store 2, the store numbered n has only one adjacent store n-1, and the other stores numbered i have two adjacent stores i-1 and i+1 .
  Given the price of the food in each store on the second day, there may be different prices of the first day that meet the requirements. Please find the one with the smallest lexicographical order among the prices of the first day that meet the requirements.
  The definition of lexicographic size: For two different price series (a1, a2, ..., an) and (b1, b2, b3, ..., bn), if there is i (i>=1), such that ai<bi, and for all j<i, aj=bj, the lexicographic order of the first sequence is considered to be less than the second sequence.

Input format

  The first line of input contains an integer n, which represents the number of stores.
  The second line contains n positive integers, which in turn indicate the prices of the dishes in each store on the second day.

Output format

  Output a row, containing n positive integers, indicating the price of the food on the first day of each store in turn.

Sample input

8
2 2 1 3 4 9 10 13

Sample output

2 2 2 1 6 5 16 10

Data size and convention

  For 30% of the evaluation cases, 2<=n<=5, the price of each store’s food on the next day is a positive integer not exceeding 10;
  for 60% of the evaluation cases, 2<=n<=20, the next day The price of dishes in each store is a positive integer not exceeding 100;
  for all evaluation cases, 2<=n<=300, the price of dishes in each store on the next day is a positive integer not exceeding 100.
  Please note that all of the above are the ranges given for the prices of the vegetables on the second day, and the prices of the vegetables on the first day may exceed this range.

Knowing the first-day prices of the first and second stores, you can launch three possible prices on the third day, and so on.

#include<iostream>
#include<vector>
using namespace std;
int n;
int a[300];
int b[300];
int is[300][300][300]={0};
int dfs(int No,int x,int y)//店铺,店铺价格,后一家店铺价格
{
    if(is[No][x][y])
        return 0;
    is[No][x][y]=1;

    if(x<1 || y<1)//判断价格是否正常
        return 0;
    a[No]=x;
    if(No==n-2)//所有的价格目前都得到了
    {
        if((x+y>=2*b[No+1]) && (x+y<=2*b[No+1]+1))//判断第n-1家店和第n家店的价格是否符合要求
        {
            a[No+1]=y;
            cout<<a[0];
            for(int i=1;i<n;i++)
                cout<<" "<<a[i];
            exit(0);//跳出来
        }
        else
            return 0;
        
    }
    
    
    dfs(No+1,y,3*b[No+1]-x-y);
    dfs(No+1,y,3*b[No+1]-x-y+1);
    dfs(No+1,y,3*b[No+1]-x-y+2);
    
    return 0;
}

int main()
{
   
    cin>>n;
    int i=0;
    for(i=0;i<n;i++)
        cin>>b[i];
    
    for(i=1;i<2*b[0];i++)
    {
        dfs(0,i,2*b[0]-i);
        dfs(0,i,2*b[0]-i+1);
    }
    return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_41242380/article/details/83902417