[ACM International Collegiate Programming Contest, Amman Collegiate Programming Contest (2018)]

A. Careful Thief
time limit per test
2.5 s
memory limit per test
256 MB
input
standard input
output
standard output

There are consecutive buildings numbered from 1 to 109 each of which has an amount of money in it. The money is given in the form of mnon-overlapping segments, in which each segment i has 3 values: liri, and vi, meaning that each building with number x (li ≤ x ≤ ri) has vi amount of money.

A thief came to rob the city, however, this thief does not want to get caught, so he can only rob at most k consecutive buildings, such that if he starts robbing buildings from building z, he can rob all buildings in the range  inclusive.

Your task is to find the maximum amount of money the thief can collect. Can you?

Input

The first line contains an integer T (1 ≤ T ≤ 100) specifying the number of test cases.

The first line of each test case contains two integers m and k (1 ≤ m ≤ 1e5, 1 ≤ k ≤ 1e9), in which m is the number money segments, and k is the maximum number of consecutive buildings the thief can rob.

Then m lines follow, giving the description of money segments. Each segment i is represented by 3 integers liri and vi (1 ≤ li ≤ ri ≤ 1e9, 1 ≤ vi ≤ 1e9), in which li and ri are the boundaries of the segment, and vi is the amount of money in each building in that segment.

The sum of m overall test cases does not exceed 2 × 1e6.

Output

For each test case, print a single line containing the maximum amount of money the thief can collect by robbing at most k consecutive buildings.

Example
input
1
2 5
1 5 1
7 11 1
output
5
题意:数轴上有1e9个点,有n(1e5)个区间,每个区间内所有点有一个相同的值ai,你可以从任意一个点开始取连续的k个数,求最大总值。
题解:易知,总值最大时的取法一定是起点在某个区间的起点或者终点在某个区间的终点(因为如果不这样取,总可以通过向左或向右移动得到更优的解),而区间的个数是1e5的数量级,所以可以直接枚举区间起点利用二分找到相应终点求出一个最优解,再通过直接枚举区间终点利用二分找到相应起点来求出另一个最优解,取二者最小值即可。
注意一些细节即可。(使用lowerbound和upperbound时注意数组边界)
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<map>
 5 #include<cstdio>
 6 #include<algorithm>
 7 using namespace std;
 8 typedef long long ll;
 9 struct edge{
10     ll l;
11     ll r;
12     ll val;
13 }e[100005];
14 ll sum[100005];
15 ll id[100005],id2[100005];
16 bool cmp(struct edge aa,struct edge bb){
17     return aa.l<bb.l;
18 }
19 int main(){
20     int t;
21     scanf("%d",&t);
22     while(t--){
23         ll m,k;
24         scanf("%lld%lld",&m,&k);
25         for(int i=1;i<=m;i++)scanf("%lld%lld%lld",&e[i].l,&e[i].r,&e[i].val);
26         sort(e+1,e+1+m,cmp);
27         for(int i=1;i<=m;i++){
28             sum[i]=sum[i-1]+e[i].val*(e[i].r-e[i].l+1);
29             id[i]=e[i].r;
30             id2[m-i+1]=-e[i].l;
31         }
32         /*
33         1
34 2 5
35 1 4 2
36 6 7 1
37         */
38         ll ans=0;
39         for(int i=1;i<=m;i++){
40             int xx=lower_bound(id+1,id+1+m,e[i].l+k-1)-id;
41             //cout<<xx<<endl;
42             ll s=sum[xx-1]-sum[i-1];
43             if(xx<=m)s+=(max(0ll,min(e[i].l+k-1,id[xx])-e[xx].l+1))*e[xx].val;
44             ans=max(s,ans);
45         }
46         for(int i=m;i>=1;i--){
47             int xx=lower_bound(id2+1,id2+1+m,-e[i].r+k-1)-id2;
48             ll s=sum[i]-sum[m-(xx-1)];
49             if(xx<=m)s+=max(0ll,(min(-e[i].r+k-1,id2[xx])-(-e[m-xx+1].r)+1))*e[m-xx+1].val;
50             ans=max(s,ans);
51         }
52         printf("%lld\n",ans);
53     }
54     return 0;
55 }
View Code
B. Friends and Cookies
time limit per test
1.0 s
memory limit per test
256 MB
input
standard input
output
standard output

Abood's birthday has come, and his n friends are aligned in a single line from 1 to n, waiting for their cookies, Abood has x cookies to give to his friends.

Here is an example to understand how Abood gives away the cookies. Suppose Abood has 4 friends and x cookies, then Abood will do the following:

  1. Give a cookie to the 1st friend.
  2. Give a cookie to the 2nd friend.
  3. Give a cookie to the 3rd friend.
  4. Give a cookie to the 4th friend.
  5. Give a cookie to the 3rd friend.
  6. Give a cookie to the 2nd friend.
  7. Give a cookie to the 1st friend.
  8. Give a cookie to the 2nd friend.
  9. And so on until all the x cookies are given away.

Your task is to find how many cookies each friend will get. Can you?

Input

The first line contains an integer T (1 ≤ T ≤ 100) specifying the number of test cases.

Each test case consists of a single line containing two integers x and n (1 ≤ x ≤ 1e18, 1 ≤ n ≤ 1000), in which x is the number of cookies Abood has, and n is the number of his friends.

Output

For each test case, print a single line containing n space-separated integers a1, ..., an, in which ai represents how many cookies the ithfriend got.

Example
input
1
5 3
output
2 2 1
题意:按题目要求分饼干。
题解:按要求模拟即可
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<map>
 5 #include<cstdio>
 6 #include<algorithm>
 7 using namespace std;
 8 typedef long long ll;
 9 ll a[1005];
10 int main(){
11     int t;
12     scanf("%d",&t);
13     while(t--){
14         ll x,n;
15         scanf("%lld%lld",&x,&n);
16         if(n==1){
17             printf("%lld\n",x);
18         }
19         else{if(x<=n){
20             for(int i=1;i<=n;i++){
21                 int s=(i<=x);
22                 printf("%d",s);
23                 char cc=(i==n)?'\n':' ';
24                 printf("%c",cc);
25             }
26         }
27         else{
28             x-=n;
29             ll q=x/(n-1);
30             if(q%2){
31                 a[1]=((q+1)/2)+1;
32                 a[n]=(q/2)+1;
33                 for(int i=2;i<=1+x%(n-1);i++){
34                     a[i]=q+2;
35                 }
36                 for(int i=2+x%(n-1);i<=n-1;i++){
37                     a[i]=q+1;
38                 }
39             }
40             else{
41                 a[1]=((q)/2)+1;
42                 a[n]=(q/2)+1;
43                 for(int i=n-1;i>=n-x%(n-1);i--){
44                     a[i]=q+2;
45                 }
46                 for(int i=n-x%(n-1)-1;i>=2;i--){
47                     a[i]=q+1;
48                 }
49             }
50             for(int i=1;i<=n;i++){
51                 printf("%lld",a[i]);
52                 char cc=(i==n)?'\n':' ';
53                 printf("%c",cc);
54             }
55         }
56         }
57     }
58     return 0;
59 }
View Code
C. Flip the Bits
time limit per test
1.0 s
memory limit per test
256 MB
input
standard input
output
standard output

You are given a positive integer n. Your task is to build a number m by flipping the minimum number of bits in the binary representation of nsuch that m is less than n (m < n) and it is as maximal as possible. Can you?

Input

The first line contains an integer T (1 ≤ T ≤ 1e5) specifying the number of test cases.

Each test case consists of a single line containing one integer n (1 ≤ n ≤ 1e9), as described in the statement above.

Output

For each test case, print a single line containing the minimum number of bits you need to flip in the binary representation of n to build the number m.

Example
input
2
5
10
output
1
2
题意:求x与x-1二进制下的不同数位的个数。
题解:异或之后直接求即可
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<map>
 5 #include<cstdio>
 6 #include<algorithm>
 7 using namespace std;
 8 typedef long long ll;
 9 ll a[1005];
10 int main(){
11     int t;
12     scanf("%d",&t);
13     while(t--){
14         int n;
15         scanf("%d",&n);
16         int k=(n-1)^n;
17         int ans=0;
18         while(k){
19             if(k&1)ans++;
20             k/=2;
21         }
22         printf("%d\n",ans);
23     }
24     return 0;
25 }
View Code
D. Magic Sticks
time limit per test
1.0 s
memory limit per test
256 MB
input
standard input
output
standard output

You are given a n × m grid, your goal is to find a group of lines such that the following conditions are met:

  1. No two lines are touching.
  2. Each cell in the grid has one of its sides covered by at least one line in the group.

A line is a border of a cell in the grid with length 1. Each cell has 4 lines covering every side of it. Every pair of neighboring cells shares a line. Two lines are touching if they meet at their ends.

The size of a group of lines is the number of lines it contains. Your task is to find the minimum size of a group of lines that meet all the conditions above. Can you?

Input

The first line contains an integer T (1 ≤ T ≤ 128) specifying the number of test cases.

Each test case consists of a single line containing two integers n and m (1 ≤ n ≤ 1e9, 1 ≤ m ≤ 1024), giving a grid of size n × m.

Output

For each test case, print a single line containing the minimum size of a group of lines that meet all the conditions in the statement above.

Example
input
1
4 4
output
10
题意:一个n*m的矩形,里面一共有n*m个格子,要求每个格子至少一条边被标记,并且所有被标记的边不能相交,求最少要标记多少边。
题解:这个我完全是画出样例的情形图之后猜公式..
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<map>
 5 #include<cstdio>
 6 #include<algorithm>
 7 using namespace std;
 8 typedef long long ll;
 9 const int mod=1e9+7;
10 ll n,m;
11 ll sol(ll n,ll m){
12     return n*((m+1)/2)+((m+1)%2)*((n)/2);
13 }
14 int main(){
15     int t;
16     scanf("%d",&t);
17     while(t--){
18         scanf("%lld%lld",&n,&m);
19 
20         ll xx=min(sol(n,m),sol(m,n));
21         printf("%lld\n",xx);
22 
23     }
24     return 0;
25 }
View Code
E. N-Dimensional Grid
time limit per test
2.5 s
memory limit per test
256 MB
input
standard input
output
standard output

You are given an n-dimensional grid in which the dimensions of the grid are a1 × a2 × ... × an. Each cell in the grid is represented as an n-tuple (x1, x2, ..., xn) (1 ≤ xi ≤ ai).

Two cells are considered to be adjacent if the Manhattan Distance between them is equal to 1. The Manhattan Distancebetween two cells X(x1, x2, ..., xn) and Y(y1, y2, ..., yn) is equal to: |x1 - y1| + |x2 - y2| + ... + |xn - yn|.

Your task is to count how many pairs of cells are adjacents. Can you? Two pairs of cells are considered the same if they include the same cells, i.e the pair (c1, c2) is the same as (c2, c1).

Input

The first line contains an integer T (1 ≤ T ≤ 100) specifying the number of test cases.

The first line of each test case contains an integer n (1 ≤ n ≤ 1e5), in which n is the number of dimensions of the grid. Then a line follows containing n integers a1, ..., an (1 ≤ ai ≤ 1e5), in which ai is the size of the ith dimension.

The sum of n overall test cases does not exceed 6 × 1e6.

Output

For each test case, print a single line containing the number of pairs of adjacent cells modulo 1e9 + 7.

Example
input
1
3
1 2 3
output
7
Note

The absolute value |x| of a real number x is the non-negative value of x without regard to its sign. Namely, |x| = x for a positive x|x| =  - xfor a negative x (in which case  - x is positive), and |0| = 0. For example, the absolute value of 3 is 3, and the absolute value of  - 3 is also 3. The absolute value of a number may be thought of as its distance from zero.

题意:一共n个数列,每个数列中都有ai个值,分别为1,2,3...ai,在每个数列中选两个值分别放到两个队列中,则最后两个队列中各有n个值,要求最后两个队列的对应元素相减的绝对值之和=1,求总情况数%1e9+7.

题解:由于绝对值之和的最后结果需要=1,所以只能是两个队列某个位置的差值为1,其他对应元素值为0,所以共有$\sum_{i=1}^n$(($\sum_{j=1}^n$aj)/ai)*(ai-1)种方案

猜你喜欢

转载自www.cnblogs.com/MekakuCityActor/p/10651180.html