A. Balloons
There are quite a lot of ways to have fun with inflatable balloons. For example, you can fill them with water and see what happens.
Grigory and Andrew have the same opinion. So, once upon a time, they went to the shop and bought nn packets with inflatable balloons, where ii-th of them has exactly aiai balloons inside.
They want to divide the balloons among themselves. In addition, there are several conditions to hold:
- Do not rip the packets (both Grigory and Andrew should get unbroken packets);
- Distribute all packets (every packet should be given to someone);
- Give both Grigory and Andrew at least one packet;
- To provide more fun, the total number of balloons in Grigory's packets should not be equal to the total number of balloons in Andrew's packets.
Help them to divide the balloons or determine that it's impossible under these conditions.
The first line of input contains a single integer nn (1≤n≤101≤n≤10) — the number of packets with balloons.
The second line contains nn integers: a1a1, a2a2, ……, anan (1≤ai≤10001≤ai≤1000) — the number of balloons inside the corresponding packet.
If it's impossible to divide the balloons satisfying the conditions above, print −1−1.
Otherwise, print an integer kk — the number of packets to give to Grigory followed by kk distinct integers from 11 to nn — the indices of those. The order of packets doesn't matter.
If there are multiple ways to divide balloons, output any of them.
31 2 1
21 2
25 5
-1
110
-1
In the first test Grigory gets 33 balloons in total while Andrey gets 11.
In the second test there's only one way to divide the packets which leads to equal numbers of balloons.
In the third test one of the boys won't get a packet at all.
坑了3次,输入的是数列的序号不是值。
1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 const int N = 110; 5 int n, sum, ans; 6 struct Nod{ 7 int id,num; 8 }nod[11]; 9 bool cmp(Nod a, Nod b) { 10 a.num < b.num; 11 } 12 int main() { 13 cin >> n; 14 for(int i = 1; i <= n; i ++) { 15 cin >> nod[i].num; 16 nod[i].id = i; 17 sum += nod[i].num; 18 } 19 sort(nod+1,nod+1+n,cmp); 20 if(n == 1) return 0*printf("-1\n"); 21 for(int i = 1; i < n; i ++) { 22 ans += nod[i].num; 23 if(ans*2 != sum) { 24 printf("%d\n",i); 25 for(int j = 1; j <= i; j ++) printf("%d ",nod[j].id); 26 printf("\n"); 27 return 0; 28 } 29 } 30 printf("-1\n"); 31 return 0; 32 }
B. Cutting
There are a lot of things which could be cut — trees, paper, "the rope". In this problem you are going to cut a sequence of integers.
There is a sequence of integers, which contains the equal number of even and odd numbers. Given a limited budget, you need to make maximum possible number of cuts such that each resulting segment will have the same number of odd and even integers.
Cuts separate a sequence to continuous (contiguous) segments. You may think about each cut as a break between two adjacent elements in a sequence. So after cutting each element belongs to exactly one segment. Say, [4,1,2,3,4,5,4,4,5,5][4,1,2,3,4,5,4,4,5,5] →→ two cuts →→ [4,1|2,3,4,5|4,4,5,5][4,1|2,3,4,5|4,4,5,5]. On each segment the number of even elements should be equal to the number of odd elements.
The cost of the cut between xx and yy numbers is |x−y||x−y| bitcoins. Find the maximum possible number of cuts that can be made while spending no more than BB bitcoins.
First line of the input contains an integer nn (2≤n≤1002≤n≤100) and an integer BB (1≤B≤1001≤B≤100) — the number of elements in the sequence and the number of bitcoins you have.
Second line contains nn integers: a1a1, a2a2, ..., anan (1≤ai≤1001≤ai≤100) — elements of the sequence, which contains the equal number of even and odd numbers
Print the maximum possible number of cuts which can be made while spending no more than BB bitcoins.
6 41 2 5 10 15 20
1
4 101 3 2 4
0
6 1001 2 3 4 5 6
2
In the first sample the optimal answer is to split sequence between 22 and 55. Price of this cut is equal to 33 bitcoins.
In the second sample it is not possible to make even one cut even with unlimited number of bitcoins.
In the third sample the sequence should be cut between 22 and 33, and between 44 and 55. The total price of the cuts is 1+1=21+1=2 bitcoins.
在花费不超过B的情况下,求最大可以有多少到切口,每切一次左右两边要保存奇数和偶数一样。
先找出可以切的位置,记下花费,然后用01背包问题计算不超过B的情况下最大有多少个切口。
1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 const int N = 110; 5 int a[N], b[N], n, B; 6 int dp[N]; 7 int main() { 8 cin >> n >> B; 9 for(int i = 1; i <= n; i ++) cin >> a[i]; 10 int ans1 = 0, ans2 = 0, cnt = 0; 11 for(int i = 1; i < n; i ++) { 12 if(a[i]&1) ans1++; 13 else ans2++; 14 if(ans1 == ans2) { 15 b[cnt++] = abs(a[i]-a[i+1]); 16 } 17 } 18 for(int i = 0; i < cnt; i ++) { 19 for(int j = B; j >= b[i]; j --) { 20 dp[j] = max(dp[j], dp[j-b[i]]+1); 21 } 22 } 23 cout << dp[B] << endl; 24 return 0; 25 } 26
C. Convert to Ones
You've got a string a1,a2,…,ana1,a2,…,an, consisting of zeros and ones.
Let's call a sequence of consecutive elements ai,ai+1,…,ajai,ai + 1,…, aj (1≤i≤j≤n1≤ i≤ j≤ n) a substring of string aa.
You can apply the following operations any number of times:
- Choose some substring of string aa (for example, you can choose entire string) and reverse it, paying xx coins for it (for example, «0101101» →→ «0111001»);
- Choose some substring of string aa (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying yy coins for it (for example, «0101101» →→ «0110001»).
You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring.
What is the minimum number of coins you need to spend to get a string consisting only of ones?
The first line of input contains integers nn, xx and yy (1≤n≤300000,0≤x,y≤1091 ≤ n ≤ 300000,0≤x,y≤109) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring).
The second line contains the string aa of length nn, consisting of zeros and ones.
Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 00, if you do not need to perform any operations.
5 1 1001000
11
5 10 101000
2
7 2 31111111
0
In the first sample, at first you need to reverse substring [1…2][1…2], and then you need to invert substring [2…5][2…5].
Then the string was changed as follows:
«01000» →→ «10000» →→ «11111».
The total cost of operations is 1+10=111+10=11.
In the second sample, at first you need to invert substring [1…1][1…1], and then you need to invert substring [3…5][3…5].
Then the string was changed as follows:
«01000» →→ «11000» →→ «11111».
The overall cost is 1+1=21+1=2.
In the third example, string already consists only of ones, so the answer is 00.
可以转化为求连续的0有多少个
当x <= y 时,肯定要让所有连续的0变成只有一个连续的0,假设连续的0有ans个,答案就是(ans-1)*x + y
当x > y 时,就让所有连续的0全变成1,答案就是ans*y
1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 const int N = 3e5+10; 5 char str[N]; 6 int main() { 7 ll n, x, y, ans = 0, tmp = 0; 8 cin >> n >> x >> y; 9 cin >> str+1; 10 for(int i = 1; i <= n; i ++) { 11 if(str[i] == '0') { 12 tmp++; 13 while(str[i] == '0' && i <= n)i++; 14 } 15 } 16 //cout << tmp << endl; 17 if(tmp == 0) return 0*printf("0\n"); 18 if(x >= y) { 19 cout << y*tmp << endl; 20 } else { 21 cout << x*(tmp-1)+y << endl; 22 } 23 return 0; 24 }
D. Roman Digits
Let's introduce a number system which is based on a roman digits. There are digits I, V, X, L which correspond to the numbers 11, 55, 1010and 5050 respectively. The use of other roman digits is not allowed.
Numbers in this system are written as a sequence of one or more digits. We define the value of the sequence simply as the sum of digits in it.
For example, the number XXXV evaluates to 3535 and the number IXI — to 1212.
Pay attention to the difference to the traditional roman system — in our system any sequence of digits is valid, moreover the order of digits doesn't matter, for example IX means 1111, not 99.
One can notice that this system is ambiguous, and some numbers can be written in many different ways. Your goal is to determine how many distinct integers can be represented by exactly nn roman digits I, V, X, L.
The only line of the input file contains a single integer nn (1≤n≤1091≤n≤109) — the number of roman digits to use.
Output a single integer — the number of distinct integers which can be represented using nn roman digits exactly.
1
4
2
10
10
244
In the first sample there are exactly 44 integers which can be represented — I, V, X and L.
In the second sample it is possible to represent integers 22 (II), 66 (VI), 1010 (VV), 1111 (XI), 1515 (XV), 2020 (XX), 5151 (IL), 5555 (VL), 6060 (XL) and 100100 (LL).
先打表求下前50个,可以看出有规律,N>11 时答案就是 a[11] + (n-11)*49
1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 ll a[] = {0,4, 10, 20, 35, 56, 83, 116, 155, 198, 244, 292}; 5 6 int main() { 7 ll n; 8 cin >> n; 9 if(n <= 11) printf("%lld\n",a[n]); 10 else printf("%lld\n",a[11]+(n-11)*49); 11 return 0; 12 }
来源:https://www.cnblogs.com/xingkongyihao/p/9253348.html