#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
using namespace std;
#define PI 3.14159265358
int main()
{
double d, h, x,n1, n2;
while (scanf("%lf%lf%lf%lf%lf",&d,&h,&x,&n1,&n2))
{
if (d==0&&h==0&&x==0&&n1==0&&n2==0)
break;
double ans = 90;
double s = 0.0;
double j;
if (x == 0)
{
printf("90.00\n");
continue;
}
for(j=0.001;j<90;j+=0.001)
{
double xxx = j * PI / 180.0;
double y = d / tan(xxx);
if (x < y)
continue;
double yy = y * y;
double xx = yy * (h * h + ((x - y) * (x - y)));
double yyy = (yy + d * d) * (x - y) * (x - y);
double fff = (n1 * n1) / (n2 * n2) - xx/yyy;
if (fabs(fff-0.0) < ans)
{
ans = fabs(fff-0.0);
s = j;
//printf("%lf\n",s);
}
}
printf("%.2lf\n",s);
}
}
1715: Positive Con Sequences
#include"iostream"
#include"cmath"
using namespace std;
#define eps 1e-6
int main(){
int i,j,a[5];
while(cin>>a[1]>>a[2]>>a[3]>>a[4]){
int sol;
if(a[1]==-1 && a[2]==-1 && a[3]==-1 && a[4]==-1){
break;
}
if(a[1]==0 || a[2]==0 || a[3]==0 || a[4]==0){
cout<<-1<<endl;
continue;
}
for(i=1; i<=4; i++){
if(a[i]==-1){
sol = i;//位置
}
}
if(sol==1){ //1 未知
a[0] = a[3]-a[2];
a[1] = a[2]-a[0];
if(a[1]+a[0] ==a[2] && a[1]+2*a[0]==a[3] && a[1]+3*a[0]==a[4]
&& a[1]<=10000 && a[1]>0){//差
cout<<a[1]<<endl;
continue;
}
a[0] = a[3]/a[2];
a[1] = a[2]/a[0];
if(a[1]*a[0] ==a[2] && a[1]*a[0]*a[0]==a[3] && a[1]*a[0]*a[0]*a[0]==a[4]
&& a[1]<=10000 && a[1]>0){ //比
cout<<a[1]<<endl;
continue;
}
cout<<-1<<endl;
}
if(sol==4){// 4未知
a[0] = a[3]-a[2];
a[4] = a[3]+a[0];
if(a[4]-a[0]==a[3] && a[4]-2*a[0]==a[2] && a[4]-3*a[0]==a[1]
&& a[4]<=10000 && a[4]>0 ){ //差
cout<<a[4]<<endl;
continue;
}
a[0] = a[3]/a[2];
a[4] = a[3]*a[0];
if(a[4]/a[0] ==a[3] && a[4]/a[0]/a[0]==a[2] && a[4]/a[0]/a[0]/a[0]==a[1]
&& a[4]<=10000 && a[4]>0 ){//比
cout<<a[4]<<endl;
continue;
}
cout<<-1<<endl;
}
if(sol==2){// 2 未知
a[0] = a[4]-a[3];
a[2] = a[1]+a[0];
if(a[2]+a[0] ==a[3] && a[2]+2*a[0]==a[4] && a[2]-a[0]==a[1]
&& a[2]<=10000 && a[2]>0){//差
cout<<a[2]<<endl;
continue;
}
a[0] = a[4]/a[3];
a[2] = a[1]*a[0];
if(a[2]*a[0] == a[3] && a[2]*a[0]*a[0]==a[4] && a[2]/a[0]==a[1]
&& a[2]<=10000 && a[2]>0 ){//比
cout<<a[2]<<endl;
continue;
}
cout<<-1<<endl;
}
if(sol==3){// 3 未知
a[0] = a[2]-a[1];
a[3] = a[2]+a[0];
if(a[3]-a[0]==a[2] && a[3]-2*a[0]==a[1] && a[3]+a[0]==a[4]
&& a[3]<=10000 && a[3]>0 ){//差
cout<<a[3]<<endl;
continue;
}
a[0] = a[2]/a[1];
a[3] = a[2]*a[0];
if(a[3]*a[0] ==a[4] && a[3]/a[0]==a[2] && a[3]/a[0]/a[0]==a[1]
&& a[3]<=10000 && a[3]>0 ){//比
cout<<a[3]<<endl;
continue;
}
cout<<-1<<endl;
}
}
return 0;
}
1720: How to Get 2^n
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
#define MAX 100005
//#define MAXN 500005
#define maxnode 105
#define sigma_size 2
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lrt rt<<1
#define rrt rt<<1|1
#define middle int m=(r+l)>>1
#define LL long long
#define ull unsigned long long
#define mem(x,v) memset(x,v,sizeof(x))
#define lowbit(x) (x&-x)
#define pii pair<int,int>
#define bits(a) __builtin_popcount(a)
#define mk make_pair
#define limit 10000
//const int prime = 999983;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-9;
const LL mod = 1e9+7;
const ull mx = 1e9+7;
/*****************************************************/
inline void RI(int &x) {
char c;
while((c=getchar())<'0' || c>'9');
x=c-'0';
while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
}
/*****************************************************/
const int MX = 10;//一共可以表示长度MX*DLEN的
const int maxn = 9999;
const int DLEN = 4;//一个int里面放多少个数字
char ret[105];
class Big {
public:
int a[MX], len;
Big(const int b = 0) {
int c, d = b;
len = 0;
memset(a, 0, sizeof(a));
while(d > maxn) {
c = d - (d / (maxn + 1)) * (maxn + 1);
d = d / (maxn + 1);
a[len++] = c;
}
a[len++] = d;
}
Big(const char *s) {
int t, k, index, L, i;
memset(a, 0, sizeof(a));
L = strlen(s);
len = L / DLEN;
if(L % DLEN) len++;
index = 0;
for(i = L - 1; i >= 0; i -= DLEN) {
t = 0;
k = i - DLEN + 1;
if(k < 0) k = 0;
for(int j = k; j <= i; j++) {
t = t * 10 + s[j] - '0';
}
a[index++] = t;
}
}
Big operator/(const int &b)const {
Big ret;
int i, down = 0;
for(int i = len - 1; i >= 0; i--) {
ret.a[i] = (a[i] + down * (maxn + 1)) / b;
down = a[i] + down * (maxn + 1) - ret.a[i] * b;
}
ret.len = len;
while(ret.a[ret.len - 1] == 0 && ret.len > 1) ret.len--;
return ret;
}
bool operator>(const Big &T)const {
int ln;
if(len > T.len) return true;
else if(len == T.len) {
ln = len - 1;
while(a[ln] == T.a[ln] && ln >= 0) ln--;
if(ln >= 0 && a[ln] > T.a[ln]) return true;
else return false;
} else return false;
}
Big operator+(const Big &T)const {
Big t(*this);
int i, big;
big = T.len > len ? T.len : len;
for(i = 0; i < big; i++) {
t.a[i] += T.a[i];
if(t.a[i] > maxn) {
t.a[i + 1]++;
t.a[i] -= maxn + 1;
}
}
if(t.a[big] != 0) t.len = big + 1;
else t.len = big;
return t;
}
Big operator-(const Big &T)const {
int i, j, big;
bool flag;
Big t1, t2;
if(*this > T) {
t1 = *this;
t2 = T;
flag = 0;
} else {
t1 = T;
t2 = *this;
flag = 1;
}
big = t1.len;
for(i = 0; i < big; i++) {
if(t1.a[i] < t2.a[i]) {
j = i + 1;
while(t1.a[j] == 0) j++;
t1.a[j--]--;
while(j > i) t1.a[j--] += maxn;
t1.a[i] += maxn + 1 - t2.a[i];
} else t1.a[i] -= t2.a[i];
}
t1.len = big;
while(t1.a[t1.len - 1] == 0 && t1.len > 1) {
t1.len--;
big--;
}
if(flag) t1.a[big - 1] = 0 - t1.a[big - 1];
return t1;
}
int operator%(const int &b)const {
int i, d = 0;
for(int i = len - 1; i >= 0; i--) {
d = ((d * (maxn + 1)) % b + a[i]) % b;
}
return d;
}
Big operator*(const Big &T) const {
Big ret;
int i, j, up, temp, temp1;
for(i = 0; i < len; i++) {
up = 0;
for(j = 0; j < T.len; j++) {
temp = a[i] * T.a[j] + ret.a[i + j] + up;
if(temp > maxn) {
temp1 = temp - temp / (maxn + 1) * (maxn + 1);
up = temp / (maxn + 1);
ret.a[i + j] = temp1;
} else {
up = 0;
ret.a[i + j] = temp;
}
}
if(up != 0) {
ret.a[i + j] = up;
}
}
ret.len = i + j;
while(ret.a[ret.len - 1] == 0 && ret.len > 1) ret.len--;
return ret;
}
ull print() {
ull ret=0;
for(int i=len-1;i>=0;i--) ret=ret*mx+a[i];
return ret;
}
}pow2[105],ss;
map<ull,int> ma;
char s[105];
void init(){
pow2[0]=1;
for(int i=1;i<=103;i++) pow2[i]=pow2[i-1]*2;
}
int main(){
//freopen("test.txt","r",stdin);
int t;
cin>>t;
init();
while(t--){
int n;
cin>>n;
ma.clear();
LL ans=0;
for(int i=0;i<n;i++){
scanf("%s",s);
int len;
ss=s;
for(int j=103;j>=1;j--){
if(pow2[j]>ss){
Big cnt=pow2[j]-ss;
ull temp=cnt.print();
if(ma.count(temp)) ans+=ma[temp];
}
}
ull tmp=ss.print();
if(!ma.count(tmp)) ma[tmp]=0;
ma[tmp]++;
//cout<<ma[ss[i]]<<" "<<ss[i]<<endl;
}
cout<<ans<<endl;
}
return 0;
}
1721: 皇家火枪手
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
#define N int(2e3+10)
#define inf int(0x3f3f3f3f)
#define mod int(1e9+7)
typedef long long LL;
int main()
{
#ifdef OFFLINE
freopen("t.txt", "r", stdin);
#endif
LL t, n, m, a, k, num;
scanf("%lld", &t);
while (t--){
LL ans = 0, sum = 0;
scanf("%lld%lld%lld", &m, &a, &k);
if (k == 1){
ans += m / a;
if (m%a) ans++;
}
else{
num = k / 2;
sum = 2 * a * (num*(num + 1) / 2);//需要num人
if (sum >= m){
while (sum >= m){
sum -= 2 * a * num;
num--;
}
num++;//通过while找到确切的num
ans = num;
}
else{
if (k % 2 == 0){
ans = num;
m -= sum;
if (m > 0){//切记防止让负数参与
//还需加入的人数,这时加入的每个人贡献(2 * a*num)点攻击力
ans += m / (2 * a*num);
if (m % (2 * a*num)) ans++;
}
}
else{
ans = num;
sum += 2 * a*num + a;//加上留到最后一秒的人的攻击力
m -= sum;
ans++;
if (m > 0){
ans += m / (2 * a*num + a);
if (m % (2 * a*num + a)) ans++;
}
}
}
}
printf("%lld\n", ans);
}
return 0;
}
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define EPS 1e-6
#define INF INT_MAX / 10
#define LL long long
#define MOD 100000000
#define PI acos(-1.0)
using namespace std;
struct node
{
int x,y;
int step;
};
bool operator < (node p,node q)
{
return p.step > q.step;
}
const int maxn = 111;
int n,m;
char maze[maxn][maxn];
int dir[4][2] = {{0,1},{-1,0},{0,-1},{1,0}};
int sx,sy;
int bfs()
{
priority_queue<node> que;
node p;
p.x = sx,p.y = sy,p.step = 0;
que.push(p);
maze[sx][sy] = '#';
while(que.size()){
node q = que.top();
que.pop();
if(q.x == n - 1 || q.y == m - 1 || q.x == 0 || q.y == 0){
return q.step;
}
for(int i = 0;i < 4;i++){
node r;
r.x = q.x + dir[i][0];
r.y = q.y + dir[i][1];
if(0 <= r.x && r.x < n && 0 <= r.y && r.y < m && maze[r.x][r.y] != '#'){
if(maze[r.x][r.y] == '*')
r.step = q.step + 1;
else
r.step = q.step;
que.push(r);
maze[r.x][r.y] = '#';
}
}
}
return -1;
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&m);
for(int i = 0;i < n;i++)
scanf("%s",maze[i]);
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
if(maze[i][j] == '@'){
sx = i;
sy = j;
break;
}
}
}
printf("%d\n",bfs());
memset(maze,0,sizeof(maze));
}
return 0;
}
1728: 线形逐步共聚合反应
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
#define MAX 200005
#define MAXN 500005
#define maxnode 105
#define sigma_size 2
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lrt rt<<1
#define rrt rt<<1|1
#define middle int m=(r+l)>>1
#define LL long long
#define ull unsigned long long
#define mem(x,v) memset(x,v,sizeof(x))
#define lowbit(x) (x&-x)
#define pii pair<int,int>
#define bits(a) __builtin_popcount(a)
#define mk make_pair
#define limit 10000
//const int prime = 999983;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-9;
const LL mod = 1e9+7;
const ull mx = 1e9+7;
/*****************************************************/
inline void RI(int &x) {
char c;
while((c=getchar())<'0' || c>'9');
x=c-'0';
while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
}
/*****************************************************/
int a[MAX];
double dp[MAX],dp1[MAX];
int n;
double get(double x){
dp[0]=dp1[0]=0;
double ret=0;
for(int i=1;i<=n;i++){
dp[i]=max(dp[i-1]+(a[i]-x),(a[i]-x));
dp1[i]=max(dp1[i-1]+(x-a[i]),(x-a[i]));
ret=max(ret,dp[i]);
ret=max(ret,dp1[i]);
}
return ret;
}
int main(){
//freopen("test.txt","r",stdin);
int t;
cin>>t;
while(t--){
cin>>n;
int mini=INF,maxn=0;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
mini=min(mini,a[i]);
maxn=max(maxn,a[i]);
}
double l=mini,r=maxn;
for(int i=0;i<100;i++){
double ll=(2*l+r)/3;
double rr=(2*r+l)/3;
if(get(ll)<get(rr)) r=rr;
else l=ll;
}
printf("%.6f\n",l);
}
return 0;
}
1731: XueXX and P-P String
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <bitset>
#include <vector>
#include <cstdio>
#include <string>
#include <cassert>
#include <climits>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
typedef unsigned long long ull;
#define fi first
#define se second
#define prN printf("\n")
#define SI(N) scanf("%d",&(N))
#define SII(N,M) scanf("%d%d",&(N),&(M))
#define SIII(N,M,K) scanf("%d%d%d",&(N),&(M),&(K))
#define cle(a,val) memset(a,(val),sizeof(a))
#define rep(i,b) for(int i=0;i<(b);i++)
#define Rep(i,a,b) for(int i=(a);i<=(b);i++)
string a;
int save[205];
int judge(int in1,int in2)
{
int len=in2-in1+1;
int fl=0;
if (len&1)
{
int mid=(in1+in2)/2;
int s=mid-1,e=mid+1;
while(a[s]==a[e]&&s>=in1&&e<=in2)
{
s--,e++;
}
if (s==in1-1&&e==in2+1)
return 1;
else return 0;
}
else
{
int mid=(in1+in2)/2;
int s=mid,e=mid+1;
while(a[s]==a[e]&&s>=in1&&e<=in2)
{
s--,e++;
}
if (s==in1-1&&e==in2+1)
return 1;
else return 0;
}
}
int main()
{
int o;
cin>>o;
while (o--)
{
cle(save,0);
cin>>a;
int len=a.size();
int ans=0;
for (int i=1; i<=len-2; i++)
{
for (int j=1; j<=len-3; j++)
{
if (i-j<0||i+2*j-1>=len)
break;
else
{
int a=i-j,b=i+j-1,c=i+2*j-1;
int f1=judge(a,b);
int f2=judge(i,c);
if (f1&&f2)
{
ans=max(3*j,ans);
}
}
}
}
printf("%d\n",ans);
}
return 0;
}
1733: XueXX and Chessboard
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <bitset>
#include <vector>
#include <cstdio>
#include <string>
#include <cassert>
#include <climits>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int INF=-1;
typedef long long ll;
typedef unsigned long long ull;
#define fi first
#define se second
#define prN printf("\n")
#define SI(N) scanf("%d",&(N))
#define SII(N,M) scanf("%d%d",&(N),&(M))
#define SIII(N,M,K) scanf("%d%d%d",&(N),&(M),&(K))
#define cle(a,val) memset(a,(val),sizeof(a))
#define rep(i,b) for(int i=0;i<(b);i++)
#define Rep(i,a,b) for(int i=(a);i<=(b);i++)
int n,m,k;
int dp[1005][1005];
const int mod=1000000007;
int main()
{
int o;
cin>>o;
while(o--)
{
cle(dp,0);
cin>>n>>m>>k;
int x,y;
rep(i,k)
{
cin>>x>>y;
dp[x][y]=INF;
}
dp[0][1]=1;
Rep(i,1,n)
{
Rep(j,1,m)
{
if(dp[i][j]==INF)continue;
if (dp[i-1][j]>=0&&dp[i][j-1]>=0)
{
dp[i][j]+=(dp[i-1][j]+dp[i][j-1]);
dp[i][j]%=mod;
}
if (dp[i-1][j]==INF&&dp[i][j-1]==INF)
{
dp[i][j]+=0;
}
if (dp[i-1][j]>=0&&dp[i][j-1]==INF)
{
dp[i][j]+=dp[i-1][j];
dp[i][j]%=mod;
}
if (dp[i-1][j]==INF&&dp[i][j-1]>=0)
{
dp[i][j]+=dp[i][j-1];
dp[i][j]%=mod;
}
}
}
if (dp[n][m]==INF)
puts("0");
else
printf("%d\n",dp[n][m]);
}
return 0;
}
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <string.h>
using namespace std;
const int N = 105;
int a[N],b[N];
int main(){
int n,k;
while(scanf("%d%d",&n,&k)!=EOF){
int sum = 0;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
sum+=a[i];
}
int cnt = 0;
sort(a+1,a+1+n);
while(sum<n*k){
cnt++;
for(int i=1;i<=n;i++) b[i] = a[i];
if(a[1]<k){
b[1]++;
sum++;
}
for(int i=2;i<=n;i++){
if(a[i]==a[i-1]||a[i]==k) continue;
b[i]++;
sum++;
}
for(int i=1;i<=n;i++){
a[i] = b[i];
}
sort(a+1,a+1+n);
}
printf("%d\n",cnt);
}
}
1765: 简单shell
#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<set>
using namespace std;
int n;
vector<vector<int> > vec;
vector<int> lujing;
char tc[15];
string ti;
int main()
{
int T,a,b;
cin>>T;
while (T--)
{
cin>>n;
lujing.clear();
lujing.push_back(1);
vec.clear();
vec.resize(n+3);
for (int i=1;i<n;i++)
{
cin>>a>>b;
vec[a].push_back(b);
}
while(cin>>tc&&tc[0]!='e')
{
if (tc[0]=='l')
{
if (vec[lujing[lujing.size()-1]].empty())
continue;
else
{
for (int i=0;i<(int)vec[lujing[lujing.size()-1]].size()-1;i++)
cout<<vec[lujing[lujing.size()-1]][i]<<' ';
cout<<vec[lujing[lujing.size()-1]][vec[lujing[lujing.size()-1]].size()-1]<<endl;
}
}
else if (tc[0]=='p')
{
if (lujing.empty())
continue;
else
{
for (int i=0;i<(int)lujing.size();i++)
cout<<'/'<<lujing[i];
cout<<endl;
}
}
else if (tc[0]=='c')
{
cin>>ti;
if (ti[0]=='.')
{
lujing.pop_back();
}
else
{
stringstream ss;
ss<<ti;
int temp;
ss>>temp;
lujing.push_back(temp);
}
}
}
}
return 0;
}
1772: 漫漫上学路
#include <cstdio>
#include <cstring>
typedef long long ll;
const int maxn = 31;
ll d[maxn];
void catalan()
{
int i,j;
d[0] = 1;
for(i = 1;i < maxn;i++){
d[i] = 0;
for(j = 0;j < i;j++){
d[i] += d[j] * d[i - j - 1];
}
}
}
int main()
{
int n,i, j;
catalan();
while(scanf("%d",&n) != EOF && 1 <= n && n <= 30){
printf("%lld\n",d[n]);
}
return 0;
}
1774: 慷慨的奖励
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1e5 + 5;
int N, D;
char str[maxn];
struct Node {
int prior, rear;
} nodes[maxn];
void del(int x) {
int a = nodes[x].prior;
int b = nodes[x].rear;
if(a == -1) {
nodes[b].prior = -1;
} else {
nodes[a].rear = b;
}
if(b == -1) {
nodes[a].rear = -1;
} else {
nodes[b].prior = a;
}
}
int main() {
while(~scanf("%d %d", &N, &D) && N) {
scanf("%s", str);
nodes[0].prior = -1;
nodes[0].rear = 1;
nodes[N - 1].prior = N - 2;
nodes[N - 1].rear = -1;
for(int i = 1; i < N - 1; i++) {
nodes[i].prior = i - 1;
nodes[i].rear = i + 1;
}
int pos = 0, phead = 0, cnt = D;
while(cnt > 0) {
int np = nodes[pos].rear;
if(np != -1 && str[pos] < str[np]) {
del(pos);
if(pos == phead) phead = np;
pos = np;
cnt --;
} else if(np != -1 && str[pos] >= str[np]) {
pos ++;
continue;
} else if(np == -1) {
pos = nodes[pos].prior;
del(nodes[pos].rear);
cnt --;
}
int pp = nodes[pos].prior;
while(cnt > 0 && pp != -1 && str[pos] > str[pp]) {
del(pp);
pp = nodes[pos].prior;
cnt --;
}
if(pp == -1) phead = pos;
}
for(int i = 0, pos = phead; i < N - D; i ++) {
printf("%c", str[pos]);
pos = nodes[pos].rear;
}
puts("");
}
return 0;
}
#include<string>
#include<math.h>
#include<stdio.h>
#include<iostream>
#include<queue>
#include<algorithm>
#include<vector>
#include<string.h>
#include <stdlib.h>
#include<iterator>
using namespace std;
typedef unsigned long long ll;
const int maxn = 505;
const int inf = 100000;
int vis1[maxn][maxn];
int vis2[maxn][maxn][4];
int a[maxn][maxn];
int n,m,r1,r2,c1,c2;
struct node{
int x,y,dir;
int step;
};
bool operator<(node a , node b){
return a.step > b.step;
}
int to[4][2] = {1,0,-1,0,0,1,0,-1};
int bfs1(){
if(c1 == c2&& r1 == r2)
return 0;
priority_queue<struct node> que;
memset(vis1,0,sizeof(vis1));
node no,nxt,now;
no.x = r1 ; no.y = c1 ; no.step = a[r1][c1];
que.push(no);
vis1[r1][c1] = 1;
while(!que.empty()){
now = que.top();
que.pop();
if(now.x == r2&&now.y == c2)
return now.step;
for(int i = 0 ; i < 4; i ++){
nxt.x = now.x + to[i][0];
nxt.y = now.y + to[i][1];
if(nxt.x>0&&nxt.x<=n&&nxt.y>0&&nxt.y<=m&&a[nxt.x][nxt.y]!=0&&!vis1[nxt.x][nxt.y]){
vis1[nxt.x][nxt.y] = 1;
nxt.step = now.step + a[nxt.x][nxt.y];
que.push(nxt);
}
}
}
return -1;
}
int bfs2(){
if(c1 == c2&& r1 == r2)
return 0;
priority_queue<struct node> que;
memset(vis2,0,sizeof(vis2));
node no,nxt,now;
no.x = r1 ; no.y = c1 ; no.step = a[r1][c1];
no.dir = -1;
que.push(no);
vis2[r1][c1][0] = vis2[r1][c1][1] = vis2[r1][c1][2] =vis2[r1][c1][3] = 1;
while(!que.empty()){
now = que.top();
que.pop();
if(now.x == r2&&now.y == c2)
return now.step;
for(int i = 0 ; i < 4; i ++){
nxt.x = now.x + to[i][0];
nxt.y = now.y + to[i][1];
if(now.dir==i)
continue;
if(nxt.x>0&&nxt.x<=n&&nxt.y>0&&nxt.y<=m&&a[nxt.x][nxt.y]!=0&&!vis2[nxt.x][nxt.y][i]){
vis2[nxt.x][nxt.y][i] = 1;
nxt.step = now.step + a[nxt.x][nxt.y];
nxt.dir = i;
que.push(nxt);
}
}
}
return -1;
}
int main()
{
int i,j;
char ch[5];
int k = 1;
while(~scanf("%d%d%d%d%d%d",&n,&m,&r1,&c1,&r2,&c2)){
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
cin>>ch;
if(ch[0] == '*')
a[i][j] = 0;
else
a[i][j] = atoi(ch);
}
}
cout<<"Case "<<k++<<": ";
int res1 = bfs1();
int res2 = bfs2();
printf("%d %d\n",res1,res2);
}
return 0;
}
1784: Internet of Lights and Switches
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
typedef long long LL;
char str[80];
const int INF = 999999999;
map<LL,vector<int> > mp;
int n,m,A,B;
LL charToLL(char *str){
LL ans = 0;
int p = 1,len = strlen(str);
for(int i=len-1;i>=0;i--){
if(str[i]=='1'){
ans+=p;
}
p*=2;
}
return ans;
}
int binary(LL val,int id){
vector <int> vec = mp[val];
int down = 0,up = vec.size()-1,r=-1,l=INF;
while(down<=up){
int mid = (down+up)>>1;
if(id-vec[mid]>=A){
r = mid;
down = mid+1;
}else up = mid-1;
}
down = 0,up = vec.size()-1;
while(down<=up){
int mid = (down+up)>>1;
if(id-vec[mid]<=B){
l = mid;
up = mid-1;
}else down = mid+1;
}
//printf("%d %d\n",l,r);
if(l>r) return 0;
return r-l+1;
}
int main(){
int t = 1;
while(scanf("%d%d%d%d",&m,&n,&A,&B)!=EOF){
mp.clear();
for(int i=0;i<m;i++){
str[i] = '1';
}
str[m]='\0';
LL k = charToLL(str),xorsum=0,ans = 0;
for(int i=1;i<=n;i++){
scanf("%s",str);
LL x = charToLL(str);
xorsum^=x;
if(xorsum==k&&i>=A&&i<=B) ans++;
ans+=binary(k^xorsum,i);
mp[xorsum].push_back(i);
}
printf("Case %d: %lld\n",t++,ans);
}
}
1797: Train passengers
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <algorithm>
using namespace std;
#define ls 2*i
#define rs 2*i+1
#define up(i,x,y) for(i=x;i<=y;i++)
#define down(i,x,y) for(i=x;i>=y;i--)
#define mem(a,x) memset(a,x,sizeof(a))
#define w(a) while(a)
#define LL long long
const double pi = acos(-1.0);
#define Len 200005
#define mod 360000
const int INF = 0x3f3f3f3f;
#define exp 1e-6
int main()
{
LL C,n,sum,w,a,b,c;
w(scanf("%lld%lld",&C,&n)>0)
{
sum=0;
w=0;
int flag=0;
w(n--)
{
scanf("%lld%lld%lld",&a,&b,&c);
if(flag)
continue;
if(sum<a)
{
flag=1;
continue;
}
sum=sum-a+b;
if(sum>C)
flag=1;
if(sum!=C && c) flag = 1;
if(!n && (sum || b || c)) flag = 1;
}
if(flag)
printf("impossible\n");
else
printf("possible\n");
}
}
#include <stdio.h>
#include <string.h>
#include <iostream>
#define MAXN 1000005
using namespace std;
typedef long long LL;
bool isprime[MAXN];
int prime[MAXN];
int indexnum[MAXN];
int num;
const int TOP=80000;
const LL MOD=1000000000LL+7;
int get_prime()
{
memset(isprime,true,sizeof(isprime));
isprime[1]=false;
for(int i=2;i*i<MAXN-4;i++)
if(isprime[i])
for(int j=2;i*j<MAXN-4;j++)
isprime[i*j]=false;
num=0;
for(int i=1;i<MAXN-4;i++)
if(isprime[i])
indexnum[i]=num,prime[num++]=i;
return num;
}
int a[TOP];
void deal(int x)
{
for(int i=0;i<num;i++){
if(isprime[x]){
a[indexnum[x]]++;
break;
}
while(x%prime[i]==0){
a[i]++;
x/=prime[i];
}
if(x==1)
break;
}
}
LL mul(LL x,LL n,LL mod)
{
LL tmp=x;
LL ans=1;
while(n){
if(n&1){
ans*=tmp;
ans%=mod;
}
tmp*=tmp;
tmp%=mod;
n>>=1;
}
return ans%mod;
}
int main()
{
//freopen("in.data","r",stdin);
get_prime();
int T,K=1;
for(cin>>T;T--;){
int n;
scanf("%d",&n);
memset(a,0,sizeof(a));
for(int i=0;i<n;i++){
int tmp;
scanf("%d",&tmp);
deal(tmp);
}
LL ans=1;
for(int i=0;i<num;i++)
if(a[i])
ans=ans*mul(2,a[i]-1,MOD)%MOD;
ans=(ans-1+MOD)%MOD;
printf("Case #%d:\n%lld\n",K++,ans);
}
return 0;
}
1802: 小X的战斗力
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#define inf 0x3f3f3f3f
using namespace std;
int n,m;
int map[200][200];
int main()
{
int test;
scanf("%d",&test);
while(test--)
{
memset(map,0,sizeof(map));
int a,b;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);//a赢b
map[b][a]=1;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
for(int k=1;k<=n;k++)
{
if(map[j][i]==1&&map[i][k]==1)
{
map[j][k]=1;
}
}
}
}
int flag=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(map[j][i]==1&&map[i][j]==1)
{
flag=1;
break;
}
}
if(flag==1)break;
}
if(flag==1)
{
printf("Wrong\n");
continue;
}
int num=0;
int is_sure1=1;
for(int i=1;i<=n;i++)//判断i号点是不是确定的
{
int is_sure=1;
for(int j=1;j<=n;j++)
{
if(i==j)continue;
if(map[i][j]==0&&map[j][i]==0)
{
is_sure=0;
break;
}
}
if(is_sure==1)num++;
}
int rank=1;
for(int i=2;i<=n;i++)
{
if(map[1][i]==1)
{
rank++;
continue;
}
if(map[i][1]==0)
{
is_sure1=0;
break;
}
}
if(is_sure1==0) printf("-1\n");
else printf("%d\n",rank);
printf("%d\n",num);
}
}
1804: 有向无环图
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int mo=1e9+7;
const int maxn=1e5+10;
int head[maxn],e[2*maxn],nex[2*maxn];
int a[maxn],b[maxn],vis[maxn],f[maxn];
int ans,tot;
void add_edge(int u,int v){
e[tot]=v;nex[tot]=head[u];head[u]=tot++;
}
void dfs(int u){
int v,i;
f[u]=0;vis[u]=1;
if (head[u]==-1) {
f[u]=b[u];
return;
}
for (i=head[u];i!=-1;i=nex[i]){
v=e[i];
if (!vis[v]) dfs(v);
f[u]=(f[u]+f[v])%mo;
}
ans=(ans+(long long)f[u]*a[u]%mo)%mo;
f[u]=(f[u]+b[u])%mo;
}
int main()
{
int i,n,m,u,v;
while (scanf("%d %d",&n,&m)!=-1){
for (i=1;i<=n;i++) head[i]=-1;
tot=0;
for (i=1;i<=n;i++){
scanf("%d %d",&a[i],&b[i]);
}
for (i=1;i<=m;i++){
scanf("%d %d",&u,&v);
add_edge(u,v);
}
memset(vis,0,sizeof(vis));
ans=0;
for (i=1;i<=n;i++){
if (!vis[i]) dfs(i);
}
printf("%d\n",ans);
}
return 0;
}
1805: Three Capitals
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
#define MAX 100010
#define MAXN 1000005
#define maxnode 5
#define sigma_size 30
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lrt rt<<1
#define rrt rt<<1|1
#define middle int m=(r+l)>>1
#define LL long long
#define ull unsigned long long
#define mem(x,v) memset(x,v,sizeof(x))
#define lowbit(x) (x&-x)
#define pii pair<int,int>
#define bits(a) __builtin_popcount(a)
#define mk make_pair
#define limit 10000
//const int prime = 999983;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-6;
const LL mod = 1e9+7;
const ull mx = 133333331;
/*****************************************************/
inline void RI(int &x) {
char c;
while((c=getchar())<'0' || c>'9');
x=c-'0';
while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
}
/*****************************************************/
bool isFreeX[5];
LL A[5][5];
LL atob,atoc,btoa,btoc,ctob,ctoa;
LL gauss(int n, int m) {//求行列式
for(int i = 0; i < m; ++i) isFreeX[i] = false;
LL ret = 1, neg = 0;
int r = 1, c = 1;//求n-1阶的行列式,去掉第一阶,所以从1开始
for(; r < n && c < m; ++r, ++c) {
int p = r;
for(; p < n; ++p) if(A[p][c]) break;
if(p == n) {--r; isFreeX[c] = true; continue;}
if(p != r) {
neg ^= 1;
for(int i = c; i <= m; ++i) swap(A[p][i], A[r][i]);
}
//eliminate coefficient
for(int i = r + 1; i < n; ++i) {
while(A[i][c]) {
LL delta = A[i][c] / A[r][c];
for(int j = c; j <= m; ++j) {
A[i][j] += mod - delta * A[r][j] % mod;
A[i][j] %= mod;
}
if(!A[i][c]) break;
neg ^= 1;
for(int j = c; j <= m; ++j) swap(A[r][j], A[i][j]);
}
}
}
if(r != n) return 0;
//0-r-1求n阶行列式,1-r-1求n-1阶行列式
for(int i = 1; i < r; ++i) ret = ret * A[i][i] % mod;
if(neg) ret = (-ret + mod) % mod;
return ret;
}
int deg[5];
LL fact[MAX];
void init(){
fact[0]=1;
for(int i=1;i<=100000;i++) fact[i]=fact[i-1]*i%mod;
}
LL qpow(LL a,LL n){
LL ans=1;
while(n){
if(n&1) ans=ans*a%mod;
a=a*a%mod;
n>>=1;
}
return ans;
}
LL C(int n,int m){
return (fact[n]*qpow(fact[m],mod-2)%mod)*qpow(fact[n-m],mod-2)%mod;
}
int main()
{
int a,b,c;
init();
while(cin>>a>>b>>c){
LL ans=0;
LL aa=a+b,bb=a+c,cc=c+b;
if(aa%2||bb%2||cc%2){
puts("0");continue;
}
for(int x=0;x<=a;x++)
{
atob=x;btoa=a-x;
btoc=bb/2-btoa;atoc=aa/2-x;
ctob=c-btoc;ctoa=b-atoc;
if(atoc<0||atob<0||btoa<0||btoc<0||ctoa<0||ctob<0) continue;
deg[0]=aa/2;
deg[1]=bb/2;
deg[2]=cc/2;
for(int i=0;i<3;i++) A[i][i]=deg[i];
A[0][1]=atob;A[0][2]=atoc;
A[1][0]=btoa;A[1][2]=btoc;
A[2][0]=ctoa;A[2][1]=ctob;
LL ret=(C(a,x)*C(b,atoc)%mod)*C(c,btoc)%mod;
ret=ret*gauss(3,3)%mod;
for(int i=0;i<3;i++) ret=ret*fact[deg[i]-1]%mod;
ret=ret*deg[0]%mod;
ans+=ret;
if(ans>=mod) ans-=mod;
}
cout<<ans<<endl;
}
return 0;
}
1806: Toll
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
#include <vector>
#include <set>
#include <bitset>
#include <stack>
#include <map>
#include <climits>
#include <functional>
using namespace std;
#define LL long long
const int INF=0x3f3f3f3f;
const double eps=1e-8;
int n,m,t;
int s[20],nt[200],e[200],c[200],d[200],cnt;
int visit[20];
double dis[20];
double SPFA(double x)
{
for(int i=0;i<=n;i++) dis[i]=1.0*INF;
memset(visit,0,sizeof visit);
queue<int>q;
visit[1]=1;
q.push(1);
dis[1]=0;
while(!q.empty())
{
int pre=q.front();
q.pop();
visit[pre]=0;
for(int i=s[pre];~i;i=nt[i])
{
if(dis[pre]+c[i]*x+d[i]<dis[e[i]])
{
dis[e[i]]=dis[pre]+c[i]*x+d[i];
if(!visit[e[i]])
{
visit[e[i]]=1;
q.push(e[i]);
}
}
}
}
return dis[n];
}
double get(double L,double R)
{
return (R-L)*(SPFA(L)+4*SPFA((L+R)/2)+SPFA(R))/6;
}
double Ans(double L,double R)
{
double mid=(L+R)/2;
double s0=get(L,R),s1=get(L,mid),s2=get(mid,R);
if(fabs(s0-(s1+s2))<=eps) return s0;
else return Ans(L,mid)+Ans(mid,R);
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&t))
{
memset(s,-1,sizeof s);
cnt=1;
for(int i=1;i<=m;i++)
{
int u,v,cc,dd;
scanf("%d%d%d%d",&u,&v,&cc,&dd);
nt[cnt]=s[u],e[cnt]=v,c[cnt]=cc,d[cnt]=dd,s[u]=cnt++;
}
printf("%.8lf\n",Ans(0,1.0*t)/t);
}
return 0;
}
#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define loop(i,j,k) for (int i = j;i != -1; i = k[i])
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
#define ff first
#define ss second
#define mp(i,j) make_pair(i,j)
#define pb push_back
#define pii pair<int,int>
#define in(x) scanf("%d", &x);
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-8;
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
int n, a[N];
LL dp[N];
void init()
{
dp[0] = dp[1] = 0; rep(i, 2, N - 1) dp[i] = dp[i - 1] + 2 * i - 3;
}
int check(int x)
{
int now = 1;
rep(i, 1, n)
{
if (now == a[x]) ++now;
if (i == x) continue;
if (a[i] && a[i] != now) return 0;
++now;
}
return 1;
}
int check(int x, int y)
{
if (x != y - 1) return 0;
rep(i, 1, n)
{
if (i == x || i == y) continue;
if (a[i] && a[i] != i) return 0;
}
return 1;
}
LL get(int x, int y)
{
int l = n + 1, r = 0;
rep(i, 1, n) if (a[i] && a[i] != i) l = min(l, i), r = max(r, i);
rep(i, l, r) if (a[i] && a[i] == i) return 0;
int L = l, R = n - r + 1;
per(i, l, 1) if (a[i] == i) { L = l - i; break; }
rep(i, r, n) if (a[i] == i) { R = i - r; break; }
return 1LL * (L - x) * (R - y);
}
LL get()
{
LL res = 0, now = 0;
rep(i, 1, n) if (a[i]) res += dp[a[i] - now - 1], now = a[i];
return res + dp[n - now];
}
int main()
{
init();
while (scanf("%d", &n) != EOF)
{
int flag = 0, l = 0, r = 0;
rep(i, 1, n)
{
scanf("%d", &a[i]);
if (!a[i]) continue;
if (abs(a[i] - i) > 1) flag = i;
if (a[i] - i == 1) l = i;
if (i - a[i] == 1) r = i;
}
if (flag) { printf("%d\n", check(flag)); continue; }
if (l&&r) { printf("%d\n", check(l, r)); continue; }
if (l || r) { printf("%lld\n", get(!l, !r)); continue; }
printf("%lld\n", get());
}
return 0;
}
1808: 地铁
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn =255555;
const int INF = 0x3f3f3f3f;
int n,m;
struct Edge{
int to,next;
int w;
}edge[maxn*2];
int head[maxn],tot;
void init(){
memset(head,-1,sizeof(head));
tot=0;
}
void addedge(int u,int v,int w){
edge[tot].to=v;
edge[tot].next = head[u];
edge[tot].w =w;
head[u]=tot++;
}
vector<int>num[maxn];
map<int,int>mp[maxn];
int dis[maxn];
int cnt;
struct node{
int now;
int c;
node(int _now = 0,int _c=0):now(_now),c(_c){}
bool operator <(const node &r)const
{
return c>r.c;
}
};
void DJ(){
priority_queue<node> que;
while(!que.empty()) que.pop();
for(int i=1;i<cnt;++i) dis[i]=INF;
for(int i=0;i<num[1].size();++i){
int st;
st = mp[1][num[1][i]];
dis[st]=0;
que.push(node(st,0));
}
node temp;
while(!que.empty()){
temp = que.top();
que.pop();
int u = temp.now;
int cost = temp.c;
if(cost>dis[u])
continue;
for(int i=head[u];~i;i=edge[i].next){
int v = edge[i].to;
int w = edge[i].w;
if(dis[v]>cost+w){
dis[v]= cost + w;
que.push(node(v,dis[v]));
}
}
}
}
int main(){
int u,v,w,x;
while(scanf("%d%d",&n,&m)!=EOF){
init();
cnt=1;
for(int i=1;i<=n;i++){
num[i].clear();
mp[i].clear();
}
for(int i=0;i<m;++i){
scanf("%d%d%d%d",&u,&v,&x,&w);
if(!mp[u][x]){
mp[u][x]=cnt++;
num[u].push_back(x);
}
u=mp[u][x];
if(!mp[v][x]){
mp[v][x]=cnt++;
num[v].push_back(x);
}
v=mp[v][x];
addedge(u,v,w);
addedge(v,u,w);
}
for(int i=1;i<=n;i++){
sort(num[i].begin(),num[i].end());
for(int j=0;j<num[i].size()-1;++j){
u=mp[i][num[i][j]];
v=mp[i][num[i][j+1]];
w=num[i][j+1]-num[i][j]; //同一站点不同线路的拆点之间的差值
addedge(u,v,w);
addedge(v,u,w);
}
}
DJ();
int ans=INF;
for(int i=0;i<num[n].size();i++){
u=mp[n][num[n][i]];
ans=min(ans,dis[u]);
}
printf("%d\n",ans);
}
return 0;
}
1809: Parenthesis
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define showtime fprintf(stderr,"time = %.15f\n",clock() / (double)CLOCKS_PER_SEC)
#define lld %I64d
#define REP(i,k,n) for(int i=k;i<n;i++)
#define REPP(i,k,n) for(int i=k;i<=n;i++)
#define scan(d) scanf("%d",&d)
#define scanl(d) scanf("%I64d",&d)
#define scann(n,m) scanf("%d%d",&n,&m)
#define scannl(n,m) scanf("%I64d%I64d",&n,&m)
#define mst(a,k) memset(a,k,sizeof(a))
#define LL long long
#define N 1005
#define mod 1000000007
inline int read(){int s=0;char ch=getchar();for(; ch<'0'||ch>'9'; ch=getchar());for(; ch>='0'&&ch<='9'; ch=getchar())s=s*10+ch-'0';return s;}
char s[100005];
int sum[100005];
struct node
{
int l,r,val;
}seg[100005*4];
void build(int i,int l,int r)
{
seg[i].l=l;
seg[i].r=r;
if(l==r)
{
seg[i].val = sum[l];
return ;
}
int mid = (l+r)>>1;
build(i<<1,l,mid);
build(i<<1|1,mid+1,r);
seg[i].val = min(seg[i<<1].val,seg[i<<1|1].val);
}
int query(int i,int l,int r)
{
if(l<=seg[i].l && r>=seg[i].r)
{
return seg[i].val;
}
int ret = 1e9;
int mid = (seg[i].l+seg[i].r)>>1;
if(mid>=l) ret = min(ret,query(i<<1,l,r));
if(mid<r) ret = min(ret,query(i<<1|1,l,r));
return ret;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n,q;
while(~scann(n,q))
{
scanf("%s",s+1);
mst(sum,0);
REPP(i,1,n)
if(s[i]=='(') sum[i] = sum[i-1] + 1;
else sum[i] = sum[i-1] - 1;
build(1,1,n);
while(q--)
{
int l,r;
scann(l,r);
if(l>r) swap(l,r);
if(s[l] == s[r] || s[l]==')')
puts("Yes");
else if(l==1 || r==n)
puts("No");
else
{
int ret = query(1,l,r-1);
if(ret<2) puts("No");
else puts("Yes");
}
}
}
return 0;
}
1811: Tree Intersection
#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define loop(i,j,k) for (int i = j;i != -1; i = k[i])
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
#define ff first
#define ss second
#define mp(i,j) make_pair(i,j)
#define pb push_back
#define pii pair<int,LL>
#define in(x) scanf("%d", &x);
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-9;
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
int n, col[N], x, y;
int ft[N], nt[N], u[N], v[N], sz;
int Ft[N], Nt[N], U[N], V[N], Sz;
int d[N], t, pre[N], L[N], R[N];
int f[N], ans[N];
void dfs(int x, int fa)
{
d[pre[x] = ++t] = x;
L[col[x]] = min(L[col[x]], t);
R[col[x]] = max(R[col[x]], t);
loop(i, ft[x], nt)
{
if (u[i] == fa) continue;
dfs(u[i], x);
U[Sz] = pre[u[i]]; V[Sz] = v[i];
Nt[Sz] = Ft[t]; Ft[t] = Sz++;
}
}
void add(int x, int y)
{
for (int i = x; i <= n; i += low(i)) f[i] += y;
}
int get(int x)
{
int res = 0;
for (int i = x; i; i -= low(i)) res += f[i];
return res;
}
int main()
{
while (scanf("%d", &n) != EOF)
{
Sz = sz = t = 0;
rep(i, 1, n) in(col[i]);
rep(i, 1, n) Ft[i] = ft[i] = -1;
rep(i, 1, n) L[i] = n + 1, R[i] = f[i] = 0;
rep(i, 1, n - 1)
{
in(x); in(y);
u[sz] = y; v[sz] = i; nt[sz] = ft[x]; ft[x] = sz++;
u[sz] = x; v[sz] = i; nt[sz] = ft[y]; ft[y] = sz++;
}
dfs(1, 0);
rep(i, 1, n) pre[i] = f[i] = 0;
rep(i, 1, n)
{
add(pre[col[d[i]]] + 1, 1); add(i + 1, -1); pre[col[d[i]]] = i;
if (i == R[col[d[i]]]) add(1, -1), add(L[col[d[i]]] + 1, 1);
loop(j, Ft[i], Nt) ans[V[j]] = get(U[j]);
}
rep(i, 1, n - 1) printf("%d\n", ans[i]);
}
return 0;
}
1812: 三角形和矩形
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#define eps 1e-8
using namespace std;
struct point{
double x,y;
}S[5],J[5],I[110];
struct Node{
point a,b;
}A[5];
int sig(double n){
if(fabs(n)<eps)return 0;
else if(n>eps)return 1;
else return -1;
}
bool judge(int a,int b){//判断线段相交
if(min(A[a].a.x,A[a].b.x)>max(A[b].a.x,A[b].b.x)||min(A[a].a.y,A[a].b.y)>max(A[b].a.y,A[b].b.y)||min(A[b].a.x,A[b].b.x)>max(A[a].a.x,A[a].b.x)||min(A[b].a.y,A[b].b.y)>max(A[a].a.y,A[a].b.y))
return false;
double h,i,j,k;
h=(A[a].b.x-A[a].a.x)*(A[b].a.y-A[a].a.y)-(A[a].b.y-A[a].a.y)*(A[b].a.x-A[a].a.x);
i=(A[a].b.x-A[a].a.x)*(A[b].b.y-A[a].a.y)-(A[a].b.y-A[a].a.y)*(A[b].b.x-A[a].a.x);
j=(A[b].b.x-A[b].a.x)*(A[a].a.y-A[b].a.y)-(A[b].b.y-A[b].a.y)*(A[a].a.x-A[b].a.x);
k=(A[b].b.x-A[b].a.x)*(A[a].b.y-A[b].a.y)-(A[b].b.y-A[b].a.y)*(A[a].b.x-A[b].a.x);
return h*i<=eps&&j*k<=eps;
}
void getinter(point p1,point p2,point p3,point p4,point &inter){//求交点
inter=p1;
double ans=((p1.x-p3.x)*(p3.y-p4.y)-(p1.y-p3.y)*(p3.x-p4.x))/((p1.x-p2.x)*(p3.y-p4.y)-(p1.y-p2.y)*(p3.x-p4.x));
inter.x+=(p2.x-p1.x)*ans;
inter.y+=(p2.y-p1.y)*ans;
}
double dist(point p1,point p2){
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
double cp(point p1,point p2,point p3){
return (p3.x-p1.x)*(p2.y-p1.y)-(p3.y-p1.y)*(p2.x-p1.x);
}
bool cmp(point a,point b){
double ans=cp(I[0],a,b);
if(sig(ans)==0){
return dist(I[0],a)-dist(I[0],b)<=0;
}
else return ans>0;
}
int main()
{
double x1,y1,x2,y2,x3,y3,x4,y4;
while(scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4)!=EOF){
S[0].x=x1;S[0].y=y1;S[1].x=x1;S[1].y=y2;S[2].x=x2;S[2].y=y1;
J[0].x=x3;J[0].y=y3;J[1].x=x3;J[1].y=y4;J[2].x=x4;J[2].y=y4;J[3].x=x4;J[3].y=y3;
int cnt=0;
for(int i=0;i<3;++i){//矩形中包含的三角形的点
if(S[i].x>=min(x3,x4)&&S[i].x<=max(x3,x4)&&S[i].y>=min(y3,y4)&&S[i].y<=max(y3,y4)){
I[cnt].x=S[i].x;I[cnt].y=S[i].y;cnt++;
}
}
for(int i=0;i<4;++i){//三角形中包含的矩形中的点
if(J[i].x>=min(x1,x2)&&J[i].x<=max(x1,x2)&&J[i].y>=min(y1,y2)&&J[i].y<=max(y1,y2)){
double k=(y1-y2)/(x2-x1);double b=y1-k*x2;
if((sig(k*x1-y1+b)>=0&&sig(J[i].x*k-J[i].y+b)>=0)||(sig(k*x1-y1+b)<=0&&sig(J[i].x*k-J[i].y+b)<=0)){
I[cnt].x=J[i].x;I[cnt].y=J[i].y;cnt++;
}
}
}
for(int i=0;i<3;++i){
for(int j=0;j<4;++j){//求出三角形和矩形的线段的交点
A[0].a=S[i];A[0].b=S[(i+1)%3];A[1].a=J[j];A[1].b=J[(j+1)%4];
//printf("%lf %lf %lf %lf\n",J[j].x,J[j].y,J[(j+1)%4].x,J[(j+1)%4].y);
if(judge(0,1)){
if(A[0].a.x==A[0].b.x&&A[0].a.x==A[1].a.x&&A[1].a.x==A[1].b.x)continue;
if(A[0].a.y==A[0].b.y&&A[0].a.y==A[1].a.y&&A[1].a.y==A[1].b.y)continue;
//printf("%lf %lf %lf %lf %lf %lf %lf %lf\n",A[0].a.x,A[0].a.y,A[0].b.x,A[0].b.y,A[1].a.x,A[1].a.y,A[1].b.x,A[1].b.y);
getinter(A[0].a,A[0].b,A[1].a,A[1].b,I[cnt]);cnt++;
// printf("%lf %lf\n",I[cnt-1].x,I[cnt-1].y);
}
}
}
// printf("%d\n",cnt);
if(cnt<2){
printf("0.00000000\n");
}
else {
int pos=0;
for(int i=0;i<cnt;++i){
if(I[pos].y>=I[i].y){
if(I[pos].y==I[i].y){
if(I[pos].x>I[i].x)pos=i;
}
else pos=i;
}
}
point temp=I[pos];I[pos]=I[0];I[0]=temp;
sort(I+1,I+cnt,cmp);//极角排序
double ans=0;
for(int i=0;i<cnt;++i){//求多边形面积
double area=(I[(i+1)%cnt].x*I[i].y-I[(i+1)%cnt].y*I[i].x)/2.0;
ans+=area;
}
printf("%.9lf\n",fabs(ans));
}
}
return 0;
}
#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define loop(i,j,k) for (int i = j;i != -1; i = k[i])
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
#define ff first
#define ss second
#define mp(i,j) make_pair(i,j)
#define pb push_back
#define pii pair<int,int>
#define in(x) scanf("%d", &x);
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-9;
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 1e3 + 10;
char s[N];
int n, m;
int c[N][N], a[N][N];
LL ls[N][N], lx[N][N], rs[N][N], rx[N][N];
void GetFour()
{
memset(c, 0, sizeof(c));
rep(i, 1, n)
{
rep(j, 1, m)
{
if (a[i][j] || c[i][j]) continue;
while (c[i][j] <= n - i && !a[i + c[i][j]][j]) c[i][j]++;
rep(k, 2, c[i][j]) c[i + k - 1][j] = c[i][j] - k + 1;
}
int sum = 0; stack<pii> p;
per(j, m, 1)
{
pii now = mp(c[i][j], 1);
while (!p.empty() && p.top().ff > now.ff)
{
pii q = p.top(); sum -= q.ff * q.ss;
now.ss += q.ss; p.pop();
}
sum += now.ff * now.ss;
p.push(now);
ls[i][j] = sum;
}
sum = 0; while (!p.empty()) p.pop();
rep(j, 1, m)
{
pii now = mp(c[i][j], 1);
while (!p.empty() && p.top().ff > now.ff)
{
pii q = p.top(); sum -= q.ff * q.ss;
now.ss += q.ss; p.pop();
}
sum += now.ff * now.ss;
p.push(now);
rs[i][j] = sum;
}
}
memset(c, 0, sizeof(c));
per(i, n, 1)
{
rep(j, 1, m)
{
if (a[i][j] || c[i][j]) continue;
while (c[i][j] < i && !a[i - c[i][j]][j]) c[i][j]++;
rep(k, 2, c[i][j]) c[i - k + 1][j] = c[i][j] - k + 1;
}
int sum = 0; stack<pii> p;
per(j, m, 1)
{
pii now = mp(c[i][j], 1);
while (!p.empty() && p.top().ff > now.ff)
{
pii q = p.top(); sum -= q.ff * q.ss;
now.ss += q.ss; p.pop();
}
sum += now.ff * now.ss;
p.push(now);
lx[i][j] = sum;
}
sum = 0; while (!p.empty()) p.pop();
rep(j, 1, m)
{
pii now = mp(c[i][j], 1);
while (!p.empty() && p.top().ff > now.ff)
{
pii q = p.top(); sum -= q.ff * q.ss;
now.ss += q.ss; p.pop();
}
sum += now.ff * now.ss;
p.push(now);
rx[i][j] = sum;
}
}
}
int main()
{
while (scanf("%d%d", &n, &m) != EOF)
{
rep(i, 0, n + 1) rep(j, 0, m + 1) ls[i][j] = rs[i][j] = 0;
rep(i, 1, n)
{
scanf("%s", s + 1);
rep(j, 1, m) a[i][j] = s[j] - '0';
}
GetFour();
LL L = 0, R = 0, ans = 0;
rep(i, 1, n)
{
rep(j, 1, m) (R += ls[i][j]) %= mod;
(ans += L * R) %= mod; R = 0;
rep(j, 1, m) (L += rx[i][j]) %= mod;
}
L = R = 0;
rep(j, 1, m)
{
rep(i, 1, n) (R += ls[i][j]) %= mod;
(ans += L * R) %= mod; R = 0;
rep(i, 1, n) (L += rx[i][j]) %= mod;
}
rep(i, 1, n)
{
rep(j, 1, m)
{
rx[i][j] += rx[i - 1][j] + rx[i][j - 1] - rx[i - 1][j - 1];
(ans -= rx[i][j] * ls[i + 1][j + 1]) %= mod;
}
per(j, m, 1)
{
lx[i][j] += lx[i - 1][j] + lx[i][j + 1] - lx[i - 1][j + 1];
(ans -= lx[i][j] * rs[i + 1][j - 1]) %= mod;
}
}
printf("%lld\n", (ans + mod) % mod);
}
return 0;
}
1815: Enterprising Escape
#include <iostream>
#include <cstring>
#include <stack>
#include <cstdio>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
const double eps=1e-8;
const double PI=acos(-1.0);
using namespace std;
struct st{
int x;
int y;
int step;
friend bool operator <(st a,st b){
return a.step>b.step;}
}node;
char a[1005][1005];
int v[1005][1005];
int d[][2]={{1,0},{0,1},{0,-1},{-1,0}};
int k,n,m,num;
map<char,int> s;
int bfs(int x,int y)
{
priority_queue<st>q;
node.x=x;
node.y=y;
node.step=0;
q.push(node);
v[x][y]=1;
while(!q.empty())
{
st tem=q.top();
//cout<<tem.x<<" "<<tem.y<<endl;
q.pop();
st tp;
if(tem.x>=m-1||tem.y>=n-1||tem.x<=0||tem.y<=0)
{
return tem.step;//cout<<"t:"<<e<<endl;
}
for(int i=0; i<4; i++)
{
tp.x=tem.x+d[i][0];
tp.y=tem.y+d[i][1];
if(v[tp.x][tp.y]) continue;
tp.step=tem.step+s[a[tp.x][tp.y]];
q.push(tp);
v[tp.x][tp.y]=1;
//cout<<tp<<endl;
}
}
return -1;
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
memset(v,0,sizeof(v));
char c;
scanf("%d%d%d",&k,&n,&m);
getchar();
for(int i=0;i<k;i++){
scanf("%c %d",&c,&num);
getchar();
s[c]=num;
}
int x,y;
for(int i=0;i<m;i++)
scanf("%s",a[i]);
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
if(a[i][j]=='E'){
x=i;y=j;
}
printf("%d\n",bfs(x,y));
}
return 0;
}
1817: Bones’s Battery
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<math.h>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
const double pi=acos(-1.0);
const int inf = 0x3f3f3f3f;
const int maxn=100005;
const double eps=1e-8;
const long long INF=10000000000;
int n,k,m;
long long dis[105][105], red[105][105];
void init(long long dis[][105]){//初始化数组
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
if (i==j)dis[i][j]=0;
else dis[i][j]=INF;
}
}
}
void floyd(long long dis[][105]){//folyd 求各源点间的最短距离
for(int t=0;t<n;t++){
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
dis[i][j]=min(dis[i][j],dis[i][t]+dis[t][j]);
}
}
}
}
int main()
{
int t;
scanf ("%d",&t);
while (t--){
scanf ("%d%d%d",&n,&k,&m);
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
if (i==j)dis[i][j]=0;
else dis[i][j]=INF;
}
}
int u,v,val;
for (int i=0;i<m;i++){
scanf ("%d%d%d",&u,&v,&val);
dis[u][v]=dis[v][u]=val;
}
floyd(dis);
long long lo=1,hi=INF;
long long mid;
long long ans=INF;
while (lo<=hi){//二分答案
mid=(lo+hi)/2;
init(red);
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
red[i][j]= dis[i][j]<=mid?1:INF;//如果小于mid说明i->j只需要充一次电
}
}
floyd(red);//跑floyd
long long d=0;
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
if (red[i][j]>d)d=red[i][j];//求出充电次数最多的一条路径
}
}
if (d<=k){//如果次数小于k说明此时的距离符合
ans=mid;//保存
hi=mid-1;
}
else lo=mid+1;
}
printf ("%lld\n",ans);//输出
}
return 0;
}
1818: Crusher’s Code
#include<iostream>
#include<string.h>
#include<algorithm>
#include<map>
#include<queue>
using namespace std;
int n;
int A[10];
void init(){
int B[10], v[105], sz = 0;
memcpy(B, A, sizeof(A));
memset(v, -1, sizeof(v));
sort(B, B + n);
for (int i = 0; i<n; i++)
if (v[B[i]] == -1) v[B[i]] = sz++;
for (int i = 0; i<n; i++) {
A[i] = v[A[i]];
}
}
int get_sta(int A[]){
int ret = 0;
for (int i = 0; i<n; i++) ret = ret * 8 + A[i];
return ret;
}
void get_arr(int S, int A[]){
for (int i = n - 1; i >= 0; i--){
A[i] = S % 8;
S /= 8;
}
}
const int MX = 1e5 + 5;
map<int, int>vis;
int sz, S[MX];
void bfs(int s){
vis.clear();
queue<int>q;
q.push(s);
vis[s] = 1;
while (!q.empty()){
int u = q.front(); q.pop();
get_arr(u, A);
for (int i = 0; i<n; i++)
for (int j = i + 1; j<n; j++)
if (A[i]>A[j]){
swap(A[i], A[j]);
int SS = get_sta(A);
if (vis[SS] == 0){
q.push(SS);
vis[SS] = 1;
}
swap(A[i], A[j]);
}
}
sz = 0;
for (map<int, int>::iterator it = vis.begin(); it != vis.end(); it++) {
S[sz] = it->first;
it->second = sz++;
}
}
double dp[MX];
//递推写法
double DP1(){
memset(dp, 0, sizeof(dp));
for (int k = 0; k<sz; k++){
int success = 0;
int sta = S[k];
get_arr(sta, A);
for (int i = 0; i<n; i++)
for (int j = 0; j<n; j++){
int l = min(i, j), r = max(i, j);
if (A[l]>A[r]){
swap(A[l], A[r]);
int SS = get_sta(A);
success++;
dp[k] += dp[vis[SS]] / (n*n);
swap(A[l], A[r]);
}
}
if (success>0) dp[k] = (dp[k] + 1)*n*n / success;
}
return dp[sz - 1];
}
double DP2(){
memset(dp, 0, sizeof(dp));
for (int k = 0; k<sz; k++){
int success = 0;
int sta = S[k];
get_arr(sta, A);
for (int l = 0; l<n - 1; l++){
int r = l + 1;
if (A[l]>A[r]){
swap(A[l], A[r]);
int SS = get_sta(A);
success++;
dp[k] += dp[vis[SS]] / (n - 1);
swap(A[l], A[r]);
}
}
if (success>0) dp[k] = (dp[k] + 1)*(n - 1) / success;
}
return dp[sz - 1];
}
//记忆化搜索写法
double DP1(int u){
if (u == 0) return dp[u] = 0;
if (dp[u] >= 0) return dp[u];
int sta = S[u];
get_arr(sta, A);
int success = 0;
dp[u] = 0;
for (int i = 0; i<n; i++){
for (int j = 0; j<n; j++){
int l = min(i, j), r = max(i, j);
if (A[l]>A[r]){
swap(A[l], A[r]);
int SS = get_sta(A);
success++;
dp[u] += DP1(vis[SS]) / (n*n);
swap(A[l], A[r]);
}
}
}
if (success>0) dp[u] = (dp[u] + 1)*n*n / success;
return dp[u];
}
double DP2(int u){
if (u == 0) return dp[u] = 0;
if (dp[u] >= 0) return dp[u];
int sta = S[u];
get_arr(sta, A);
int success = 0;
dp[u] = 0;
for (int i = 0; i<n - 1; i++){
int l = i, r = i + 1;
if (A[l]>A[r]){
swap(A[l], A[r]);
int SS = get_sta(A);
success++;
dp[u] += DP2(vis[SS]) / (n - 1);
swap(A[l], A[r]);
}
}
if (success>0) dp[u] = (dp[u] + 1)*(n - 1) / success;
return dp[u];
}
int main(){
int T;
scanf("%d", &T);
while (T--){
scanf("%d", &n);
for (int i = 0; i<n; i++) scanf("%d", &A[i]);
init();
int init_sta = get_sta(A);
bfs(init_sta);
for (int i = 0; i<sz; i++) dp[i] = 0;
double ans1 = DP1();
for (int i = 0; i<sz; i++) dp[i] = 0;
double ans2 = DP2();
printf("Monty %.6f Carlos %.6f\n", ans1, ans2);
}
return 0;
}
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<math.h>
#include<map>
#include<queue>
#include<sstream>
#include<algorithm>
using namespace std;
const double pi=acos(-1.0);
const int inf = 0x3f3f3f3f;
const int maxn=100005;
const double eps=1e-8;
int n;
map<string,string>mp;//map
string a,b,c;
int main()
{
scanf ("%d",&n);
mp.clear();//清空
getchar(); //得将换行符吃掉,没加这个就过不了
for (int i=0;i<n;i++){
getline(cin,a);
stringstream ss(a);//定义
ss>>b;//先输入语言名称
while (ss>>c){
for (int j=0;j<c.size();j++){
if (c[j]>='A'&&c[j]<='Z'){
c[j]+=32;//改成小写
}
}
mp[c]=b;//映射
}
}
while (getline(cin,a)){
string tmp;
int len=a.size();
for (int i=0;i<len;i++){//把标点符号改成空格
if (a[i]==','||a[i]=='.'||a[i]=='!'||a[i]==';'||a[i]=='?'||a[i]=='('||a[i]==')'){
a[i]=' ';
}
}
stringstream ss1(a);//定义
while (ss1>>tmp){
for (int i=0;i<tmp.size();i++){
if (tmp[i]>='A'&&tmp[i]<='Z'){
tmp[i]+=32;//该小写
}
}
if (mp.count(tmp)){
cout<<mp[tmp]<<endl;//如果找到就输出
break;
}
}
}
return 0;
}
1828: Dictionary
#include<iostream>
#include<cstdio>
using namespace std;
#define ll long long
int f[10];
void init()
{
f[1]=1;
for(int i=2;i<10;i++)
f[i]=f[i-1]*i;
}
int main()
{
int N,M;
char s[10];
init();
scanf("%d",&N);
while(N--)
{
int ans=0;
scanf("%s",s);
for(int i=0;i<9;i++)
{
int temp=0;
for(int j=i+1;j<9;j++)
if(s[j]<s[i])temp++;
ans+=temp*f[9-i-1];
}
printf("%d\n",ans+1);
}
return 0;
}
1830: FarAway
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <vector>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int maxn=1e5+10;
int dis[maxn];
struct Edge{
int v,val;
};
vector<Edge>G[maxn];
void bfs(){
queue<int>q;
q.push(1);
dis[1]=0;
while (!q.empty()){
int u=q.front();q.pop();
for (int i=0;i<G[u].size();i++){
int v=G[u][i].v;
dis[v]=dis[u]+G[u][i].val;
q.push(v);
}
}
}
int C,M;
int main()
{
int T;
scanf ("%d",&T);
while (T--){
scanf ("%d%d",&C,&M);
for (int i=0;i<=C;i++)G[i].clear();
int u,v,val;
for (int i=0;i<C-1;i++){
scanf ("%d%d%d",&u,&v,&val);
G[u].push_back(Edge{v,val});
}
bfs();
int ans=0;
for (int i=1;i<=C;i++){
ans=max(dis[i],ans);
}
if (ans>=M)printf ("%d\n",ans);
else printf ("-1\n");
}
return 0;
}
1831: Found
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
const int mod = 9973;
int n,m;
long long num;
int mp[105][105];
struct matrix{
int mat[105][105];
matrix operator * (const matrix& t) const{
matrix tmp;
for (int i=0;i<n*n;i++){
for (int j=0;j<n*n;j++){
tmp.mat[i][j]=0;
for (int k=0;k<n*n;k++){
tmp.mat[i][j]+=mat[i][k]*t.mat[k][j]%mod;
tmp.mat[i][j]%=mod;
}
}
}
return tmp;
}
}res;
matrix Pow(matrix &t,long long num){
matrix ans;
memset (ans.mat,0,sizeof (ans.mat));
for (int i=0;i<n;i++) ans.mat[i][i]=1;
while(num){
if (num&1) ans=ans*t;
num>>=1;
t=t*t;
}
return ans;
}
int main()
{
int cas;
scanf ("%d",&cas);
while (cas--){
scanf ("%d%d%lld",&n,&m,&num);
memset (mp,0,sizeof (mp));
memset (res.mat,0,sizeof (res.mat));
int u,v;
for (int i=0;i<m;i++){
scanf ("%d%d",&u,&v);
mp[u][v]=1;
mp[v][u]=1;
}
for (int i=0;i<n*n;i++){
int x1=i%n+1,y1=i/n+1;//x1表示a当前的位置,y1表示b当前的位置
if (x1==y1) continue;//a,b两个人的位置不能一样
for (int j=0;j<n*n;j++){
int x2=j%n+1,y2=j/n+1;//x2表示a下一分钟可能到达的位置,y1表示b下一分钟可能到达的位置
if(x2==y2)continue;
if (x1==x2||y1==y2)continue;//a,b移动前后不能一样
if (!mp[x1][x2]||!mp[y1][y2]) continue;
res.mat[i][j]=1;
}
}
res=Pow(res,num-1);
int ans=0;
for (int i=0;i<n*n;i++){
int x1=i%n+1,y1=i/n+1;
if (x1==y1)continue;
//n-1表示的是初始状态(0,n-1 ==> 也就是一开始a在0,b在n-1的位置)
//然后最终在i这个位置相遇,而i这个状态中,a的位置为i%n+1,b的位置为i/n+1
if (!res.mat[n-1][i])continue;
for (int j=1;j<=n;j++){
if (mp[x1][j]&&mp[y1][j]){//如果两个人在下一秒能同时移动到同一点
ans+=res.mat[n-1][i]%mod;
ans%=mod;
}
}
}
printf ("%d\n",ans);
}
return 0;
}
1835: Pry Sequence Transformation
#include<cstdio>
#include<algorithm>
#include<string.h>
using namespace std;
const int MX = 2e4 + 5;
char S[MX],T[MX];
double dp[2][MX];
int val(char c){return c-'a'+1;}
double INS(char c){return 1+val(c)*0.01;}
double REP(char c1,char c2){return c1==c2?0:(val(c1)+val(c2))*0.1;}
int main(){
int cas;
int k;
//freopen("in.txt","r",stdin);
scanf("%d",&cas);
while(cas--){
scanf("%d",&k);
scanf("%s%s",S+1,T+1);
int n=strlen(S+1),m=strlen(T+1);
dp[0][0]=0;
for(int i=1;i<=k;i++) dp[0][i]=dp[0][i-1]+INS(T[i]);
int cur,pre;
for(int i=1;i<=n;i++){
cur=i%2;
pre=cur^1;
dp[cur][0]=i;
int mx=min(m,i+k+1);
for(int j=max(1,i-k-1);j<=mx;j++){
dp[cur][j]=dp[pre][j-1]+REP(S[i],T[j]);
if(abs(i-j)<=k) dp[cur][j]=min(dp[cur][j],dp[pre][j]+1);
if(abs(j-i)<=k) dp[cur][j]=min(dp[cur][j],dp[cur][j-1]+INS(T[j]));
}
}
if(dp[cur][m]>k) printf("TOSS\n");
else printf("%.4f\n",dp[cur][m]);
}
return 0;
}
#include<cstdio>
#include<algorithm>
#include<string.h>
#include<vector>
#include<queue>
#include<string>
#include<iostream>
#include<sstream>
using namespace std;
typedef unsigned long long ULL;
const int MX = 7e4 + 5;
const int inf = 0x3f3f3f3f;
const int P1 = 1000001;
const int P2 = 3111111;
queue<int>q;
struct Edge{
int u,v,nxt;
}E[MX*4];
ULL H[MX];
int val[MX],level[MX],head[MX],tot,IN[MX];
char buff[MX*100];
void init(){
while(!q.empty())q.pop();
memset(IN,0,sizeof(IN));
memset(head,-1,sizeof(head));
memset(level,0,sizeof(level));
tot=0;
}
void add(int u,int v){
E[tot].u=u;
E[tot].v=v;
E[tot].nxt=head[u];
head[u]=tot++;
IN[v]++;
}
struct Tree{
int n,root[2],rt;
ULL Hash[2];
void top_sort(){
for(int i=1;i<=n;i++) if(IN[i]==1) q.push(i);
int Max_L=0;
while(!q.empty()){
int u=q.front();q.pop();
Max_L=max(Max_L,level[u]);
for(int i=head[u];~i;i=E[i].nxt){
int v=E[i].v;
IN[v]--;
if(IN[v]==1){
level[v]=level[u]+1;
q.push(v);
}
}
}
rt=0;
for(int i=1;i<=n;i++) if(level[i]==Max_L) {
root[rt++]=i;
}
}
void read(){
n=1;
gets(buff);
int u=0,v=0,len=strlen(buff);
buff[len++]=' ';
for(int i=0,j=0;i<len;i++){
if(buff[i]==' ') {
if(j%2==1) {
add(u,v);add(v,u);
u=v=0;
n++;
}
j++;
continue;
}
if(j%2==0) u=u*10+buff[i]-'0';
else v=v*10+buff[i]-'0';
}
for(int i=1;i<=n;i++) scanf("%d",&val[i]);
gets(buff);
}
}T[105];
int dfs(int u,int fa){
if(fa==-1) H[u]=P1;
else H[u]=(ULL)(val[fa]-val[u])^P1;
for(int i=head[u];~i;i=E[i].nxt){
int v=E[i].v;
if(v==fa) continue;
H[u]*=dfs(v,u)^P2;
}
return H[u];
}
bool cmp(int x,int y){
for(int i=0;i<T[x].rt;i++){
for(int j=0;j<T[y].rt;j++){
if(T[x].Hash[i]==T[y].Hash[j]) return 1;
}
}
return 0;
}
int vis[105];
vector<int>v[105];
int main(){
int d;
//freopen("in.txt","r",stdin);
while(~scanf("%d\n",&d)){
for(int i=0;i<d;i++) {
init();
T[i].read();
T[i].top_sort();
for(int j=0;j<T[i].rt;j++) T[i].Hash[j]=dfs(T[i].root[j],-1);
}
for(int i=0;i<d;i++) v[i].clear();
memset(vis,0,sizeof(vis));
for(int i=0;i<d;i++){
if(vis[i]) continue;
for(int j=i+1;j<d;j++){
if(vis[j]||T[i].n!=T[j].n) continue;
if(cmp(i,j)){
vis[i]=vis[j]=1;
v[i].push_back(j);
}
}
}
int tot=d;
priority_queue<int,vector<int>,greater<int> >Q;
for(int i=0;i<d;i++) if(v[i].size()>0){
tot-=v[i].size()+1;
Q.push(v[i].size()+1);
}
for(int i=0;i<tot;i++) Q.push(1);
printf("%d",Q.top());Q.pop();
while(!Q.empty()){
printf(" %d",Q.top());Q.pop();
}
printf("\n");
}
return 0;
}
1838: Water Pump
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<stack>
#define MAXN 100000
#define INF 2147483647
using namespace std;
struct node
{
int h,id;
node(){}
node(int _h,int _id):h(_h),id(_id){}
};
int lh[MAXN],rh[MAXN],wah[MAXN],l[MAXN],r[MAXN];
int sl[MAXN],sr[MAXN],a[MAXN],n;
stack<node> q;
int main()
{
int t;
scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
lh[i]=rh[i]=wah[i]=l[i]=r[i]=sl[i]=sr[i]=0;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
// calculate the first higher pump from the left and right
q.push(node(INF,0));
for(int i=1;i<=n;i++)
{
while (a[i]>q.top().h) q.pop();
lh[i]=q.top().id;
q.push(node(a[i],i));
}
while (!q.empty()) q.pop();
q.push(node(INF,0));
for(int i=n;i>=1;i--)
{
while (a[i]>q.top().h) q.pop();
rh[i]=q.top().id;
q.push(node(a[i],i));
}
// calculate the height of the water of each segment
int i=1,j=1;
while (rh[i])
{
while (j<rh[i])
{
l[j]=i,r[j]=rh[i];
wah[j++]=a[i];
}
i=rh[i];
}
i=j=n;
while(lh[i])
{
while (j>lh[i])
{
wah[--j]=a[i];
l[j]=lh[i],r[j]=i;
}
i=lh[i];
}
//calculate possible sum of the water above left and right
for(int i=2;i<=n;i++)
if (lh[i]) sl[i]=sl[lh[i]]+(i-lh[i])*a[i];
else sl[i]=wah[i-1]*(r[i-1]-l[i-1])+sl[l[i-1]];
for(int i=n-1;i>0;i--)
if (rh[i]) sr[i]=sr[rh[i]]+(rh[i]-i)*a[i];
else sr[i]=wah[i]*(r[i]-l[i])+sr[r[i]];
//find maximum answer
int ans=0;
for(int i=1;i<n;i++)
{
int lwa=sl[i]-sl[l[i]];
int rwa=sr[i+1]-sr[r[i]];
int water=wah[i]*(r[i]-l[i]);
ans=max(ans,water-lwa-rwa);
}
printf("%d\n",ans);
}
}
1839: Who Feels Lucky
#include<cstdio>
#include<algorithm>
#include<string.h>
#include<iostream>
#define fuck(x) cout<<"["<<x<<"]"<<endl
using namespace std;
const int MX = 35;
const double eps=1e-6;
int a[MX],b[MX],tot[MX],s1[MX],s2[MX];
double L,R;
double get_L(int i,int k){return 1.0*(k*tot[i]-b[i])/s1[i];}
double get_R(int i,int k){return 1.0*k*tot[i]/s2[i];}
int get_K(int i,double t,bool &flag){
int cnt=(int)t/tot[i];
if (cnt*tot[i]==t) cnt--;
flag=cnt*tot[i]+a[i]>t?0:1;
return ++cnt;
}
bool check(int i){
bool flag1,flag2;
double l,r;
int maxK=get_K(i,R*s2[i],flag1);
int minK=get_K(i,L*s1[i],flag2);
if (maxK<minK||maxK==minK&&!flag1&&!flag2) return 0;
for (int k=maxK;k>=minK;k--){
r=get_R(i,k);
l=get_L(i,k);
if(r<L||l>R) continue;
R=min(R,r);
L=max(L,l);
return 1;
}
return 0;
}
int main(){
//freopen("in.txt","r",stdin);
int n,T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=1;i<=n;i++) {
scanf("%d%d%d%d",&a[i],&b[i],&s1[i],&s2[i]);
tot[i]=a[i]+b[i];
}
int flag=0,i;
int K=b[n]*s2[n]/((s2[n]-s1[n])*tot[n]);
for(int k=K;k>0;k--){
L=get_L(n,k);R=get_R(n,k);
for(i=1;i<=n;i++) if(!check(i)) break;
if(i==n+1){
flag=1;
break;
}
}
if(!flag) printf("IMPOSSIBLE\n");
else printf("%.4lf\n",1.0/R);
}
return 0;
}
1840: Lawn mower
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string.h>
using namespace std;
double xi[1005],yi[1005];
int main()
{
int nx,ny;
double w;
while(cin>>nx>>ny>>w)
{
bool suc=true;
if(nx==0&&ny==0&&abs(w)<1e-5)
break;
for(int i=0;i<nx;i++)
cin>>xi[i];
for(int i=0;i<ny;i++)
cin>>yi[i];
sort(xi,xi+nx);
sort(yi,yi+ny);
double endx=xi[0]+w/2.0;
double endy=yi[0]+w/2.0;
for(int i=1;i<nx;i++)
if(endx<xi[i]-w/2.0)
{
suc=false;
break;
}
else
endx=xi[i]+w/2.0;
if(endx<75&&suc)
suc=false;
if(suc)
{
for(int i=1;i<ny;i++)
if(endy<yi[i]-w/2.0)
{
suc=false;
break;
}
else
endy=yi[i]+w/2.0;
if(endy<100)
suc=false;
}
if(suc)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}
1842: Fake scoreboard
#include<iostream>
#define MAX_V 200
#include<string.h>
using namespace std;
int n,m;
namespace MaxNetFlow
{
struct Edge
{
int dest,cap;
Edge *next,*pair;
Edge() {}
Edge(int _dest,int _cap,Edge* _next):dest(_dest),cap(_cap),next(_next){}
};
Edge *e[MAX_V],*c[MAX_V],*p[MAX_V];
int Stack[MAX_V],Queue[MAX_V],dist[MAX_V];
int s,t,maxflow,top;
inline void clear()
{
memset(e,0,sizeof(e));
}
inline void addedge(int a,int b,int c)
{
e[a]=new Edge(b,c,e[a]);
e[b]=new Edge(a,0,e[b]);
e[a]->pair=e[b],e[b]->pair=e[a];
}
inline bool lab()
{
memset(dist,0x3f,sizeof(dist));
dist[s]=0; Queue[1]=s;
for (int l=1,r=1;l<=r;l++)
{
int i=Queue[l];
for (Edge *j=e[i];j;j=j->next)
if (j->cap && dist[j->dest]>dist[i]+1)
{
dist[j->dest]=dist[i]+1;
if (j->dest==t) return 1;
Queue[++r]=j->dest;
}
}
return 0;
}
inline void aug()
{
int top=0; Stack[++top]=s;
memcpy(c,e,sizeof(e));
while (top)
{
int i=Stack[top];
if (i!=t)
{
Edge *j;
for (j=c[i];j;j=j->next)
if (j->cap && dist[j->dest]==dist[i]+1) break;
if (j) top++,Stack[top]=j->dest,c[i]=p[top]=j;
else top--,dist[i]=1e9;
} else
{
int delta=1e9;
for (int i=top;i>=2;i--)
delta=std::min(delta,p[i]->cap);
maxflow+=delta;
for (int i=top;i>=2;i--)
{
p[i]->cap-=delta,p[i]->pair->cap+=delta;
if (!p[i]->cap) top=i-1;
}
}
}
}
inline int dinic()
{
maxflow=0;
while (lab()) aug();
return maxflow;
}
inline void BuildAnswer()
{
for(int i=1;i<=n;i++)
{
for(Edge *j=e[i];j;j=j->next)
{
if (j->dest==s || j->dest==t) continue; //连向源汇的边当然不用产生答案
bool flag=(j->cap);
if (!flag)
{
addedge(s,i,1);
addedge(j->dest,t,1);
j->cap=j->pair->cap=0; //尝试封死这条边
dinic(); //看是否能找到增广路
if (maxflow<=0) e[s]->cap=e[j->dest]->cap=0; //找不到则封死新加的边
else flag=1;
}
j->cap=j->pair->cap=0; //直接封死该边
if (flag) putchar('N');
else putchar('Y');
}
putchar('\n');
}
}
}
int main()
{
while (~scanf("%d%d",&n,&m) && n && m)
{
int sum1=0,sum2=0,c;
MaxNetFlow::clear();
int s=MaxNetFlow::s=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&c); sum1+=c;
MaxNetFlow::addedge(s,i,c);
}
int t=MaxNetFlow::t=n+m+1;
for(int i=1;i<=m;i++)
{
scanf("%d",&c); sum2+=c;
MaxNetFlow::addedge(i+n,t,c);
}
for(int i=n;i>=1;i--)
for(int j=m;j>=1;j--) //因为BuildAnswer()遍历边的时候是按照先加入后遍历的
MaxNetFlow::addedge(i,j+n,1); //所以为了满足字典序,我们就从后往前加边
MaxNetFlow::dinic();
int maxflow=MaxNetFlow::maxflow;
if (sum1!=sum2 || maxflow!=sum1) {puts("Impossible\n");continue;}
MaxNetFlow::BuildAnswer();
putchar('\n');
}
}
#include<iostream>
#include<string.h>
#include<map>
#include<queue>
using namespace std;
deque<int> que,ans;
int num[1<<21],fa[1<<21];
int n,m,nxt[21];
bool v[1<<21];
int main()
{
while(~scanf("%d%d",&n,&m) && n)
{
bool flag=0; int l=1<<n;
for(int i=0;i<l;i++) v[i]=fa[i]=0;
memset(nxt,0,sizeof(nxt));
for(int i=1;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
nxt[u]|=(1<<v);
nxt[v]|=(1<<u);
}
if (m>n-1)
{
puts("Impossible");
continue;
}
v[(1<<n)-1]=1;
que.clear();
que.push_back((1<<n)-1);
while (!que.empty() && !flag)
{
int cur=que.front();
que.pop_front();
for(int i=0;i<n;i++)
{
int next=0;
if (cur&(1<<i))
{
for(int j=0;j<n;j++)
if ((cur&(1<<j)) && j!=i) next|=nxt[j];
if (!v[next])
{
v[next]=1;
num[next]=i;
fa[next]=cur;
que.push_back(next);
if (!next) {flag=1;break;}
}
}
}
}
if (flag)
{
ans.clear();
for(int cur=0;fa[cur];cur=fa[cur])
ans.push_back(num[cur]);
printf("%d: ",ans.size());
for(int i=ans.size()-1;i>=0;i--)
printf("%d%c",ans[i],!i? '\n':' ');
} else puts("Impossible");
}
return 0;
}
1846: Assembly line
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int cost[128][128],sz,dp[210][210][26],n,m,len;
char sym[26],ch[128][128],str[210];
bool first;
int main(){
ios_base::sync_with_stdio(false);
while(cin>>n&&n){
if(first)
cout<<endl;
else
first=true;
for(int i=0;i<n;++i)
cin>>sym[i];
for(int i=0;i<n;++i)
for(int j=0;j<n;++j)
cin>>cost[sym[i]][sym[j]]>>ch[sym[i]][sym[j]]>>ch[sym[i]][sym[j]];
cin>>m;
while(m--){
memset(dp,-1,sizeof dp);
cin>>str;
len=strlen(str);
for(int i=0;i<len;++i)
dp[i][i][str[i]]=0;
for(int length=2;length<=len;++length)
for(int i=0,j=length-1;j<len;++i,++j)
for(int k=i;k<j;++k)
for(int le=0;le<n;++le)
if(dp[i][k][sym[le]]!=-1)
for(int ri=0;ri<n;++ri)
if(dp[k+1][j][sym[ri]]!=-1)
if(dp[i][j][ch[sym[le]][sym[ri]]]==-1||dp[i][j][ch[sym[le]][sym[ri]]]>dp[i][k][sym[le]]+dp[k+1][j][sym[ri]]+cost[sym[le]][sym[ri]])
dp[i][j][ch[sym[le]][sym[ri]]]=dp[i][k][sym[le]]+dp[k+1][j][sym[ri]]+cost[sym[le]][sym[ri]];
int ans=-1;
for(int i=0;i<n;++i)
if(dp[0][len-1][sym[i]]!=-1)
ans=(ans==-1?i:(dp[0][len-1][sym[i]]<dp[0][len-1][sym[ans]]?i:ans));
cout<<dp[0][len-1][sym[ans]]<<'-'<<sym[ans]<<endl;
}
}
return 0;
}
1848: 3-sided dice
#include<cstdio>
#include<algorithm>
#include<string.h>
#include<stdlib.h>
using namespace std;
struct node{
int x,y,z;
node(){}
node(int X,int Y,int Z){x=X;y=Y;z=Z;}
bool operator==(const node P)const{
return x==P.x&&y==P.y&&z==P.z;
}
}p[5];
int Cross(int i,int j,int k){
node v1(p[i].x-p[k].x,p[i].y-p[k].y,p[i].z-p[k].z),v2(p[j].x-p[k].x,p[j].y-p[k].y,p[j].z-p[k].z);
return (v1.y*v2.z-v1.z*v2.y)+(v1.z*v2.x-v1.x*v2.z)+(v1.x*v2.y-v1.y*v2.x);
}
bool check(int i,int j,int k){
node v1(p[k].x-p[i].x,p[k].y-p[i].y,p[k].z-p[i].z),v2(p[k].x-p[j].x,p[k].y-p[j].y,p[k].z-p[j].z);
//向量每项必须符号相反(有一个为0,另一个也必须是0),否则两向量不相反
if((v1.x==0&&v2.x!=0)||(v1.x!=0&&v2.x==0)) return 0;
if((v1.y==0&&v2.y!=0)||(v1.y!=0&&v2.y==0)) return 0;
if((v1.z==0&&v2.z!=0)||(v1.z!=0&&v2.z==0)) return 0;
if(v1.x*v2.x>0||v1.y*v2.y>0||v1.z*v2.z>0) return 0;
//如果A=α*B(α<0),那么A与B共线且方向相反
if(v1.x==0&&v1.y==0&&v1.z==0) return 0;
if(v1.x!=0&&v1.y!=0&&v1.x*v2.y!=v1.y*v2.x) return 0;
if(v1.x!=0&&v1.z!=0&&v1.x*v2.z!=v1.z*v2.x) return 0;
if(v1.y!=0&&v1.z!=0&&v1.y*v2.z!=v1.z*v2.y) return 0;
return 1;
}
int main(){
//freopen("in.txt","r",stdin);
while(~scanf("%d%d%d",&p[0].x,&p[0].y,&p[0].z),p[0].x||p[0].y||p[0].z){
for(int i=1;i<4;i++) scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].z);
if(p[0]==p[1]&&p[1]==p[2]) { //3个向量相等
printf("%s\n",p[3]==p[0]?"YES":"NO");
continue;
}
int area=abs(Cross(0,1,2));
if(area==0){ //3条向量共面
if(p[0]==p[1]||p[0]==p[2]||p[1]==p[2]) {
bool flag;
if(p[0]==p[1]) flag=check(0,2,3);
else if(p[0]==p[2]) flag=check(0,1,3);
else flag=check(1,2,3);
printf("%s\n",flag?"YES":"NO");
}
else printf("%s\n",(check(0,1,3)||check(0,2,3)||check(1,2,3))?"YES":"NO");
}
else{
//第四个点必须在三角形内
//每个向量的可能性必须大于0,3个小三角形面积必须大于0且等于总面积
int area1=abs(Cross(0,1,3));
int area2=abs(Cross(0,2,3));
int area3=abs(Cross(1,2,3));
if(area1>0&&area2>0&&area3>0&&area==area1+area2+area3) printf("YES\n");
else printf("NO\n");
}
}
return 0;
}
1849: Comparing answers
#include <cstdio>
#include <iostream>
#include <set>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
using ll = long long;
const int maxn = 1e3 + 9;
typedef vector<ll> vec;
typedef vector<vec> mat;
ll n, M = 1e9;
vec mul(mat &A, vec &B)
{
vec C(A.size());
for (int i = 0; i<A.size(); i++)
for (int j = 0; j<B.size(); j++){
C[i] = (C[i] + A[i][j] * B[j]);
}
return C;
}
int main()
{
while (cin >> n){
if (n == 0)break;
mat A(n, vec(n)), B(n, vec(n));
for (int i = 0; i<n; i++)
for (int j = 0; j<n; j++)
cin >> A[i][j];
for (int i = 0; i<n; i++)
for (int j = 0; j<n; j++)
cin >> B[i][j];
vec H(n), TE(n);
for (int i = 0; i<n; i++){
H[i] = i + 1;
TE[i] = i + 1;
}
H = mul(A, H);
H = mul(A, H);
TE = mul(B, TE);
int f = 1;
for (int i = 0; i<n; i++){
if (TE[i] != H[i])
f = 0;
}
if (f)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
1850: Grade School Multiplication
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
void PrintLine(int len)
{
while (len--)
printf("-");
printf("\n");
}
int main(void)
{
long long x,y;
int num = 1;
while (scanf("%lld %lld", &x, &y))
{
if (x==0&&y==0)
break;
printf("Problem %d\n",num++);
long long s, ss, yy;
int f=0;
s = x*y;
ss=s;
yy=y;
int len = 0;
while (ss)
{
len++;
ss/=10;
}
int lenx = (int)log10(x)+1;
int leny = (int)log10(y)+1;
for (int i = 0; i < len-lenx; i++)
printf(" ");
printf("%lld\n",x);
for (int i = 0; i < len-leny; i++)
printf(" ");
printf("%lld\n",y);
PrintLine(len);
int p = 0, t=0;
s = 1;
while (yy)
{
while (yy%10==0)
{
yy/=10;
p++;
t++;
}
s = s * (yy%10) * x;
yy/=10;
int lens = (int)log10(s)+1;
for (int i = 0; i < len-lens-p; i++)
printf(" ");
printf("%lld",s);
for (int i = 0; i < t; i++)
printf("0");
printf("\n");
s = 1;
f++;
t=0;
p++;
}
if (f>1)
{
PrintLine(len);
s = x*y;
printf("%lld\n",s);
}
}
return 0;
}
1853: Su-domino-ku
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define rep(i,j,k) for (int i = j; i <= k; i++)
const int maxn = 5e5 + 10;
int n, a, b, tot, X, Y, T = 0;
int r[10][10], c[10][10], d[10][10], f[10][10], mp[10][10];
int p[maxn][6];
char x[3], y[3], s[11][11];
struct DLX
{
int L[maxn], R[maxn], U[maxn], D[maxn];
int row[maxn], col[maxn], ans[maxn], cnt[maxn];
int n, m, num, sz;
void add(int now, int l, int r, int u, int d, int x, int y)
{
L[now] = l; R[now] = r; U[now] = u;
D[now] = d; row[now] = x; col[now] = y;
}
void reset(int n, int m)
{
this->n = n; this->m = m;
for (int i = 0; i <= m; i++)
{
add(i, i - 1, i + 1, i, i, 0, i);
cnt[i] = 0;
}
L[0] = m; num = R[m] = 0; sz = m + 1;
}
void insert(int x, int y)
{
int ft = sz - 1;
if (row[ft] != x)
{
add(sz, sz, sz, U[y], y, x, y);
U[D[sz]] = sz; D[U[sz]] = sz;
}
else
{
add(sz, ft, R[ft], U[y], y, x, y);
R[L[sz]] = sz; L[R[sz]] = sz;
U[D[sz]] = sz; D[U[sz]] = sz;
}
++cnt[y]; ++sz;
}
void remove(int now)
{
R[L[now]] = R[now];
L[R[now]] = L[now];
for (int i = D[now]; i != now; i = D[i])
for (int j = R[i]; j != i; j = R[j])
{
D[U[j]] = D[j];
U[D[j]] = U[j];
--cnt[col[j]];
}
}
void resume(int now)
{
for (int i = U[now]; i != now; i = U[i])
for (int j = L[i]; j != i; j = L[j])
{
D[U[j]] = j;
U[D[j]] = j;
++cnt[col[j]];
}
R[L[now]] = now;
L[R[now]] = now;
}
bool dfs(int x)
{
if (!R[0]) { num = x; return true; }
int now = R[0];
for (int i = now; i != 0; i = R[i])
if (cnt[now]>cnt[i]) now = i;
remove(now);
for (int i = D[now]; i != now; i = D[i])
{
ans[x] = row[i];
for (int j = R[i]; j != i; j = R[j]) remove(col[j]);
if (dfs(x + 1)) return true;
for (int j = L[i]; j != i; j = L[j]) resume(col[j]);
}
resume(now);
return false;
}
void display()
{
for (int i = 0; i < num; ++i)
{
if (ans[i] == 1) continue;
s[p[ans[i]][0]][p[ans[i]][1]] = p[ans[i]][2] + '0';
s[p[ans[i]][3]][p[ans[i]][4]] = p[ans[i]][5] + '0';
}
for (int i = 1; i <= 9; i++) s[i][10] = 0, puts(s[i] + 1);
}
}dlx;
int un(int x, int y) { return (x - 1) / 3 * 3 + (y - 1) / 3; }
void insert(int x, int y, int z)
{
dlx.insert(tot, 9 * (x - 1) + y);
dlx.insert(tot, 81 + 9 * (x - 1) + z);
dlx.insert(tot, 162 + 9 * (y - 1) + z);
dlx.insert(tot, 243 + 9 * un(x, y) + z);
}
int main()
{
while (scanf("%d", &n) != EOF, n)
{
tot = 1;
dlx.reset(2000, 405);
memset(r, 0, sizeof(r));
memset(f, 0, sizeof(f));
memset(c, 0, sizeof(c));
memset(d, 0, sizeof(d));
memset(mp, 0, sizeof(mp));
rep(i, 1, n)
{
scanf("%d%s%d%s", &a, x, &b, y);
f[min(a, b)][max(a, b)] = 1;
X = x[0] - 'A' + 1, Y = x[1] - '0';
r[X][a] = c[Y][a] = mp[X][Y] = d[un(X, Y)][a] = 1;
insert(X, Y, a); s[X][Y] = '0' + a;
X = y[0] - 'A' + 1, Y = y[1] - '0';
r[X][b] = c[Y][b] = mp[X][Y] = d[un(X, Y)][b] = 1;
insert(X, Y, b); s[X][Y] = '0' + b;
dlx.insert(tot, 324 + (a - 1) * 9 + b);
dlx.insert(tot, 324 + (b - 1) * 9 + a);
}
rep(i, 1, 9)
{
scanf("%s", x);
X = x[0] - 'A' + 1, Y = x[1] - '0';
r[X][i] = c[Y][i] = mp[X][Y] = d[un(X, Y)][i] = 1;
insert(X, Y, i); s[X][Y] = '0' + i;
dlx.insert(tot, 324 + (i - 1) * 9 + i);
}
rep(i, 1, 9)
{
rep(j, 1, 9)
{
if (mp[i][j]) continue;
rep(x, 1, 9)
{
if (r[i][x] || c[j][x] || d[un(i, j)][x]) continue;
rep(y, 1, 9)
{
if (x == y || f[min(x, y)][max(x, y)]) continue;
if (i + 1 < 10 && !mp[i + 1][j])
{
if (!(r[i + 1][y] || c[j][y] || d[un(i + 1, j)][y]))
{
++tot;
insert(i, j, x);
insert(i + 1, j, y);
dlx.insert(tot, 324 + 9 * (x - 1) + y);
dlx.insert(tot, 324 + 9 * (y - 1) + x);
p[tot][0] = i; p[tot][1] = j; p[tot][2] = x;
p[tot][3] = i + 1; p[tot][4] = j; p[tot][5] = y;
}
}
if (j + 1 < 10 && !mp[i][j + 1])
{
if (!(r[i][y] || c[j + 1][y] || d[un(i, j + 1)][y]))
{
++tot;
insert(i, j, x);
insert(i, j + 1, y);
dlx.insert(tot, 324 + 9 * (x - 1) + y);
dlx.insert(tot, 324 + 9 * (y - 1) + x);
p[tot][0] = i; p[tot][1] = j; p[tot][2] = x;
p[tot][3] = i; p[tot][4] = j + 1; p[tot][5] = y;
}
}
}
}
}
}
dlx.dfs(0);
printf("Puzzle %d\n", ++T);
dlx.display();
}
return 0;
}
1857: Crash and Go(relians)
#include <cstdio>
#include <iostream>
#include <set>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
using ll=long long;
const int maxn= 1e2 +9;
int n;
struct po
{
double x,y,r;
}te;
vector<po> vp;
double dis(po u,po v)
{
return sqrt((u.x-v.x)*(u.x-v.x)+(u.y-v.y)*(u.y-v.y));
}
bool ok(po u,po v)
{
return dis(u,v)<max(u.r,v.r);
}
void sol(int pos,vector<po>& tt)
{
double x=0,y=0,r=0;
for(int i=0;i<tt.size();i++){
x+=tt[i].x;
y+=tt[i].y;
r+=tt[i].r*tt[i].r;
}
vp[pos].x=x/tt.size();
vp[pos].y=y/tt.size();
vp[pos].r=sqrt(r);
}
int main()
{
while(cin>>n){
if(n==0) break;
vp.clear();
for(int i=0;i<n;i++){
double u,v,r;
cin>>u>>v>>r;
vp.push_back(po{u,v,r});
}
for(int i=0;i<n;i++) {
vector<po> feasible;
feasible.push_back(vp[i]);
for(int j=0;j<i;j++) if(vp[j].r>0)
{
if(ok(vp[i],vp[j])){
feasible.push_back(vp[j]);
vp[j].r=-1;
}
}
if(feasible.size()>1){
sol(i,feasible);
//这块就是新合成的一个圆 所以你要把它再循环一遍看之前有没有可以联系上的其他人
i--;
}
}
int ans=0;
for(int i=0;i<n;i++){ if(vp[i].r>0) ans++;
}
cout<<ans<<endl;
}
return 0;
}
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<vector>
#include<set>
using namespace std;
const int maxn=25+5;
int n,toalltime;
int fishBone[maxn],cost[maxn],finalTime[maxn];
struct node
{
int fishes;
int id;
int theMinus;
node(int fishes=0,int id=0,int theMinus=0):fishes(fishes),id(id),theMinus(theMinus){};
bool operator <(const node& p) const
{
return fishes>p.fishes||(fishes==p.fishes&&id<p.id);
}
}t[maxn];
int main()
{
while(~scanf("%d",&n))
{
if(!n)
return 0;
int ans=-1;
scanf("%d",&toalltime);
toalltime*=60;
memset(fishBone,0,sizeof(fishBone));
for(int i=0;i<n;i++)
{
int x;
scanf("%d",&x);
t[i].id=i;
t[i].fishes=x;
}
for(int i=0;i<n;i++)
{
int x;
scanf("%d",&x);
t[i].theMinus=x;
}
for(int i=0;i<n-1;i++)
{
scanf("%d",&cost[i]);
}
int tmptime[maxn];
int tmpsum=0;
for(int i=0;i<n;i++)
{
int tmptoall=toalltime;
tmpsum=0;
memset(tmptime,0,sizeof(tmptime));
set<node>Q;
for(int j=i;j>=0;j--)
{
if(j-1>=0)
tmptoall-=(cost[j-1]*5);
Q.insert(t[j]);
}
while(tmptoall)
{
node first=*Q.begin();
Q.erase(Q.begin());
tmpsum+=first.fishes;
first.fishes-=first.theMinus;
if(first.fishes<0)
first.fishes=0;
Q.insert(first);
tmptime[first.id]+=5;
tmptoall-=5;
if(tmptoall<5)
tmptoall=0;
}
if(tmpsum>ans)
{
ans=tmpsum;
memcpy(fishBone,tmptime,sizeof(tmptime));
}
}
for(int i=0;i<n;i++)
{
printf("%d%s",fishBone[i],i==n-1?"\n":", ");
}
cout<<"Number of fish expected: "<<ans<<endl;
cout<<endl;
}
return 0;
}
1861: Unscrambling Images
#include <iostream>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <map>
using namespace std;
typedef pair<int,int> pa;
int arr[300],raw[20][20],board[20][20],n,k,kcase;
map<int,int> ma;
void create(int arr[][20],pa le=make_pair(1,1),int len=n,int node=0);
int main(){
ios_base::sync_with_stdio(false);
int times;
cin>>times;
while(times--){
cout<<"Case "<<++kcase<<endl<<endl;
cin>>n;
cin>>k;
for(int i=0,a,b;i<k;++i)
cin>>a>>b,ma.insert(make_pair(a,b));
create(raw);
ma.clear();
cin>>k;
for(int i=0,a,b;i<k;++i)
cin>>a>>b,ma.insert(make_pair(a,b));
create(board);
ma.clear();
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
arr[raw[i][j]]=board[i][j];
for(int i=1,tmp=0;i<=n;++i){
for(int j=1;j<=n;++j)
cout<<setw(4)<<arr[tmp++];
cout<<endl;
}
cout<<endl;
}
return 0;
}
void create(int arr[][20],pa le,int len,int node){
if(ma.find(node)!=ma.end()){
int tmp=ma[node];
for(int i=0;i<len;++i)
for(int j=0;j<len;++j)
arr[le.first+i][le.second+j]=tmp;
}else{
create(arr,le,(len>>1),(node<<2)+1);
create(arr,make_pair(le.first+(len>>1),le.second),(len>>1),(node<<2)+2);
create(arr,make_pair(le.first,(le.second)+(len>>1)),(len>>1),(node<<2)+3);
create(arr,make_pair(le.first+(len>>1),le.second+(len>>1)),(len>>1),(node<<2)+4);
}
}
1862: The Same Game
#include <cstdio>
#include <bitset>
#include <iostream>
#include <set>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long ll;
const int maxn= 30 +9;
string s[maxn];
int n=10,m=15,vis[maxn][maxn];
struct pn
{
int x,y;
};
void dfs(int x,int y,char c,int& t)
{
if(x<0||x>=n||y<0||y>=m||vis[x][y]) return;
if(s[x][y]!=c) return;
t++;
vis[x][y]=1;
for(int i=-1;i<2;i++){
for(int j=-1;j<2;j++) if(!i^!j){
dfs(x+i,y+j,c,t);
}
}
}
void dfs2(int x,int y,char c)
{
if(x<0||x>=n||y<0||y>=m||vis[x][y]) return;
if(s[x][y]!=c) return;
s[x][y]='.';
vis[x][y]=1;
for(int i=-1;i<2;i++){
for(int j=-1;j<2;j++) if(!i^!j){
dfs2(x+i,y+j,c);
}
}
}
int K;
vector<int> vi;
bool allem(int y)
{
for(int i=0;i<n;i++){
if(s[i][y]!='.')
return false;
}
return true;
}
void smove()
{
//down
for(int j=0;j<m;j++){
for(int i=n-1;i>=0;i--){
if(s[i][j]=='.'){
for(int k=i;k>=0;k--){
if(s[k][j]!='.'){
swap(s[i][j],s[k][j]);
break;
}
}
}
}
}
//left
for(int j=0;j<m;j++){
if(allem(j)){
for(int k=j;k<m;k++){
if(!allem(k)){
for(int i=0;i<n;i++)
swap(s[i][j],s[i][k]);
break;
}
}
}
}
}
int okw()
{
int ans=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++){
if(s[i][j]!='.')
ans++;
}
return ans;
}
int main()
{
int T; cin>>T;
while(T--){
ll fin=0;
for(int i=0;i<n;i++){
cin>>s[i];
}
printf("Game %d:\n\n",++K);
int KK=0;
for(;1;){
vi.clear();
memset(vis,0,sizeof(vis));
int num=0,J=0;
pn p;
char CC;
for(int j=0;j<m;j++)
for(int i=n-1;i>=0;i--) if(!vis[i][j]&&s[i][j]!='.'){
int t=0;
dfs(i,j,s[i][j],t);
J++;
if(t<=1) continue;
vi.push_back(t);
if(t>num){
CC=s[i][j];
p.x=i,p.y=j;
num=t;
}
else if(t==num){
if(j<p.y&&i>p.x){
CC=s[i][j];
p.x=i,p.y=j;
}
}
}
memset(vis,0,sizeof(vis));
if(vi.empty()||okw()==0){
break;
}
dfs2(p.x,p.y,CC);
printf("Move %d at (%d,%d): removed %d balls of color %c, got %lld points.\n",++KK,n-p.x,p.y+1,num,CC,(ll)(num-2)*(num-2));
fin+=(ll)(num-2)*(num-2);
smove();
}
if(!okw()){
fin+=1000;
}
printf("Final score: %lld, with %d balls remaining.\n",fin,okw());
if(K!=n) puts("");
}
return 0;
}
1865: Triangle War
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<bitset>
#define LL long long
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define down(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
inline LL read()
{
LL d=0,f=1;char s=getchar();
while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
while(s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
return d*f;
}
#define N 18
#define MIN -10
int tri[9]={7,52,152,352,3200,12544,34304,71680,155648};
int m=(1<<18)-1;
int map[11][11]={0};
int pri[4][2]={1,0,0,1,-1,0,0,-1};
int f[(1<<19)+5];
int n;
void getmap()
{
memset(map,0,sizeof(map));
map[1][2]=1;map[1][3]=2;
map[2][3]=3;map[2][4]=4;map[2][5]=5;
map[3][5]=6;map[3][6]=7;
map[4][5]=8;map[4][7]=10;map[4][8]=11;
map[5][6]=9;map[5][8]=12;map[5][9]=13;
map[6][9]=14;map[6][10]=15;
map[7][8]=16;
map[8][9]=17;
map[9][10]=18;
fo(i,1,9)
fo(j,i+1,10)
if(map[i][j])
map[j][i]=map[i][j];
}
int getvalue(int old,int now)
{
int ret=0;
fo(i,0,8)
if((old&tri[i])!=tri[i]&&(now&tri[i])==tri[i])
ret++;
return ret;
}
int calc(int now)
{
if(f[now]!=MIN)return f[now];
int cnt=getvalue(0,now),ma=MIN;
bitset<20>t;t=now;
fo(i,0,17)
if(t[i]==0)
{
t[i]=1;
int ne=t.to_ulong();
int val=getvalue(now,ne);
if(val>0)val+=calc(ne);
else val+=-1*calc(ne);
ma=max(ma,val);
t[i]=0;
}
return f[now]=ma;
}
void solve(int ch)
{
n=read();
int player=1,cnt=0;
bitset<20>t;t.reset();
fo(i,1,n)
{
int x=read(),y=read();
int k=0;
if(map[x][y])
{
// cout<<"B:"<<map[x][y]<<endl;
unsigned long old=t.to_ulong();
t[map[x][y]-1]=1;
unsigned long now=t.to_ulong();
k=getvalue(old,now);
}
if(k)cnt+=player*k;
else player*=-1;
}
unsigned long now=t.to_ulong();
cnt+=player*calc(now);
if(cnt>0)printf("Game %d: A wins.\n",ch);
else printf("Game %d: B wins.\n",ch);
}
int main()
{
getmap();
fill(f,f+(1<<19),-10);
f[m]=0;
int ttttt=read();
fo(i,1,ttttt)solve(i);
return 0;
}
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int maxn=100005;
const int maxm=400005;
const long long inf=1e20;
int head[maxn];
typedef struct Edge {
int to;
int cost;
int next;
} edge;
edge edges[maxm];
int cnt,inque[maxn];
long long dis[maxn];
int n,m;
void init()
{
memset(head, -1, sizeof(head));
memset(inque, 0, sizeof(inque));
cnt = 0;
}
void add_edge(int from , int to, int cost)
{
edges[cnt].to = to;
edges[cnt].cost = cost;
edges[cnt].next = head[from];
head[from] = cnt++;
}
queue<int> que;
void spfa()
{
while(que.size()) que.pop();
for(int i = 1; i <= n; i++) dis[i] = inf;
dis[1] = 0,inque[1] = 1;
que.push(1);
while(que.size()) {
int u = que.front();
que.pop();
inque[u] = 0;
for(int i = head[u]; i != -1; i = edges[i].next) {
if(dis[edges[i].to] > dis[u] + edges[i].cost) {
dis[edges[i].to] = dis[u] + edges[i].cost;
if(!inque[edges[i].to]) {
que.push(edges[i].to);
inque[edges[i].to] = 1;
}
}
}
}
return ;
}
int main(void)
{
std::ios::sync_with_stdio(false);
while(cin>>n && n) {
cin>>m;
init();
int u,v,w;
for(int i = 0; i < m; i++) {
cin>>u>>v>>w;
add_edge(u,v,-w);
}
spfa();
cout<<-dis[n]<<endl;
}
return 0;
}
1886: Phone List
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
struct P{
char s[101];
}a[10001];
bool cmp(P a,P b)///排序
{
if(strcmp(a.s,b.s)>0) return false;
else return true;
}
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%s",&a[i].s);
sort(a,a+n,cmp);
int f=0;
for(int i=0;i<n;i++)
{
if(strncmp(a[i].s,a[i+1].s,strlen(a[i].s))==0)
{f=1; break;}
}
if(f) printf("NO\n");
else printf("YES\n");
}
return 0;
}
1887: Cuckoo Hashing
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
const int maxn = 10000+5;
int h1[maxn],h2[maxn];
int vis[maxn];
int pos[maxn];
int T,m,n;
int dfs(int a)
{
for(int i = 0;i<2;i++)
{
int tmp;
if(!i)
tmp = h1[a];
else
tmp = h2[a];
if(!vis[tmp])
{ //cout<<a<<' '<<tmp<<' '<<"pos"<<tmp<<' '<<pos[tmp]<<endl;
vis[tmp] = 1;
if(pos[tmp]==-1||dfs(pos[tmp]))
{
pos[tmp] = a;
return 1;
}
}
}
return 0;
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
memset(pos,-1,sizeof(pos));
for(int i = 0;i <n ;i++)
{
scanf("%d%d",&h1[i],&h2[i]);
}
int ok = 1;
for(int i = 0; i< n;i++)
{
memset(vis,0,sizeof(vis));
if(!dfs(i))
{
// cout<<i<<endl;
ok = 0;
break;
}
}
if(ok)
{
puts("successful hashing");
}
else
{
puts("rehash necessary");
}
}
return 0;
}
1888: Optimal Parking
#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;
int main(){
int i,j,n,t,max,min,ans,p;
scanf("%d",&t);
for(i=1;i<=t;i++){
ans=max=0;
min=200;
scanf("%d",&n);
for(j=1;j<=n;j++){
scanf("%d",&p);
if(p>max)max=p;
if(p<min)min=p;
}
ans=2*(max-min);
printf("%d\n",ans);
}
return 0;
}
1889: Copying DNA
#include <iostream>
#include <cstring>
#include <algorithm>
#include <bitset>
using namespace std;
const int maxm=20;
int memo[1<<maxm],n,m,times,match(int i,int g,char* str,int e),solve(int found);
char s[maxm],sr[maxm],t[maxm];
int main(){
ios_base::sync_with_stdio(0);
cin>>times;
while(times--){
cin>>s>>t;
m=strlen(s),n=strlen(t);
reverse_copy(s,s+m,sr);
memset(memo,-1,sizeof(int)*(1<<n));
memo[(1<<n)-1]=0;
if(solve(0)==0x3f3f3f3f)
cout<<"impossible\n";
else
cout<<solve(0)<<endl;
}
return 0;
}
int match(int i, int g, char* str, int e){
int longest=0;
for(int k=0,h=0;k<e;++k,h=0){
while(h<g&&k+h<e&&str[k+h]==t[i+h])++h;
longest=max(h,longest);
}
return longest;
}
int solve(int found){
if(memo[found]!=-1)
return memo[found];
char u[maxm]{},ur[maxm]{};
int best=0x3f3f3f3f,last_h=0;
for(int i=0;i<n;++i)
if(found&(1<<i))u[i]=t[i];
reverse_copy(u,u+n,ur);
for(int i=0,g=0,h=0;i<n;++i,g=h=0){
while(i+g<n&&!(found&(1<<i+g)))++g;
if(g==0)
last_h=0;
else{
h=max(max(match(i,g,sr,m),match(i,g,s,m)),max(match(i,g,u,n),match(i,g,ur,n)));
if(h==0)return 0x3f3f3f3f;
if(h>last_h-1)
best=min(best,1+solve(found|((1<<i+h)-1)^((1<<i)-1)));
if(best==0x3f3f3f3f)
break;
last_h=h;
}
}
return memo[found]=best;
}
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxm=10010,maxn=1010,maxc=110;
struct Edge{
int v,nt,c;
explicit Edge(int a=0,int b=0,int cc=0):v(a),nt(b),c(cc){};
}edge[maxm<<1];
struct Node{
int cost,city,fuel;
explicit Node(int a=0,int b=0,int c=0):cost(a),city(b),fuel(c){};
bool operator<(const Node& n)const{return cost>n.cost;}
}tmp;
int n,m,fi[maxn],p[maxn],x,y,c,ind,q,s,e,co[maxn][maxc];
bool vis[maxn][maxc];
priority_queue<Node> pq;
inline void addeage(){
edge[ind]=Edge(x,fi[y],c);
fi[y]=ind++;
edge[ind]=Edge(y,fi[x],c);
fi[x]=ind++;
}
int main(){
ios_base::sync_with_stdio(0);
while(cin>>n>>m){
memset(fi,-1,sizeof(int)*(n+1));
for(int i=0;i<n;++i)
cin>>p[i];
for(int i=0;i<m;++i){
cin>>x>>y>>c;
addeage();
}
cin>>q;
while(q--){
cin>>c>>s>>e;
memset(co,0x3f3f3f3f,sizeof(co[0])*(n+1));
memset(vis,0,sizeof(vis[0])*(n+1));
co[s][0]=0;
pq.push(Node(0,s,0));
while(!pq.empty()){
tmp=pq.top();pq.pop();
if(vis[tmp.city][tmp.fuel])
continue;
if(tmp.city==e)
break;
if(tmp.fuel<c&&tmp.cost+p[tmp.city]<co[tmp.city][tmp.fuel+1]){
co[tmp.city][tmp.fuel+1]=tmp.cost+p[tmp.city];
pq.push(Node(tmp.cost+p[tmp.city],tmp.city,tmp.fuel+1));
}
for(int i=fi[tmp.city];i!=-1;i=edge[i].nt)
if(tmp.fuel-edge[i].c>=0&&co[tmp.city][tmp.fuel]<co[edge[i].v][tmp.fuel-edge[i].c]){
co[edge[i].v][tmp.fuel-edge[i].c]=co[tmp.city][tmp.fuel];
pq.push(Node(co[edge[i].v][tmp.fuel-edge[i].c],edge[i].v,tmp.fuel-edge[i].c));
}
vis[tmp.city][tmp.fuel]=true;
}
if(co[e][0]<0x3f3f3f3f)
cout<<co[e][0]<<endl;
else
cout<<"impossible\n";
while(!pq.empty())pq.pop();
}
ind=0;
}
return 0;
}
1892: Nested Dolls
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
struct Doll{
int width,height;
}doll[20005],selected[20005];
bool cmp(Doll a,Doll b){
if(a.width==b.width)
return a.height>b.height; //这里只可用 >,用 < WA了
return a.width<b.width;
}
int main(){
int t,n,cnt;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d%d",&doll[i].width,&doll[i].height);
sort(doll,doll+n,cmp);
cnt=0;
for(int i=0;i<n;i++){
int left=0,right=cnt,mid;
while(left<right){
mid=(left+right)/2;
if(doll[i].width<=selected[mid].width ||
doll[i].height<=selected[mid].height)
left=mid+1;
else
right=mid;
}
if(left==cnt)
selected[cnt++]=doll[i];
else
selected[left]=doll[i];
}
printf("%d\n",cnt);
}
return 0;
}
1897: The price table of the snack store
#include<iostream>
#include<cstdio>
using namespace std;
typedef long long LL;
int L[10005];
LL c[10005],x[10005];
int main()
{
int T;
scanf("%d",&T);
LL sigmax;
while(T--)
{
int k,n,m;
scanf("%d%d%d",&k,&n,&m);
sigmax=0;
for(int i=1;i<=k;i++)
{
scanf("%lld%d",&c[i],&L[i]);
sigmax+=c[i];
}
sigmax=sigmax/(n+(k-1)*m);
for(int i=1;i<=k;i++)
x[L[i]]=(c[i]-sigmax*m)/(n-m);
for(int i=1;i<=k;i++)
{
if(i==1) printf("%lld",x[i]);
else printf(" %lld",x[i]);
}
printf("\n");
}
return 0;
}