CCF CSP then selling vegetables c ++ java python 201809_4

CCF CSP then selling vegetables c ++ java python 201809_4

Problem description
  with n vegetables in stores on a street, in the order 1 to n in a row, these stores have sold a vegetable.
  The first day, each store set himself a positive integer price. Shopkeepers want their consistency vegetables and other stores, the next day, every shop will adjust their prices according to the price of his own and neighboring shops. Specifically, each of the vegetables will store the next day and their average value is set to the first day of the neighboring stores of vegetable (tail spent rounding method).
  Note that, for the store No. 1, only a 2 adjacent shop, store number n only a neighboring store n-1, i stores the number of other two adjacent stores i-1 and i + 1 .
  Vegetables given to the first day of the second day of vegetables each store, there may be different to meet the requirements, please find the smallest of the lexicographically first day of vegetables meet the requirements. B . 1
  defined lexicographically size: for two different prices sequences (A . 1 , A 2 , ..., A n- ) and (B . 1 , B 2 , B . 3 , ..., B n- ), if there is i (i> = 1), such that a I <B I , and for all J <I, a J = B J , is considered the first sequence lexicographically less than the second sequence.
Input format
  of the first line of the input contains an integer n, the number of stores.
  The second line contains n positive integers, respectively for each vegetable store the next day.
Output format
  output line, comprising n positive integers, respectively for the first day of each store vegetables.
Sample input
  . 8
  2 2 10. 9. 1. 4. 3 13 is
sample output
  2221651610
size of the data and agreed
  to use Example Reviews 30%, 2 <= n <= 5 , the next day each store dish price is a positive integer not exceeding 10; and
  for evaluation by 60% in Example, 2 <= n <= 20 , each store vegetables for the next day is not more than 100, a positive integer;
  for reviews all use cases, 2 <= n <= 300, the next day each store vegetable is a positive integer not exceeding 100.
  Please note that the above are given range of vegetables the next day, the first day of vegetables may exceed this range.
Analysis
after seeing this question, the first thought is deep search, but only after submitting found to run overtime, can only get 80 points, to check someone else's blog, find someone to do a linear differential, as well as the who set up a three-dimensional array, and then in the search to determine if visited before if not visited then search, but I never wanted to understand the meaning of that array, so if you know how to interpret the three-dimensional array, welcome Gangster advise in the comments area. My code is as follows:
c ++ code is as follows

#include<cstdio>
#include<stdlib.h>
const int MAXN = 305;
int array[MAXN];
int target[MAXN];
int number;
int main(){
	void dfs(int index);
	scanf("%d",&number);
	for(int i = 1;i <= number;i++){
		scanf("%d",&array[i]);
	}
	for(int i = 1;i <= 2 * array[1];i++){
		target[1] = i;
		target[2] = 2 * array[1] - target[1];
		if(target[2] > 0){
			dfs(2);
		}
		target[2] = 2 * array[1] - target[1] + 1;
		if(target[2] > 0){
			dfs(2);
		}
	}
	return 0;
}
void dfs(int index){
	if(index == number - 1){
		int temp = 3*array[index]-target[index-1];
		if((temp/2==array[number])||(temp+1)/2==array[number] ||(temp+2)/2==array[number]){
			for(int i = 1;i <= number - 1;i++){
				printf("%d ",target[i]);
			}
			for(int i = 0;i < 3;i++){
				if((temp+i)/2==array[number]){
					printf("%d",3*array[index]+i-target[index]-target[index-1]);
					exit(0);
				}
			}
		}
	}
	for(int i = 0;i < 3;i++){
		target[index+1] = 3 * array[index]-target[index] - target[index-1] + i;
		if(target[index+1] > 0){
			dfs(index+1);
		}
	}
}

The results will run overtime, you can only get 80 points.
Java code is as follows:

import java.util.Scanner;
public class csp201809_4 {
	public static int[] a = new int[305];
	public static int[] b = new int[305];
	public static int number;
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		number = input.nextInt();
		for(int i = 1;i <= number;i++) {
			a[i] = input.nextInt();
		}
		for(int i = 1;i <= 2 * a[1];i++) {
			b[1] = i;
			b[2] = 2*a[1]-b[1];
			if(b[2] > 0) {
				dfs(2);
			}
			b[2] = 2 * a[1] - b[1] + 1;
			if(b[2] > 0) {
				dfs(2);
			}
		}
	}
	public static void dfs(int index) {
		if(index == number - 1) {
			if((3*a[index]-b[index-1])/2==a[number]||(3*a[index]-b[index-1]+1)/2==a[number]||(3*a[index]-b[index-1]+2)/2==a[number]) {
				for(int i = 1;i <= number-1;i++) {
					System.out.print(b[i] + " ");
				}
				for(int i = 0;i < 3;i++) {
					if((3*a[index]-b[index-1]+i)/2==a[number]) {
						System.out.print(3*a[index]-b[index]-b[index-1]+i);
						System.exit(0);
					}
				}
			}
		}
		for(int i = 0;i < 3;i++) {
			b[index+1] = 3 * a[index] - b[index] - b[index - 1] + i;
			if(b[index+1] > 0) {
				dfs(index+1);
			}
		}
	}

}

The results will run overtime, you can only get 80 points.
python3 code is as follows:

import sys
number = int(input())
temp = input().split(" ")
array = [0]*(number+5)
target = [0]*(number+5)
def dfs(index):
    if index == number - 1:
        temp = 3 * array[index]-target[index-1]
        if (temp // 2 == array[number]) or ((temp+1) // 2 == array[number]) or ((temp+2) // 2 == array[number]):
            for i in range(1,number):
                print((str(target[i])+" "),end="")
            for i in range(3):
                if ((temp+i) // 2 == array[number]):
                    print(3 * array[index]+i-target[index]-target[index-1],end="")
                    sys.exit(0)
    for i in range(3):
        target[index + 1] = 3 * array[index]-target[index] - target[index-1] + i
        if (target[index+1] > 0):
            dfs(index+1)
if __name__=="__main__":
    for i in range(1,number+1):
        array[i] = int(temp[i - 1])
    for i in range(1,2*array[1]+1):
        target[1] = i
        target[2] = 2 * array[1] - target[1]
        if target[2] > 0:
            dfs(2)
        target[2] = 2 * array[1] - target[1] + 1
        if target[2] > 0:
            dfs(2)

If there is a better way big brother, welcome advice in the comments area.

发布了19 篇原创文章 · 获赞 13 · 访问量 4083

Guess you like

Origin blog.csdn.net/qq_38929464/article/details/88408291