链接:http://icpc.upc.edu.cn/contest.php?cid=1776
A:括号匹配2019
猜的,证明等会了再补上
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=110;
const ll mod=1e9+7;
int n,k;
char s[100100];
int main() {
while(~scanf("%s",s)){
n=strlen(s);
int l=0,r=0;
for(int i=0;i<n;i++){
if(s[i]=='(')l++;
else r++;
}
printf(l==r?"YES\n":"NO\n");
}
return 0;
}
C:给你一个666
我写麻烦了好像,直接对8取模即可,但坑是真坑,还要注意l大于n和r小于1的情况
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int N = 50010;
struct node{
int l ,r;
int maxx, minn;
}tree[N << 2];
int n, q;
int num1[8], num2[8];
void build(int l, int r, int cur) {
tree[cur].l = l;
tree[cur].r = r;
if(l == r) {
scanf("%d", &tree[cur].maxx);
tree[cur].minn = tree[cur].maxx;
return;
}
int mid = (r + l) >> 1;
build(l, mid, cur << 1);
build(mid + 1, r, cur << 1 | 1);
tree[cur].maxx = max(tree[cur << 1].maxx, tree[cur << 1 | 1].maxx);
tree[cur].minn = min(tree[cur << 1].minn, tree[cur << 1 | 1].minn);
}
ll ans1, ans2;
void query(int pl ,int pr, int cur) {
if(pl <= tree[cur].l && tree[cur].r <= pr) {
// cout<<tree[cur].maxx<<" "<<tree[cur].minn<<endl;
ans1 = max(ans1, 1LL * tree[cur].maxx);
ans2 = min(ans2, 1LL * tree[cur].minn);
return;
}
if(pl <= tree[cur << 1].r) query(pl, pr, cur << 1);
if(pr >= tree[cur << 1 |1].l) query(pl, pr , cur << 1 |1);
}
int main() {
scanf("%d %d", &n, &q);
build(1, n, 1);
int x, y, len, xx, yy;
int c, d;
int t1, t2;
int l, r;
while(q--) {
scanf("%d %d", &x, &y);
xx=x;yy=y;
memset(num1, 0, sizeof(num1));
memset(num2, 0, sizeof(num2));
len = 1;
while(x) {
num1[len] = x % 2;
x >>= 1;
len++;
}
len = 1;
while(y) {
num2[len] = y % 2;
y >>= 1;
len++;
}
t1 = num1[6] * 4 + num1[5] * 2 + num1[4] * 1;
t2 = num2[3] * 4 + num2[2] * 2 + num2[1] * 1;
c = t1 * t2;
t1 = num2[6] * 4 + num2[5] * 2 + num2[4] * 1;
t2 = num1[3] * 4 + num1[2] * 2 + num1[1] * 1;
d = t1 * t2;
l = max(1, min(c, d) * min(xx, yy));
if(l>n) l=1;
r = min(n, max(c, d) * max(xx, yy));
if(r<1) r=n;
ans1 = -1e10;
ans2 = 1e10;
query(l, r, 1);
printf("%lld %lld\n", ans1, ans2);
}
return 0;
}
D:LiMn2O4的数学之路
推了一下发现还是不好算,觉得应该是个公式,打表果然,斐波那契数列
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
ll n;
struct node {
ll mat[3][3];
};
node cul ( node x, node y) {
node z;
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
z.mat[i][j] = 0;
for(int k = 0; k < 2; k++)
z.mat[i][j] = (z.mat[i][j] + x.mat[i][k] * y.mat[k][j] % mod) % mod;
}
}
return z;
}
void solve() {
n -= 2;
node ans, a;
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
ans.mat[i][j] = 0;
if(i == j) ans.mat[i][j] = 1;
}
}
a.mat[0][0] = a.mat[0][1] = a.mat[1][0] = 1;
a.mat[1][1] = 0;
while(n) {
if(n & 1) ans = cul(ans, a);
a = cul(a, a);
n >>= 1;
}
printf("%lld\n", ((ans.mat[0][0] + ans.mat[0][1]) % mod + mod) % mod);
}
int main() {
int T;
scanf("%d", &T);
while(T--) {
scanf("%lld", &n);
if(n == 1 || n == 2) printf("1\n");
else if(n == 0) printf("0\n");
else solve();
}
return 0;
}
G:似魔鬼的步伐
显然第一种优,但奇数的情况第二种要取一次
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=110;
const ll mod=1e9+7;
int n;
int main() {
int T;
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
ll ans=0;
if(n&1) ans=1;
while(n>1) {
ans = (ans * 2 + 2) %mod;
n = n - 2;
}
printf("%lld\n",ans);
}
return 0;
}
H:挑剔程度
排序完事
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=110;
const ll mod=1e9+7;
int n,k;
int a[20010];
int main() {
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
sort(a+1,a+1+n);
printf("%d\n",a[max(0,n-k)]);
return 0;
}
I:热狗树
树形dp,dp[i][0/1]分别记录下,分支中两种颜色的个数,然后计算每条边的贡献即可
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+10;
const ll mod = 998244353;
struct node{
int to,val;
node(int to_,int val_)
{
to=to_;
val=val_;
}
};
vector<node> v[N];
int n,vis[N];
ll dp[N][2];
ll ans,num1,num0;
void dfs(int u,int fa)
{
int len=v[u].size();
dp[u][vis[u]]=1;
for(int i=0;i<len;i++)
{
int to=v[u][i].to;
if(to==fa) continue;
dfs(to,u);
ans=(ans+dp[to][1]*(num0-dp[to][0])%mod*v[u][i].val%mod)%mod;
ans=(ans+dp[to][0]*(num1-dp[to][1])%mod*v[u][i].val%mod)%mod;
// cout<<to<<" "<<ans<<endl;
dp[u][1]+=dp[to][1];
dp[u][0]+=dp[to][0];
}
}
int main() {
scanf("%d",&n);
int x,y,z;
for(int i=1;i<=n;i++)scanf("%d",&vis[i]),num1+=vis[i];
num0=n-num1;
for(int i=1;i<n;i++){
scanf("%d%d%d",&x,&y,&z);
v[x].push_back(node(y,z));
v[y].push_back(node(x,z));
}
dfs(1,0);
printf("%lld\n",ans*2%mod);
return 0;
}
J:流浪西邮之寻找火石碎片
背包
#include<bits/stdc++.h>
using namespace std;
const int N=110;
int dp[N][N][7];
struct node{
int a,b,v;
}c[N];
int n,v1,v2,k;
int main() {
while(~scanf("%d%d%d%d",&n,&v1,&v2,&k)) {
for(int i=1;i<=n;i++)scanf("%d%d%d",&c[i].a,&c[i].b,&c[i].v);
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
for(int j=v1;j>=0;j--)
{
for(int kk=v2;kk>=0;kk--)
{
for(int l=k;l>=0;l--){
if(l>=1) dp[j][kk][l]=max(dp[j][kk][l],dp[j][kk][l-1]+c[i].v);
if(j>=c[i].a) dp[j][kk][l]=max(dp[j][kk][l],dp[j-c[i].a][kk][l]+c[i].v);
if(kk>=c[i].b) dp[j][kk][l]=max(dp[j][kk][l],dp[j][kk-c[i].b][l]+c[i].v);
}
}
}
}
printf("%d\n",dp[v1][v2][k]);
}
return 0;
}
K:到底有多少个小和尚?
求个前缀
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=110;
const ll mod=1e9+7;
ll n,m;
ll dp[1000100];
int main() {
dp[1]=2;
for(int i=2;i<=1000000;i++)
dp[i]=dp[i-1]+1LL*(i+1)*i;
int T;
// cout<<dp[1000000]<<endl;
scanf("%d",&T);
while(T--) {
scanf("%d%d",&n,&m);
printf("%lld\n",dp[m]-dp[n-1]);
}
return 0;
}