Time Limit:1s Memory Limit:1024MByte
Submissions:593Solved:187
In a mysterious cave, PigVan ( Mr.Van's pet ) has lived for thousands years. PigVan absorbed the power of nature, and it may pretend a human in speaking, walking and so on.
One day, he bought some valuable stone, and divided them into n piles of stone where the ith pile (1≤i≤n) contains valuesai .
After PigVan put them in a line, he wants to play a game.
In the boring game, he can do this operation:
Choose a stone pile ai (i>1)and its two adjacent piles ai-1, ai+1, turn(ai-1, ai, ai+1) to(ai-1 + ai, -ai, ai + ai+1).
PigVan wonders whether he can get (b1, b2, b3, …, bn) after several operations.
Note:
If you choose the last pile an, the operation will be( an-1 + an, -an ) .
For each test case:
In the first line, there are only one integer nn(n≤10 5), indicating the number of food piles.
The second line is nn integers indicate sequence aa ( | a i | ≤ 10 6).
The third line is nn integers indicate sequence bb ( | b i | ≤ 10 6).
Yes
No
考虑一次操作
(ai-1, ai, ai+1)->(ai-1 + ai, -ai, ai+1 + ai)
如果考虑前缀和,那么一次操作等效为
(si-1, si, si+1)->(si, si-1, si+1)
即对于前缀和而言,他只是交换了位置。
因此,我们只需求出前缀和,然后看元素是否对等就行了。
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
using namespace std;
int a[100006];
int b[100006];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
scanf("%d",&a[0]);
for(int i=1;i<n;i++)
{
scanf("%d",&a[i]);
a[i]+=a[i-1];
}
scanf("%d",&b[0]);
for(int i=1;i<n;i++)
{
scanf("%d",&b[i]);
b[i]+=b[i-1];
}
sort(a,a+n);
sort(b,b+n);
int m=-1;
for(int i=0;i<n;i++)
{
if(a[i]!=b[i])
{
m=1;
break;
}
}
if(m==1)
printf("No\n");
else
printf("Yes\n");
}
}
Time Limit:10s Memory Limit:1024MByte
Submissions:342Solved:133
There are n coins in a line, indexed from 0 to n-1. Each coin has its ownvalue.You are asked to pick up all the coins to get maximum value. If the you pickcoini (1 ≤ i ≤n-2), you will getvalue[left]×value[i]×value[right]value[left]×value[i]×value[right] coin value. Here left and right are adjacent indices of i. After the picked, the left and right then becomes adjacent.
Note.
If you pick the first coin, you can assume the value[left] is 1.
If you pick the last coin, you can assume the value[right] is 1.
Find the maximum value when you pick up all the coins.
For each test case:
The first line contains a number nn (3≤ nn ≤10 3) — The number of magic coins.
The second line contains nn numbers ( all numbers ≤10) — The value of each coin.
区间动态规划, dp[i,j]表示捡起 i~j 区间的硬币, 有:
dp[i,j] = max{ dp[i+1,k-1] + dp[k+1, j-1] + value[i] x value[k] x value[j] }
Brige
Source : - Sealed - | |||
Time limit : 2 sec | Memory limit : 131072 M |
Submitted : 15, Accepted : 1
Description
Long long ago, there were two tribes called Radiant and Dire.In each tribe, there are many heroes who were aggressive that they wanted to fight with the heroes in the other tribe. Unfortunately, a river stoped their pace of attack.So the elders of the two tribes had a negotiation with each other. Finally, they decided to build some bridges. They gave the solutions of building bridges with the coordinate of different sides of the bridge and the cost of it. To prevent confusion, any of the two bridges could not cut across each other(expect on the endpoints of the bridges). And they wanted to know how many bridges they could build at most, and under this condition, what is the minimum cost.Input
The first line contains an integer T, indicating the total number of test cases(T<=10). First line of each test case contains a number n(n<=10^4). Then following n lines contains three integer xi yi vi(1<=xi,yi,vi<=10^4).It is guaranteed that there are no two bridges have the same xi and yi.Output
For each test case, output one line containing "Case #t: a b", where t is the test case number(starting from 1),a is the maximum number of bridges, and b is the minimum cost of building the bridges.Sample Input
1 3 3 1 3 1 2 2 4 5 6
Sample Output
Case #1: 2 8