A.Teemo's bad day

Today is a bad day. Teemo is scolded badly by his teacher because he didn't do his homework.But Teemo is very self-confident, he tells the teacher that the problems in the homework are too simple to solve. So the teacher gets much angrier and says"I will choose a problem in the homework, if you can't solve it, I will call you mother! "

The problem is that:

There is an array A which contains n integers, and an array B which also contains n integers. You can pay one dollar to buy a card which contains two integers a1 and a2, The card can arbitrary number of times transform a single integer a1 to a2 and vise-versa on both array A and Array B. Please calculate the minimum dollars you should pay to make the two array same(For every 1<=i<=n,A[i]=B[i]);

Input Format

  • The first line of the input contains an integer T(1<=T<=10), giving the number of test cases.
  • For every test case, the first line contains an integer n(1<=n<=500000). The second line contains n integers. The i th integer represents A[i](1<=A[i]<=100000). And the third line contains n integers. The i th integer represents B[i](1<=B[i]<=100000).

Output Format

For each test case, output an integer which means the minimum dollars you should pay in a line.

样例输入

1
5
1 1 2 3 2
1 2 3 1 1

样例输出

2
将不匹配的联通跑bfs就可以
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#pragma GCC diagnostic error "-std=c++11"
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define esp 1e-9
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
int dcmp(double x){return fabs(x)<esp?:x<?-:;}
typedef long long ll;
int t,n,a[],b[];
int vis[],c[];
vector<int>v[];
void dfs(int u){
vis[u]=;
for(auto t:v[u]){
if(!vis[t]) dfs(t);
}
}
int main()
{
scanf("%d",&t);
while(t--)
{
memset(vis,,sizeof(vis));
memset(c,,sizeof(c));
scanf("%d",&n);
for(int i=;i<=;i++)
v[i].clear();
for(int i=;i<n;i++) scanf("%d",&a[i]);
for(int i=;i<n;i++){
scanf("%d",&b[i]);
if(a[i]==b[i]) continue;
c[a[i]]=;
c[b[i]]=;
v[a[i]].push_back(b[i]);
v[b[i]].push_back(a[i]);
}
int ans=,pos=;
for(int i=;i<=;i++)
{
if(c[i]){
pos++;
if(!vis[i]){
dfs(i);
ans++;
}
}
}
printf("%d\n",pos-ans);
}
return ;
}

B.Teemo's hard problem

Teemo starts to do homework everyday. Today, he meets a hard problem when doing his homework.

There's an array A which contains n integers(for every 1<=i<=n, A[i] = 1 or A[i]= 2), you can choose an interval [l,r](1<=l<=r<=n), then reverse it so that the length of the longest non-decreasing subsequence of the new sequence is maximum.

Input Format

  • The first line of the input contains an integer T(1=<T<=10), giving the number of test cases.
  • For every test case, the first line contains an integer n(1<=n<=2000). The second line contains n integers. The i th integer represents A[i](1<=A[i]<=2).

Output Format

Print a single integer, which means the maximum possible length of the longest non-decreasing subsequence of the new sequence.

样例输入

1
4
1 2 1 2

样例输出

4
暴力求出正反最长上升子序列,枚举区间
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#pragma GCC diagnostic error "-std=c++11"
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define esp 1e-9
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
int dcmp(double x){return fabs(x)<esp?:x<?-:;}
typedef long long ll;
int t,n,a[];
int dpf[][],dpl[][];
int b[],ans,pos;
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
memset(dpf,,sizeof(dpf));
memset(dpl,,sizeof(dpl));
for(int i=;i<=n;i++)
{
pos=;
memset(b,,sizeof(b));
for(int j=i;j<=n;j++)
{
if(a[j]>=b[pos]) b[++pos]=a[j];
else
{
int k=upper_bound(b+,b+pos+,a[j])-b;
b[k]=a[j];
}
dpf[i][j]=pos;
}
}
for(int i=n;i;i--)
{
pos=;
memset(b,,sizeof(b));
for(int j=i;j;j--)
{
if(a[j]>=b[pos]) b[++pos]=a[j];
else
{
int k=upper_bound(b+,b+pos+,a[j])-b;
b[k]=a[j];
}
dpl[j][i]=pos;
}
}
ans=;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
ans=max(ans,dpf[][n]-dpf[i][j]+dpl[i][j]);
printf("%d\n",ans);
}
return ;
}

C.Teemo's tree problem

There is an apple tree in Teemo's yard. It contains n nodes and n-1 branches, and the node 1 is always the root of the tree. Today, Teemo's father will go out for work. So Teemo should do his father's job in the family: Cut some branches to make the tree more beautiful. His father's told him that he should cut some branches, finally, the tree should just contains q branches. But when Teemo start to cut, he realizes that there are some apples in the branches( For example, there are 10 apples in the branches which connecting node 1 and node 4). So Teemo not only wants to achieve his father's order, but also wants to preserve apples as much as possible. Can you help him?

 
1
2   5
2
 \ / 
3
  3   4
4
   \ /
5
    1

Input Format

  • The first line of the input contains an integer T(1<=T<=10) which means the number of test cases.
  • For
    each test case, The first line of the input contains two integers
    n,q(3<=n<=100,1<=q<=n-1), giving the number of the node and
    the number of branches that the tree should preserve.
  • In the
    next n-1 line, each line contains three integers
    u,v,w(1<=u<=n,1<=v<=n,u!=v,1<=w<=100000), which means
    there is a branch connecting node u and node v, and there are w apple(s)
    on it.

Output Format

Print a single integer, which means the maximum possible number of apples can be preserved.

样例输入

1
5 2
1 3 1
1 4 10
2 3 20
3 5 20

样例输出

21
树型DP,只有n-1边,一遍dfs,把边的权值赋给子节点,1结点赋成无限大
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <ext/rope>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#pragma GCC diagnostic error "-std=c++11"
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define esp 1e-9
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 1044266560
#define mem(a) (memset(a,0,sizeof(a)))
int dcmp(double x){return fabs(x)<esp?:x<?-:;}
typedef long long ll;
typedef pair<int,int> P;
int n,m,t,f[][];
int vis[],val[],ans;
vector<P>v[];
void solve(int u)
{
vis[u]=;
for(int i=;i<v[u].size();i++)
{
if(!vis[v[u][i].first]){
val[v[u][i].first]=v[u][i].second;
solve(v[u][i].first);
}
}
}
int dfs(int u,int fa)
{
vis[u]=;
for(int i=;i<v[u].size();i++)
{
if(v[u][i].first==fa) continue;
vis[u]+=dfs(v[u][i].first,u);
}
f[u][]=val[u];
for(int i=;i<v[u].size();i++){
if(v[u][i].first==fa) continue;
for(int j=vis[u];j>=;j--){
for(int k=;k<j && k<=vis[v[u][i].first];k++)
f[u][j]=max(f[u][j],f[u][j-k]+f[v[u][i].first][k]);
}
}
if(vis[u]>=m) ans=max(ans,f[u][m]);
return vis[u];
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
memset(vis,,sizeof(vis));
memset(val,,sizeof(val));
memset(f,,sizeof(f));
for(int i=;i<=n;i++)
v[i].clear();
for(int i=,x,y,z;i<n;i++)
{
scanf("%d%d%d",&x,&y,&z);
v[x].push_back(P(y,z));
v[y].push_back(P(x,z));
}
solve();
memset(vis,,sizeof(vis));
val[]=;
m++;
ans=-;
dfs(,);
printf("%d\n",ans-val[]);
}
return ;
}

F.Teemo's dream

Teemo decides to use his money to conquer the universe.

It is known that there are m planets that humans can reach at present. They are numbered from 1 to m. Teemo bought n kinds of gateways. Their IDs are a1, a2, ..., an, the gateway whose ID is ai can transmit Teemo to the stars numbered ai,2ai, 3ai, ..., k*ai (1<=k*ai<=m, k is a positive integer), now Teemo wants to know, how many planets can he reach?

Input Format

On the firstline one positive number: the number of test cases, at most 20. After that per test case:

  • One line contains two integers n and m, (1 <= n <= 15, 1<= m < = 1e9), respectively represent the number of the gateway, the number of the stars that humans can reach.
  • One line contains integers, the i-th integer a[i], indicating that the ID of the  i-th gateway is a[i], (2<=a[i]<=1e9).

Ouput Format

Per test case:

  • One line contains an integer, which indicates how many planets Teemo can reach at most.

容斥定理

1到m包含多少个a[i]的倍数

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#pragma GCC diagnostic error "-std=c++11"
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define esp 1e-9
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
int dcmp(double x){return fabs(x)<esp?:x<?-:;}
typedef long long ll;
ll t,n,m;
ll a[],b[],ans=;
ll lcm(ll x,ll y){
return x/__gcd(x,y)*y;
}
void solve(int n,int l,int r,int k)
{
for(int i=n;i>=l;i--)
{
b[l-]=a[i-];
if(l>) solve(i-,l-,r,k);
else{
ll pos=lcm(b[r-],b[r-]);
for(int j=r-;j>=;j--)
{
pos=lcm(pos,b[j]);
if(pos>m) goto eg;
}
if(k) ans+=m/pos;
else ans-=m/pos;
eg:;
}
}
}
int main()
{
scanf("%lld",&t);
while(t--)
{
scanf("%lld%lld",&n,&m);
ans=;
for(int i=;i<n;i++)
{
scanf("%lld",&a[i]);
ans+=m/a[i];
}
for(int i=;i<=n;i++)
solve(n,i,i,i&);
printf("%lld\n",ans);
}
return ;
}

G.Teemo's convex polygon

Teemo is very interested in convex polygon. There is a convex n-sides polygon, and Teemo connect every two points as diagonal lines, and he want to kown how many segments which be divided into intersections. Teemo ensure that any three diagonals do not intersect at a point.

As the result may be very large, please output the result mod 1000000007.

Input Format

The
input contains several test cases, and the first line is a positive
integer T indicating the number of test cases which is up to 100.

For each test case, the first line contains an integer n(3<=n<=10^18).

Output Format

For each test case, output a line containing an integer that indicates the answer.

样例输入

2
3
4

样例输出

0
4
数据保证没有三条线段交于一点,公式为(n^4-6n^3+17n^2-24n)/12
import java.math.BigInteger;
import java.util.Scanner;
public class Main{
static Scanner cin = new Scanner(System.in);
static BigInteger[] a = new BigInteger[10];
public static void main(String args[]){
long n,m;
m = cin.nextLong();
while(m>0){
m =m-1;
n = cin.nextLong();
a[1] = BigInteger.valueOf(n);
a[2] = a[1].multiply(BigInteger.valueOf(n));
a[3] = a[2].multiply(BigInteger.valueOf(n));
a[4] = a[3].multiply(BigInteger.valueOf(n));
a[3] = a[3].multiply(BigInteger.valueOf(6));
a[2] = a[2].multiply(BigInteger.valueOf(17));
a[1] = a[1].multiply(BigInteger.valueOf(24));
a[5] = a[4].subtract(a[3]);
a[5] = a[5].add(a[2]);
a[5] = a[5].subtract(a[1]);
a[5] = a[5].divide(BigInteger.valueOf(12));
System.out.println(a[5].mod(BigInteger.valueOf(1000000007)));
}
}
}

J.Teemo's formula

Teemo has a formula and he want to calculate it quickly.

The formula is .

As the result may be very large, please output the result mod 1000000007.

Input Format

The
input contains several test cases, and the first line is a positive
integer T indicating the number of test cases which is up to 10^5.

For each test case, the first line contains an integer n(1<=n<=10^9).

Output Format

For each test case, output a line containing an integer that indicates the answer.

样例输入

2
2
3

样例输出

6
24
组合公式n(n+1)2^(n-2)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <ext/rope>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#pragma GCC diagnostic error "-std=c++11"
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define esp 1e-9
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 1044266560
#define mem(a) (memset(a,0,sizeof(a)))
int dcmp(double x){return fabs(x)<esp?:x<?-:;}
typedef long long ll;
ll n,t;
ll quick_pow(ll x,ll y)
{
ll ans=;
while(y){
if(y&) ans=ans*x%MOD;
y>>=;
x=x*x%MOD;
}
return ans;
}
int main()
{
scanf("%lld",&t);
while(t--)
{
scanf("%lld",&n);
if(n==) {printf("1\n");continue;}
printf("%lld\n",n*(n+)%MOD*quick_pow(,n-)%MOD);
}
return ;
}

K.Teemo's reunited

Teemo likes to drink raspberry juice.  He even spent some of his spare time tomake the raspberry juice himself. The way to make the raspberries juice is simple. You just have to press the raspberries through a fine sieve.

Unfortunately,today Teemo was splited in several pieces by the sieve which was used to makethe raspberry juice. The pieces were losted in the huge two-dimensional map. Onlywhen all the pieces gather, can Teemo drink the raspberry juice he made today.

Teemo's piece can only move parallel to the x or y axis, or he would be hated by theraspberry Queen and will not be able to have raspberry juice any more. One of the piece of Teemo should carry the raspberry juice.In order to avoid spilling, this piece cannot move anymore.

Teemo’spiece are lazy, they’d like to make the sum of paths be the minimal. Your task is to find the minimal sum of the paths.

InputFormat
The
first line contains a integer n (1<=n<=100000) represent the
number of thepieces. Then next n lines. Each line contains the pairs of
xi, yi(-1000000000<xi,yi<1000000000) in turn as points by order.

OutputFormat
Printa single line contains the minimal sum of the paths.

样例输入1

3
1 0
2 0
3 0

样例输出1

2

样例输入2

5
4 1
4 4
9 2
2 9
2 6

样例输出2

21
在这些点中找一个点使得所有的点到此点的曼哈顿距离最小,分别对x,y排序
枚举x,y找出不同的x,y对应的最小值,最后枚举点使得x+y最小
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#pragma GCC diagnostic error "-std=c++11"
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define esp 1e-9
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
int dcmp(double x){return fabs(x)<esp?:x<?-:;}
typedef long long ll;
const int lower=;
struct Point
{
ll x,y,num;
}e[];
ll sum_x[],sum_y[],n,sumx=,sumy=;
ll l[],r[];
bool cmpx(const Point &a,const Point &b){return a.x<b.x;}
bool cmpy(const Point &a,const Point &b){return a.y<b.y;}
int main()
{
scanf("%lld",&n);
for(int i=;i<=n;i++)
{
scanf("%lld%lld",&e[i].x,&e[i].y);
e[i].x+=lower;
e[i].y+=lower;
e[i].num=i;
sumx+=e[i].x;
sumy+=e[i].y;
}
sort(e+,e++n,cmpx);
for(int i=;i<=n;i++)
sum_x[i]=sum_x[i-]+e[i].x;
for(int i=;i<=n;i++)
{
ll x=e[i].x;
ll left=(i-)*x-sum_x[i-];
ll right=sumx-sum_x[i]-(n-i)*x;
l[e[i].num]=left+right;
}
sort(e+,e++n,cmpy);
for(int i=;i<=n;i++)
sum_y[i]=sum_y[i-]+e[i].y;
for(int i=;i<=n;i++)
{
ll y=e[i].y;
ll left=(i-)*y-sum_y[i-];
ll right=sumy-sum_y[i]-(n-i)*y;
r[e[i].num]=left+right;
}
ll inf=l[]+r[];
for(int i=;i<=n;i++)
inf=min(inf,l[i]+r[i]);
printf("%lld\n",inf);
return ;
}

最新文章

  1. CentOS 7.1编译安装PHP7
  2. GridControl读取xml和保存xml
  3. Microsoft.Office.Interop.Word 创建word
  4. github android
  5. Angular学习
  6. C安全编码--整数理解
  7. TortoiseGit使用与操作
  8. 152. Maximum Product Subarray
  9. careercup-排序和查找 11.6
  10. TCP/UDP基本概念部分
  11. oracle系统参数修改
  12. Mac OS下SVN的使用:服务的和客户端
  13. SpringBoot配置devtools实现热部署
  14. 《第一行代码-李兴华》-10.3 System类
  15. java线程学习之线程创建
  16. using python to compute production rules
  17. MVC Helper Methods
  18. greenDao 介绍
  19. eclipse 创建普通maven项目
  20. spring 优点

热门文章

  1. UI组件之TextView及其子类(一)TextView和EditText
  2. bzoj3442: 学习小组(费用流好题)
  3. vc应用CPictureEx类(重载CStatic类)加载gif动画
  4. 一个比NPM更快更安全可靠的JavaScript包管理工具——Yarn
  5. 记录一下c++的一点指针所得
  6. 搞定ubuntu下环境变量的配置
  7. mount ntfs 失败解决办法
  8. TYVJ 1541 八数码
  9. vue keep-alive保存路由状态2 (高级用法,接上篇)
  10. MySQL日期数据类型和时间类型使用总结