Codeforces#640div4解題報告

Codeforces #640div4

A

原題

A positive (strictly greater than zero) integer is called round if it is of the form d00…0. In other words, a positive integer is round if all its digits except the leftmost (most significant) are equal to zero. In particular, all numbers from 1 to 9 (inclusive) are round.

For example, the following numbers are round: 4000, 1, 9, 800, 90. The following numbers are not round: 110, 707, 222, 1001.

You are given a positive integer n (1≤n≤104). Represent the number n as a sum of round numbers using the minimum number of summands (addends). In other words, you need to represent the given number n as a sum of the least number of terms, each of which is a round number.

Input
The first line contains an integer t (1≤t≤104) — the number of test cases in the input. Then t test cases follow.

Each test case is a line containing an integer n (1≤n≤104).

Output
Print t answers to the test cases. Each answer must begin with an integer k — the minimum number of summands. Next, k terms must follow, each of which is a round number, and their sum is n. The terms can be printed in any order. If there are several answers, print any of them.

思路:簡單的模擬

ACcodes:

#include <bits/stdc++.h>
using namespace std;
int test = 1;

const int N = 2e5+10;
int n,a[N];

int main()
{
	scanf("%d",&test);
	while(test--)
	{
		char ch[1005];
		cin >> ch;
		int tot=0;
		if(strlen(ch)<=1)
		{
			cout << 1<< endl;
			cout << ch << endl;
			continue;
		}
		int len = strlen(ch);
		for(int i=0;i<len;i++)
			if(ch[i]!='0')
				tot++;
		cout << tot << endl;
		for(int i=len-1;i>=0;i--)
		{
			if(ch[i]!='0')
			{
				cout << ch[i];
				for(int j=0;j<len-i-1;j++)
					cout << 0;
				cout << " ";
			}
		}
		cout << endl;
	}
	return 0;
}

B

題目

You are given two positive integers n (1≤n≤109) and k (1≤k≤100). Represent the number n as the sum of k positive integers of the same parity (have the same remainder when divided by 2).

In other words, find a1,a2,…,ak such that all ai>0, n=a1+a2+…+ak and either all ai are even or all ai are odd at the same time.

If such a representation does not exist, then report it.

Input
The first line contains an integer t (1≤t≤1000) — the number of test cases in the input. Next, t test cases are given, one per line.

Each test case is two positive integers n (1≤n≤109) and k (1≤k≤100).

Output
For each test case print:

YES and the required values ai, if the answer exists (if there are several answers, print any of them);
NO if the answer does not exist.
The letters in the words YES and NO can be printed in any case.

思路:分類討論

ACcodes:

#include <bits/stdc++.h>
using namespace std;
int test = 1;

const int N = 2e5+10;
int n,k,a[105],b[105];

int main()
{
	scanf("%d",&test);
	while(test--)
	{	
		cin >> n >> k;
		if(n<k)
		{
			cout << "NO" << endl;
			continue;
		}
		for(int i=1;i<=k;i++)
			a[i] = 1,b[i]=2;
		a[k] = n-k+1;
		b[k] = n-2*k+2;
		if(a[k]%2)
		{
			cout << "YES" << endl;
			for(int i=1;i<=k;i++)
				cout << a[i] << " ";
			cout << endl;
		}
		else if(b[k]%2==0 && b[k]>0)
		{
			cout << "YES" << endl;
			for(int i=1;i<=k;i++)
				cout << b[i] << " ";
			cout << endl;
		}
		else
			cout << "NO" << endl;

	}
	return 0;
}

C

題目

You are given two positive integers n and k. Print the k-th positive integer that is not divisible by n.

For example, if n=3, and k=7, then all numbers that are not divisible by 3 are: 1,2,4,5,7,8,10,11,13…. The 7-th number among them is 10.

Input
The first line contains an integer t (1≤t≤1000) — the number of test cases in the input. Next, t test cases are given, one per line.

Each test case is two positive integers n (2≤n≤109) and k (1≤k≤109).

Output
For each test case print the k-th positive integer that is not divisible by n.

思路:規律題,但要注意long long 的坑

ACcodes:

#include <bits/stdc++.h>
using namespace std;
int test = 1;

const int N = 2e5+10;
long long int n,k,a[N];

int main()
{
	scanf("%d",&test);
	while(test--)
	{
		int pos=0;
		cin>>n>>k;//4 12
		pos+=(k/(n-1))*n;//16
		
		if (k%(n-1)==0)  pos--;
		long long int now=k%(n-1);
		if (now<n)  pos+=now;
		else pos+=(now+1);
		
		cout<<pos<<endl;
	}
	return 0;
}

D

題目

There are n candies in a row, they are numbered from left to right from 1 to n. The size of the i-th candy is ai.

Alice and Bob play an interesting and tasty game: they eat candy. Alice will eat candy from left to right, and Bob — from right to left. The game ends if all the candies are eaten.

The process consists of moves. During a move, the player eats one or more sweets from her/his side (Alice eats from the left, Bob — from the right).

Alice makes the first move. During the first move, she will eat 1 candy (its size is a1). Then, each successive move the players alternate — that is, Bob makes the second move, then Alice, then again Bob and so on.

On each move, a player counts the total size of candies eaten during the current move. Once this number becomes strictly greater than the total size of candies eaten by the other player on their previous move, the current player stops eating and the move ends. In other words, on a move, a player eats the smallest possible number of candies such that the sum of the sizes of candies eaten on this move is strictly greater than the sum of the sizes of candies that the other player ate on the previous move. If there are not enough candies to make a move this way, then the player eats up all the remaining candies and the game ends.

For example, if n=11 and a=[3,1,4,1,5,9,2,6,5,3,5], then:

move 1: Alice eats one candy of size 3 and the sequence of candies becomes [1,4,1,5,9,2,6,5,3,5].
move 2: Alice ate 3 on the previous move, which means Bob must eat 4 or more. Bob eats one candy of size 5 and the sequence of candies becomes [1,4,1,5,9,2,6,5,3].
move 3: Bob ate 5 on the previous move, which means Alice must eat 6 or more. Alice eats three candies with the total size of 1+4+1=6 and the sequence of candies becomes [5,9,2,6,5,3].
move 4: Alice ate 6 on the previous move, which means Bob must eat 7 or more. Bob eats two candies with the total size of 3+5=8 and the sequence of candies becomes [5,9,2,6].
move 5: Bob ate 8 on the previous move, which means Alice must eat 9 or more. Alice eats two candies with the total size of 5+9=14 and the sequence of candies becomes [2,6].
move 6 (the last): Alice ate 14 on the previous move, which means Bob must eat 15 or more. It is impossible, so Bob eats the two remaining candies and the game ends.
Print the number of moves in the game and two numbers:

a — the total size of all sweets eaten by Alice during the game;
b — the total size of all sweets eaten by Bob during the game.
Input
The first line contains an integer t (1≤t≤5000) — the number of test cases in the input. The following are descriptions of the t test cases.

Each test case consists of two lines. The first line contains an integer n (1≤n≤1000) — the number of candies. The second line contains a sequence of integers a1,a2,…,an (1≤ai≤1000) — the sizes of candies in the order they are arranged from left to right.

It is guaranteed that the sum of the values of n for all sets of input data in a test does not exceed 2⋅105.

Output
For each set of input data print three integers — the number of moves in the game and the required values a and b.

思路:模擬,利用好vector

ACcodes:

#include <bits/stdc++.h>
using namespace std;
int test = 1;

const int N = 2e5+10;
int n;
int a,b,tot,m;
int main()
{
	scanf("%d",&test);
	while(test--)
	{
		a=0;
		b=0;
		tot=0;
		m=0;
		vector<int> q;
		cin >> n;
		for(int i=0;i<n;i++)
		{
			int x;
			cin >> x;
			q.push_back(x);
		}
		while(q.size()>0)
		{
			bool flag=false;
			int res=0;
			while(res<=tot && q.size()>0)
			{
				res+=q[0];
				q.erase(q.begin());
				flag = true;
			}
			if(flag)
			{
				m++;
				a+=res;
				tot = res;
				res=0;
				flag = false;
			}
			while(res<=tot && q.size()>0)
			{
				res+=q.back();
				q.pop_back();
				flag = true;
			}
			if(flag)
			{
				b+=res;
				tot=res;
				m++;
			}
		}
		cout << m << " " << a << " " <<  b << endl;
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章